+ if (!(mutable->flags & TNL_F_HDR_CACHE))
+ return NULL;
+
+ /*
+ * If there is no entry in the ARP cache or if this device does not
+ * support hard header caching just fall back to the IP stack.
+ */
+ if (!rt_dst(rt).hh)
+ return NULL;
+
+ /*
+ * If lock is contended fall back to directly building the header.
+ * We're not going to help performance by sitting here spinning.
+ */
+ if (!spin_trylock_bh(&tnl_vport->cache_lock))
+ return NULL;
+
+ cache = cache_dereference(tnl_vport);
+ if (check_cache_valid(cache, mutable))
+ goto unlock;
+ else
+ cache = NULL;
+
+ cache_len = rt_dst(rt).hh->hh_len + mutable->tunnel_hlen;
+
+ cache = kzalloc(ALIGN(sizeof(struct tnl_cache), CACHE_DATA_ALIGN) +
+ cache_len, GFP_ATOMIC);
+ if (!cache)
+ goto unlock;
+
+ cache->len = cache_len;
+
+ create_eth_hdr(cache, rt);
+ cache_data = get_cached_header(cache) + rt_dst(rt).hh->hh_len;
+
+ create_tunnel_header(vport, mutable, rt, cache_data);
+
+ cache->mutable_seq = mutable->seq;
+ cache->rt = rt;
+#ifdef NEED_CACHE_TIMEOUT
+ cache->expiration = jiffies + tnl_vport->cache_exp_interval;
+#endif
+
+ if (is_internal_dev(rt_dst(rt).dev)) {
+ struct sw_flow_key flow_key;
+ struct tbl_node *flow_node;
+ struct vport *dst_vport;
+ struct sk_buff *skb;
+ bool is_frag;
+ int err;
+ int flow_key_len;
+
+ dst_vport = internal_dev_get_vport(rt_dst(rt).dev);
+ if (!dst_vport)
+ goto done;
+
+ skb = alloc_skb(cache->len, GFP_ATOMIC);
+ if (!skb)
+ goto done;
+
+ __skb_put(skb, cache->len);
+ memcpy(skb->data, get_cached_header(cache), cache->len);
+
+ err = flow_extract(skb, dst_vport->port_no, &flow_key,
+ &flow_key_len, &is_frag);
+
+ kfree_skb(skb);
+ if (err || is_frag)
+ goto done;
+
+ flow_node = tbl_lookup(rcu_dereference(dst_vport->dp->table),
+ &flow_key, flow_key_len,
+ flow_hash(&flow_key, flow_key_len),
+ flow_cmp);
+ if (flow_node) {
+ struct sw_flow *flow = flow_cast(flow_node);
+
+ cache->flow = flow;
+ flow_hold(flow);
+ }