2 * Copyright (c) 2010 Nicira Networks.
3 * Distributed under the terms of the GNU GPL version 2.
5 * Significant portions of this file may be copied from parts of the Linux
6 * kernel, by Linus Torvalds and others.
9 #include <linux/if_arp.h>
10 #include <linux/if_ether.h>
12 #include <linux/if_vlan.h>
14 #include <linux/in_route.h>
15 #include <linux/jhash.h>
16 #include <linux/kernel.h>
17 #include <linux/version.h>
18 #include <linux/workqueue.h>
20 #include <net/dsfield.h>
23 #include <net/inet_ecn.h>
25 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
28 #include <net/route.h>
37 #include "vport-generic.h"
38 #include "vport-internal_dev.h"
40 #ifdef NEED_CACHE_TIMEOUT
42 * On kernels where we can't quickly detect changes in the rest of the system
43 * we use an expiration time to invalidate the cache. A shorter expiration
44 * reduces the length of time that we may potentially blackhole packets while
45 * a longer time increases performance by reducing the frequency that the
46 * cache needs to be rebuilt. A variety of factors may cause the cache to be
47 * invalidated before the expiration time but this is the maximum. The time
48 * is expressed in jiffies.
50 #define MAX_CACHE_EXP HZ
54 * Interval to check for and remove caches that are no longer valid. Caches
55 * are checked for validity before they are used for packet encapsulation and
56 * old caches are removed at that time. However, if no packets are sent through
57 * the tunnel then the cache will never be destroyed. Since it holds
58 * references to a number of system objects, the cache will continue to use
59 * system resources by not allowing those objects to be destroyed. The cache
60 * cleaner is periodically run to free invalid caches. It does not
61 * significantly affect system performance. A lower interval will release
62 * resources faster but will itself consume resources by requiring more frequent
63 * checks. A longer interval may result in messages being printed to the kernel
64 * message buffer about unreleased resources. The interval is expressed in
67 #define CACHE_CLEANER_INTERVAL (5 * HZ)
69 #define CACHE_DATA_ALIGN 16
71 /* Protected by RCU. */
72 static struct tbl *port_table __read_mostly;
74 static void cache_cleaner(struct work_struct *work);
75 DECLARE_DELAYED_WORK(cache_cleaner_wq, cache_cleaner);
78 * These are just used as an optimization: they don't require any kind of
79 * synchronization because we could have just as easily read the value before
80 * the port change happened.
82 static unsigned int key_local_remote_ports __read_mostly;
83 static unsigned int key_remote_ports __read_mostly;
84 static unsigned int local_remote_ports __read_mostly;
85 static unsigned int remote_ports __read_mostly;
87 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
88 #define rt_dst(rt) (rt->dst)
90 #define rt_dst(rt) (rt->u.dst)
93 static inline struct vport *tnl_vport_to_vport(const struct tnl_vport *tnl_vport)
95 return vport_from_priv(tnl_vport);
98 static inline struct tnl_vport *tnl_vport_table_cast(const struct tbl_node *node)
100 return container_of(node, struct tnl_vport, tbl_node);
103 static inline void schedule_cache_cleaner(void)
105 schedule_delayed_work(&cache_cleaner_wq, CACHE_CLEANER_INTERVAL);
108 static void free_cache(struct tnl_cache *cache)
113 flow_put(cache->flow);
114 ip_rt_put(cache->rt);
118 static void free_config_rcu(struct rcu_head *rcu)
120 struct tnl_mutable_config *c = container_of(rcu, struct tnl_mutable_config, rcu);
124 static void free_cache_rcu(struct rcu_head *rcu)
126 struct tnl_cache *c = container_of(rcu, struct tnl_cache, rcu);
130 static void assign_config_rcu(struct vport *vport,
131 struct tnl_mutable_config *new_config)
133 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
134 struct tnl_mutable_config *old_config;
136 old_config = tnl_vport->mutable;
137 rcu_assign_pointer(tnl_vport->mutable, new_config);
138 call_rcu(&old_config->rcu, free_config_rcu);
141 static void assign_cache_rcu(struct vport *vport, struct tnl_cache *new_cache)
143 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
144 struct tnl_cache *old_cache;
146 old_cache = tnl_vport->cache;
147 rcu_assign_pointer(tnl_vport->cache, new_cache);
150 call_rcu(&old_cache->rcu, free_cache_rcu);
153 static unsigned int *find_port_pool(const struct tnl_mutable_config *mutable)
155 if (mutable->port_config.flags & TNL_F_IN_KEY_MATCH) {
156 if (mutable->port_config.saddr)
157 return &local_remote_ports;
159 return &remote_ports;
161 if (mutable->port_config.saddr)
162 return &key_local_remote_ports;
164 return &key_remote_ports;
168 struct port_lookup_key {
173 const struct tnl_mutable_config *mutable;
177 * Modifies 'target' to store the rcu_dereferenced pointer that was used to do
180 static int port_cmp(const struct tbl_node *node, void *target)
182 const struct tnl_vport *tnl_vport = tnl_vport_table_cast(node);
183 struct port_lookup_key *lookup = target;
185 lookup->mutable = rcu_dereference(tnl_vport->mutable);
187 return (lookup->mutable->tunnel_type == lookup->tunnel_type &&
188 lookup->mutable->port_config.daddr == lookup->daddr &&
189 lookup->mutable->port_config.in_key == lookup->key &&
190 lookup->mutable->port_config.saddr == lookup->saddr);
193 static u32 port_hash(struct port_lookup_key *k)
195 return jhash_3words(k->key, k->saddr, k->daddr, k->tunnel_type);
198 static u32 mutable_hash(const struct tnl_mutable_config *mutable)
200 struct port_lookup_key lookup;
202 lookup.saddr = mutable->port_config.saddr;
203 lookup.daddr = mutable->port_config.daddr;
204 lookup.key = mutable->port_config.in_key;
205 lookup.tunnel_type = mutable->tunnel_type;
207 return port_hash(&lookup);
210 static void check_table_empty(void)
212 if (tbl_count(port_table) == 0) {
213 struct tbl *old_table = port_table;
215 cancel_delayed_work_sync(&cache_cleaner_wq);
216 rcu_assign_pointer(port_table, NULL);
217 tbl_deferred_destroy(old_table, NULL);
221 static int add_port(struct vport *vport)
223 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
227 struct tbl *new_table;
229 new_table = tbl_create(0);
233 rcu_assign_pointer(port_table, new_table);
234 schedule_cache_cleaner();
236 } else if (tbl_count(port_table) > tbl_n_buckets(port_table)) {
237 struct tbl *old_table = port_table;
238 struct tbl *new_table;
240 new_table = tbl_expand(old_table);
241 if (IS_ERR(new_table))
242 return PTR_ERR(new_table);
244 rcu_assign_pointer(port_table, new_table);
245 tbl_deferred_destroy(old_table, NULL);
248 err = tbl_insert(port_table, &tnl_vport->tbl_node, mutable_hash(tnl_vport->mutable));
254 (*find_port_pool(tnl_vport->mutable))++;
259 static int move_port(struct vport *vport, struct tnl_mutable_config *new_mutable)
262 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
265 hash = mutable_hash(new_mutable);
266 if (hash == tnl_vport->tbl_node.hash)
270 * Ideally we should make this move atomic to avoid having gaps in
271 * finding tunnels or the possibility of failure. However, if we do
272 * find a tunnel it will always be consistent.
274 err = tbl_remove(port_table, &tnl_vport->tbl_node);
278 err = tbl_insert(port_table, &tnl_vport->tbl_node, hash);
285 assign_config_rcu(vport, new_mutable);
290 static int del_port(struct vport *vport)
292 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
295 err = tbl_remove(port_table, &tnl_vport->tbl_node);
300 (*find_port_pool(tnl_vport->mutable))--;
305 struct vport *tnl_find_port(__be32 saddr, __be32 daddr, __be32 key,
307 const struct tnl_mutable_config **mutable)
309 struct port_lookup_key lookup;
310 struct tbl *table = rcu_dereference(port_table);
311 struct tbl_node *tbl_node;
313 if (unlikely(!table))
316 lookup.saddr = saddr;
317 lookup.daddr = daddr;
319 if (tunnel_type & TNL_T_KEY_EXACT) {
321 lookup.tunnel_type = tunnel_type & ~TNL_T_KEY_MATCH;
323 if (key_local_remote_ports) {
324 tbl_node = tbl_lookup(table, &lookup, port_hash(&lookup), port_cmp);
329 if (key_remote_ports) {
332 tbl_node = tbl_lookup(table, &lookup, port_hash(&lookup), port_cmp);
336 lookup.saddr = saddr;
340 if (tunnel_type & TNL_T_KEY_MATCH) {
342 lookup.tunnel_type = tunnel_type & ~TNL_T_KEY_EXACT;
344 if (local_remote_ports) {
345 tbl_node = tbl_lookup(table, &lookup, port_hash(&lookup), port_cmp);
353 tbl_node = tbl_lookup(table, &lookup, port_hash(&lookup), port_cmp);
362 *mutable = lookup.mutable;
363 return tnl_vport_to_vport(tnl_vport_table_cast(tbl_node));
366 static inline void ecn_decapsulate(struct sk_buff *skb)
368 u8 tos = ip_hdr(skb)->tos;
370 if (INET_ECN_is_ce(tos)) {
371 __be16 protocol = skb->protocol;
372 unsigned int nw_header = skb_network_offset(skb);
374 if (skb->protocol == htons(ETH_P_8021Q)) {
375 if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
378 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
379 nw_header += VLAN_HLEN;
382 if (protocol == htons(ETH_P_IP)) {
383 if (unlikely(!pskb_may_pull(skb, nw_header
384 + sizeof(struct iphdr))))
387 IP_ECN_set_ce((struct iphdr *)(skb->data + nw_header));
389 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
390 else if (protocol == htons(ETH_P_IPV6)) {
391 if (unlikely(!pskb_may_pull(skb, nw_header
392 + sizeof(struct ipv6hdr))))
395 IP6_ECN_set_ce((struct ipv6hdr *)(skb->data + nw_header));
401 /* Called with rcu_read_lock. */
402 void tnl_rcv(struct vport *vport, struct sk_buff *skb)
404 /* Packets received by this function are in the following state:
405 * - skb->data points to the inner Ethernet header.
406 * - The inner Ethernet header is in the linear data area.
407 * - skb->csum does not include the inner Ethernet header.
408 * - The layer pointers point at the outer headers.
411 struct ethhdr *eh = (struct ethhdr *)skb->data;
413 if (likely(ntohs(eh->h_proto) >= 1536))
414 skb->protocol = eh->h_proto;
416 skb->protocol = htons(ETH_P_802_2);
421 skb_set_network_header(skb, ETH_HLEN);
423 ecn_decapsulate(skb);
424 compute_ip_summed(skb, false);
426 vport_receive(vport, skb);
429 static bool check_ipv4_address(__be32 addr)
431 if (ipv4_is_multicast(addr) || ipv4_is_lbcast(addr)
432 || ipv4_is_loopback(addr) || ipv4_is_zeronet(addr))
438 static bool ipv4_should_icmp(struct sk_buff *skb)
440 struct iphdr *old_iph = ip_hdr(skb);
442 /* Don't respond to L2 broadcast. */
443 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
446 /* Don't respond to L3 broadcast or invalid addresses. */
447 if (!check_ipv4_address(old_iph->daddr) ||
448 !check_ipv4_address(old_iph->saddr))
451 /* Only respond to the first fragment. */
452 if (old_iph->frag_off & htons(IP_OFFSET))
455 /* Don't respond to ICMP error messages. */
456 if (old_iph->protocol == IPPROTO_ICMP) {
457 u8 icmp_type, *icmp_typep;
459 icmp_typep = skb_header_pointer(skb, (u8 *)old_iph +
460 (old_iph->ihl << 2) +
461 offsetof(struct icmphdr, type) -
462 skb->data, sizeof(icmp_type),
468 if (*icmp_typep > NR_ICMP_TYPES
469 || (*icmp_typep <= ICMP_PARAMETERPROB
470 && *icmp_typep != ICMP_ECHOREPLY
471 && *icmp_typep != ICMP_ECHO))
478 static void ipv4_build_icmp(struct sk_buff *skb, struct sk_buff *nskb,
479 unsigned int mtu, unsigned int payload_length)
481 struct iphdr *iph, *old_iph = ip_hdr(skb);
482 struct icmphdr *icmph;
485 iph = (struct iphdr *)skb_put(nskb, sizeof(struct iphdr));
486 icmph = (struct icmphdr *)skb_put(nskb, sizeof(struct icmphdr));
487 payload = skb_put(nskb, payload_length);
491 iph->ihl = sizeof(struct iphdr) >> 2;
492 iph->tos = (old_iph->tos & IPTOS_TOS_MASK) |
493 IPTOS_PREC_INTERNETCONTROL;
494 iph->tot_len = htons(sizeof(struct iphdr)
495 + sizeof(struct icmphdr)
497 get_random_bytes(&iph->id, sizeof(iph->id));
500 iph->protocol = IPPROTO_ICMP;
501 iph->daddr = old_iph->saddr;
502 iph->saddr = old_iph->daddr;
507 icmph->type = ICMP_DEST_UNREACH;
508 icmph->code = ICMP_FRAG_NEEDED;
509 icmph->un.gateway = htonl(mtu);
512 nskb->csum = csum_partial((u8 *)icmph, sizeof(struct icmphdr), 0);
513 nskb->csum = skb_copy_and_csum_bits(skb, (u8 *)old_iph - skb->data,
514 payload, payload_length,
516 icmph->checksum = csum_fold(nskb->csum);
519 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
520 static bool ipv6_should_icmp(struct sk_buff *skb)
522 struct ipv6hdr *old_ipv6h = ipv6_hdr(skb);
524 int payload_off = (u8 *)(old_ipv6h + 1) - skb->data;
525 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
527 /* Check source address is valid. */
528 addr_type = ipv6_addr_type(&old_ipv6h->saddr);
529 if (addr_type & IPV6_ADDR_MULTICAST || addr_type == IPV6_ADDR_ANY)
532 /* Don't reply to unspecified addresses. */
533 if (ipv6_addr_type(&old_ipv6h->daddr) == IPV6_ADDR_ANY)
536 /* Don't respond to ICMP error messages. */
537 payload_off = ipv6_skip_exthdr(skb, payload_off, &nexthdr);
541 if (nexthdr == NEXTHDR_ICMP) {
542 u8 icmp_type, *icmp_typep;
544 icmp_typep = skb_header_pointer(skb, payload_off +
545 offsetof(struct icmp6hdr,
547 sizeof(icmp_type), &icmp_type);
549 if (!icmp_typep || !(*icmp_typep & ICMPV6_INFOMSG_MASK))
556 static void ipv6_build_icmp(struct sk_buff *skb, struct sk_buff *nskb,
557 unsigned int mtu, unsigned int payload_length)
559 struct ipv6hdr *ipv6h, *old_ipv6h = ipv6_hdr(skb);
560 struct icmp6hdr *icmp6h;
563 ipv6h = (struct ipv6hdr *)skb_put(nskb, sizeof(struct ipv6hdr));
564 icmp6h = (struct icmp6hdr *)skb_put(nskb, sizeof(struct icmp6hdr));
565 payload = skb_put(nskb, payload_length);
570 memset(&ipv6h->flow_lbl, 0, sizeof(ipv6h->flow_lbl));
571 ipv6h->payload_len = htons(sizeof(struct icmp6hdr)
573 ipv6h->nexthdr = NEXTHDR_ICMP;
574 ipv6h->hop_limit = IPV6_DEFAULT_HOPLIMIT;
575 ipv6_addr_copy(&ipv6h->daddr, &old_ipv6h->saddr);
576 ipv6_addr_copy(&ipv6h->saddr, &old_ipv6h->daddr);
579 icmp6h->icmp6_type = ICMPV6_PKT_TOOBIG;
580 icmp6h->icmp6_code = 0;
581 icmp6h->icmp6_cksum = 0;
582 icmp6h->icmp6_mtu = htonl(mtu);
584 nskb->csum = csum_partial((u8 *)icmp6h, sizeof(struct icmp6hdr), 0);
585 nskb->csum = skb_copy_and_csum_bits(skb, (u8 *)old_ipv6h - skb->data,
586 payload, payload_length,
588 icmp6h->icmp6_cksum = csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
589 sizeof(struct icmp6hdr)
591 ipv6h->nexthdr, nskb->csum);
595 bool tnl_frag_needed(struct vport *vport, const struct tnl_mutable_config *mutable,
596 struct sk_buff *skb, unsigned int mtu, __be32 flow_key)
598 unsigned int eth_hdr_len = ETH_HLEN;
599 unsigned int total_length = 0, header_length = 0, payload_length;
600 struct ethhdr *eh, *old_eh = eth_hdr(skb);
601 struct sk_buff *nskb;
604 if (skb->protocol == htons(ETH_P_IP)) {
605 if (mtu < IP_MIN_MTU)
608 if (!ipv4_should_icmp(skb))
611 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
612 else if (skb->protocol == htons(ETH_P_IPV6)) {
613 if (mtu < IPV6_MIN_MTU)
617 * In theory we should do PMTUD on IPv6 multicast messages but
618 * we don't have an address to send from so just fragment.
620 if (ipv6_addr_type(&ipv6_hdr(skb)->daddr) & IPV6_ADDR_MULTICAST)
623 if (!ipv6_should_icmp(skb))
631 if (old_eh->h_proto == htons(ETH_P_8021Q))
632 eth_hdr_len = VLAN_ETH_HLEN;
634 payload_length = skb->len - eth_hdr_len;
635 if (skb->protocol == htons(ETH_P_IP)) {
636 header_length = sizeof(struct iphdr) + sizeof(struct icmphdr);
637 total_length = min_t(unsigned int, header_length +
638 payload_length, 576);
640 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
642 header_length = sizeof(struct ipv6hdr) +
643 sizeof(struct icmp6hdr);
644 total_length = min_t(unsigned int, header_length +
645 payload_length, IPV6_MIN_MTU);
649 total_length = min(total_length, mutable->mtu);
650 payload_length = total_length - header_length;
652 nskb = dev_alloc_skb(NET_IP_ALIGN + eth_hdr_len + header_length +
657 skb_reserve(nskb, NET_IP_ALIGN);
659 /* Ethernet / VLAN */
660 eh = (struct ethhdr *)skb_put(nskb, eth_hdr_len);
661 memcpy(eh->h_dest, old_eh->h_source, ETH_ALEN);
662 memcpy(eh->h_source, mutable->eth_addr, ETH_ALEN);
663 nskb->protocol = eh->h_proto = old_eh->h_proto;
664 if (old_eh->h_proto == htons(ETH_P_8021Q)) {
665 struct vlan_ethhdr *vh = (struct vlan_ethhdr *)eh;
667 vh->h_vlan_TCI = vlan_eth_hdr(skb)->h_vlan_TCI;
668 vh->h_vlan_encapsulated_proto = skb->protocol;
670 skb_reset_mac_header(nskb);
673 if (skb->protocol == htons(ETH_P_IP))
674 ipv4_build_icmp(skb, nskb, mtu, payload_length);
675 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
677 ipv6_build_icmp(skb, nskb, mtu, payload_length);
681 * Assume that flow based keys are symmetric with respect to input
682 * and output and use the key that we were going to put on the
683 * outgoing packet for the fake received packet. If the keys are
684 * not symmetric then PMTUD needs to be disabled since we won't have
685 * any way of synthesizing packets.
687 if ((mutable->port_config.flags & (TNL_F_IN_KEY_MATCH | TNL_F_OUT_KEY_ACTION)) ==
688 (TNL_F_IN_KEY_MATCH | TNL_F_OUT_KEY_ACTION))
689 OVS_CB(nskb)->tun_id = flow_key;
691 compute_ip_summed(nskb, false);
692 vport_receive(vport, nskb);
697 static bool check_mtu(struct sk_buff *skb,
699 const struct tnl_mutable_config *mutable,
700 const struct rtable *rt, __be16 *frag_offp)
705 frag_off = (mutable->port_config.flags & TNL_F_PMTUD) ? htons(IP_DF) : 0;
707 mtu = dst_mtu(&rt_dst(rt))
709 - mutable->tunnel_hlen
710 - (eth_hdr(skb)->h_proto == htons(ETH_P_8021Q) ? VLAN_HLEN : 0);
714 if (skb->protocol == htons(ETH_P_IP)) {
715 struct iphdr *old_iph = ip_hdr(skb);
717 frag_off |= old_iph->frag_off & htons(IP_DF);
718 mtu = max(mtu, IP_MIN_MTU);
720 if ((old_iph->frag_off & htons(IP_DF)) &&
721 mtu < ntohs(old_iph->tot_len)) {
722 if (tnl_frag_needed(vport, mutable, skb, mtu, OVS_CB(skb)->tun_id))
726 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
727 else if (skb->protocol == htons(ETH_P_IPV6)) {
728 unsigned int packet_length = skb->len - ETH_HLEN
729 - (eth_hdr(skb)->h_proto == htons(ETH_P_8021Q) ? VLAN_HLEN : 0);
731 mtu = max(mtu, IPV6_MIN_MTU);
733 /* IPv6 requires PMTUD if the packet is above the minimum MTU. */
734 if (packet_length > IPV6_MIN_MTU)
735 frag_off = htons(IP_DF);
737 if (mtu < packet_length) {
738 if (tnl_frag_needed(vport, mutable, skb, mtu, OVS_CB(skb)->tun_id))
744 *frag_offp = frag_off;
752 static void create_tunnel_header(const struct vport *vport,
753 const struct tnl_mutable_config *mutable,
754 const struct rtable *rt, void *header)
756 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
757 struct iphdr *iph = header;
760 iph->ihl = sizeof(struct iphdr) >> 2;
761 iph->frag_off = htons(IP_DF);
762 iph->protocol = tnl_vport->tnl_ops->ipproto;
763 iph->tos = mutable->port_config.tos;
764 iph->daddr = rt->rt_dst;
765 iph->saddr = rt->rt_src;
766 iph->ttl = mutable->port_config.ttl;
768 iph->ttl = dst_metric(&rt_dst(rt), RTAX_HOPLIMIT);
770 tnl_vport->tnl_ops->build_header(vport, mutable, iph + 1);
773 static inline void *get_cached_header(const struct tnl_cache *cache)
775 return (void *)cache + ALIGN(sizeof(struct tnl_cache), CACHE_DATA_ALIGN);
778 static inline bool check_cache_valid(const struct tnl_cache *cache,
779 const struct tnl_mutable_config *mutable)
782 #ifdef NEED_CACHE_TIMEOUT
783 time_before(jiffies, cache->expiration) &&
786 atomic_read(&init_net.ipv4.rt_genid) == cache->rt->rt_genid &&
789 rt_dst(cache->rt).hh->hh_lock.sequence == cache->hh_seq &&
791 mutable->seq == cache->mutable_seq &&
792 (!is_internal_dev(rt_dst(cache->rt).dev) ||
793 (cache->flow && !cache->flow->dead));
796 static int cache_cleaner_cb(struct tbl_node *tbl_node, void *aux)
798 struct tnl_vport *tnl_vport = tnl_vport_table_cast(tbl_node);
799 const struct tnl_mutable_config *mutable = rcu_dereference(tnl_vport->mutable);
800 const struct tnl_cache *cache = rcu_dereference(tnl_vport->cache);
802 if (cache && !check_cache_valid(cache, mutable) &&
803 spin_trylock_bh(&tnl_vport->cache_lock)) {
804 assign_cache_rcu(tnl_vport_to_vport(tnl_vport), NULL);
805 spin_unlock_bh(&tnl_vport->cache_lock);
811 static void cache_cleaner(struct work_struct *work)
813 schedule_cache_cleaner();
816 tbl_foreach(port_table, cache_cleaner_cb, NULL);
820 static inline void create_eth_hdr(struct tnl_cache *cache,
821 const struct rtable *rt)
823 void *cache_data = get_cached_header(cache);
824 int hh_len = rt_dst(rt).hh->hh_len;
825 int hh_off = HH_DATA_ALIGN(rt_dst(rt).hh->hh_len) - hh_len;
831 hh_seq = read_seqbegin(&rt_dst(rt).hh->hh_lock);
832 memcpy(cache_data, (void *)rt_dst(rt).hh->hh_data + hh_off, hh_len);
833 } while (read_seqretry(&rt_dst(rt).hh->hh_lock, hh_seq));
835 cache->hh_seq = hh_seq;
837 read_lock_bh(&rt_dst(rt).hh->hh_lock);
838 memcpy(cache_data, (void *)rt_dst(rt).hh->hh_data + hh_off, hh_len);
839 read_unlock_bh(&rt_dst(rt).hh->hh_lock);
843 static struct tnl_cache *build_cache(struct vport *vport,
844 const struct tnl_mutable_config *mutable,
847 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
848 struct tnl_cache *cache;
852 if (!(mutable->port_config.flags & TNL_F_HDR_CACHE))
856 * If there is no entry in the ARP cache or if this device does not
857 * support hard header caching just fall back to the IP stack.
863 * If lock is contended fall back to directly building the header.
864 * We're not going to help performance by sitting here spinning.
866 if (!spin_trylock_bh(&tnl_vport->cache_lock))
869 cache = tnl_vport->cache;
870 if (check_cache_valid(cache, mutable))
875 cache_len = rt_dst(rt).hh->hh_len + mutable->tunnel_hlen;
877 cache = kzalloc(ALIGN(sizeof(struct tnl_cache), CACHE_DATA_ALIGN) +
878 cache_len, GFP_ATOMIC);
882 cache->len = cache_len;
884 create_eth_hdr(cache, rt);
885 cache_data = get_cached_header(cache) + rt_dst(rt).hh->hh_len;
887 create_tunnel_header(vport, mutable, rt, cache_data);
889 cache->mutable_seq = mutable->seq;
891 #ifdef NEED_CACHE_TIMEOUT
892 cache->expiration = jiffies + tnl_vport->cache_exp_interval;
895 if (is_internal_dev(rt_dst(rt).dev)) {
896 struct odp_flow_key flow_key;
897 struct tbl_node *flow_node;
903 vport = internal_dev_get_vport(rt_dst(rt).dev);
907 skb = alloc_skb(cache->len, GFP_ATOMIC);
911 __skb_put(skb, cache->len);
912 memcpy(skb->data, get_cached_header(cache), cache->len);
914 err = flow_extract(skb, vport->port_no, &flow_key, &is_frag);
920 flow_node = tbl_lookup(rcu_dereference(vport->dp->table),
921 &flow_key, flow_hash(&flow_key),
924 struct sw_flow *flow = flow_cast(flow_node);
932 assign_cache_rcu(vport, cache);
935 spin_unlock_bh(&tnl_vport->cache_lock);
940 static struct rtable *find_route(struct vport *vport,
941 const struct tnl_mutable_config *mutable,
942 u8 tos, struct tnl_cache **cache)
944 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
945 struct tnl_cache *cur_cache = rcu_dereference(tnl_vport->cache);
950 if (likely(tos == mutable->port_config.tos &&
951 check_cache_valid(cur_cache, mutable))) {
953 return cur_cache->rt;
956 struct flowi fl = { .nl_u = { .ip4_u =
957 { .daddr = mutable->port_config.daddr,
958 .saddr = mutable->port_config.saddr,
960 .proto = tnl_vport->tnl_ops->ipproto };
962 if (unlikely(ip_route_output_key(&init_net, &rt, &fl)))
965 if (likely(tos == mutable->port_config.tos))
966 *cache = build_cache(vport, mutable, rt);
972 static struct sk_buff *check_headroom(struct sk_buff *skb, int headroom)
974 if (skb_headroom(skb) < headroom || skb_header_cloned(skb)) {
975 struct sk_buff *nskb = skb_realloc_headroom(skb, headroom + 16);
976 if (unlikely(!nskb)) {
978 return ERR_PTR(-ENOMEM);
981 set_skb_csum_bits(skb, nskb);
984 skb_set_owner_w(nskb, skb->sk);
993 static inline bool need_linearize(const struct sk_buff *skb)
997 if (unlikely(skb_shinfo(skb)->frag_list))
1001 * Generally speaking we should linearize if there are paged frags.
1002 * However, if all of the refcounts are 1 we know nobody else can
1003 * change them from underneath us and we can skip the linearization.
1005 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1006 if (unlikely(page_count(skb_shinfo(skb)->frags[0].page) > 1))
1012 static struct sk_buff *handle_offloads(struct sk_buff *skb,
1013 const struct tnl_mutable_config *mutable,
1014 const struct rtable *rt)
1019 forward_ip_summed(skb);
1021 err = vswitch_skb_checksum_setup(skb);
1025 min_headroom = LL_RESERVED_SPACE(rt_dst(rt).dev) + rt_dst(rt).header_len
1026 + mutable->tunnel_hlen;
1028 if (skb_is_gso(skb)) {
1029 struct sk_buff *nskb;
1032 * If we are doing GSO on a pskb it is better to make sure that
1033 * the headroom is correct now. We will only have to copy the
1034 * portion in the linear data area and GSO will preserve
1035 * headroom when it creates the segments. This is particularly
1036 * beneficial on Xen where we get a lot of GSO pskbs.
1037 * Conversely, we avoid copying if it is just to get our own
1038 * writable clone because GSO will do the copy for us.
1040 if (skb_headroom(skb) < min_headroom) {
1041 skb = check_headroom(skb, min_headroom);
1042 if (unlikely(IS_ERR(skb))) {
1048 nskb = skb_gso_segment(skb, 0);
1050 if (unlikely(IS_ERR(nskb))) {
1051 err = PTR_ERR(nskb);
1057 skb = check_headroom(skb, min_headroom);
1058 if (unlikely(IS_ERR(skb))) {
1063 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1065 * Pages aren't locked and could change at any time.
1066 * If this happens after we compute the checksum, the
1067 * checksum will be wrong. We linearize now to avoid
1070 if (unlikely(need_linearize(skb))) {
1071 err = __skb_linearize(skb);
1076 err = skb_checksum_help(skb);
1079 } else if (skb->ip_summed == CHECKSUM_COMPLETE)
1080 skb->ip_summed = CHECKSUM_NONE;
1088 return ERR_PTR(err);
1091 static int send_frags(struct sk_buff *skb,
1092 const struct tnl_mutable_config *mutable)
1099 struct sk_buff *next = skb->next;
1100 int frag_len = skb->len - mutable->tunnel_hlen;
1103 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
1105 err = ip_local_out(skb);
1106 if (likely(net_xmit_eval(err) == 0))
1107 sent_len += frag_len;
1120 * There's no point in continuing to send fragments once one has been
1121 * dropped so just free the rest. This may help improve the congestion
1122 * that caused the first packet to be dropped.
1124 tnl_free_linked_skbs(skb);
1128 int tnl_send(struct vport *vport, struct sk_buff *skb)
1130 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1131 const struct tnl_mutable_config *mutable = rcu_dereference(tnl_vport->mutable);
1133 enum vport_err_type err = VPORT_E_TX_ERROR;
1135 struct dst_entry *unattached_dst = NULL;
1136 struct tnl_cache *cache;
1143 /* Validate the protocol headers before we try to use them. */
1144 if (skb->protocol == htons(ETH_P_8021Q)) {
1145 if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
1148 skb->protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
1149 skb_set_network_header(skb, VLAN_ETH_HLEN);
1152 if (skb->protocol == htons(ETH_P_IP)) {
1153 if (unlikely(!pskb_may_pull(skb, skb_network_offset(skb)
1154 + sizeof(struct iphdr))))
1157 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1158 else if (skb->protocol == htons(ETH_P_IPV6)) {
1159 if (unlikely(!pskb_may_pull(skb, skb_network_offset(skb)
1160 + sizeof(struct ipv6hdr))))
1166 if (skb->protocol == htons(ETH_P_IP))
1167 inner_tos = ip_hdr(skb)->tos;
1168 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1169 else if (skb->protocol == htons(ETH_P_IPV6))
1170 inner_tos = ipv6_get_dsfield(ipv6_hdr(skb));
1175 if (mutable->port_config.flags & TNL_F_TOS_INHERIT)
1178 tos = mutable->port_config.tos;
1180 tos = INET_ECN_encapsulate(tos, inner_tos);
1183 rt = find_route(vport, mutable, tos, &cache);
1186 if (unlikely(!cache))
1187 unattached_dst = &rt_dst(rt);
1195 skb = handle_offloads(skb, mutable, rt);
1196 if (unlikely(IS_ERR(skb)))
1200 if (unlikely(!check_mtu(skb, vport, mutable, rt, &frag_off))) {
1201 err = VPORT_E_TX_DROPPED;
1206 * If we are over the MTU, allow the IP stack to handle fragmentation.
1207 * Fragmentation is a slow path anyways.
1209 if (unlikely(skb->len + mutable->tunnel_hlen > dst_mtu(&rt_dst(rt)) &&
1211 unattached_dst = &rt_dst(rt);
1212 dst_hold(unattached_dst);
1217 ttl = mutable->port_config.ttl;
1219 ttl = dst_metric(&rt_dst(rt), RTAX_HOPLIMIT);
1221 if (mutable->port_config.flags & TNL_F_TTL_INHERIT) {
1222 if (skb->protocol == htons(ETH_P_IP))
1223 ttl = ip_hdr(skb)->ttl;
1224 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1225 else if (skb->protocol == htons(ETH_P_IPV6))
1226 ttl = ipv6_hdr(skb)->hop_limit;
1232 struct sk_buff *next_skb = skb->next;
1235 if (likely(cache)) {
1236 skb_push(skb, cache->len);
1237 memcpy(skb->data, get_cached_header(cache), cache->len);
1238 skb_reset_mac_header(skb);
1239 skb_set_network_header(skb, rt_dst(rt).hh->hh_len);
1242 skb_push(skb, mutable->tunnel_hlen);
1243 create_tunnel_header(vport, mutable, rt, skb->data);
1244 skb_reset_network_header(skb);
1247 skb_dst_set(skb, dst_clone(unattached_dst));
1249 skb_dst_set(skb, unattached_dst);
1250 unattached_dst = NULL;
1253 skb_set_transport_header(skb, skb_network_offset(skb) + sizeof(struct iphdr));
1258 iph->frag_off = frag_off;
1259 ip_select_ident(iph, &rt_dst(rt), NULL);
1261 skb = tnl_vport->tnl_ops->update_header(vport, mutable, &rt_dst(rt), skb);
1265 if (likely(cache)) {
1266 int orig_len = skb->len - cache->len;
1267 struct vport *cache_vport = internal_dev_get_vport(rt_dst(rt).dev);
1269 skb->protocol = htons(ETH_P_IP);
1270 iph->tot_len = htons(skb->len - skb_network_offset(skb));
1274 OVS_CB(skb)->flow = cache->flow;
1275 compute_ip_summed(skb, true);
1276 vport_receive(cache_vport, skb);
1277 sent_len += orig_len;
1281 skb->dev = rt_dst(rt).dev;
1282 err = dev_queue_xmit(skb);
1284 if (likely(net_xmit_eval(err) == 0))
1285 sent_len += orig_len;
1288 sent_len += send_frags(skb, mutable);
1294 if (unlikely(sent_len == 0))
1295 vport_record_error(vport, VPORT_E_TX_DROPPED);
1300 tnl_free_linked_skbs(skb);
1302 dst_release(unattached_dst);
1303 vport_record_error(vport, err);
1308 static int set_config(const void *config, const struct tnl_ops *tnl_ops,
1309 const struct vport *cur_vport,
1310 struct tnl_mutable_config *mutable)
1312 const struct vport *old_vport;
1313 const struct tnl_mutable_config *old_mutable;
1315 mutable->port_config = *(struct tnl_port_config *)config;
1317 if (mutable->port_config.daddr == 0)
1320 if (mutable->port_config.tos != RT_TOS(mutable->port_config.tos))
1323 mutable->tunnel_hlen = tnl_ops->hdr_len(&mutable->port_config);
1324 if (mutable->tunnel_hlen < 0)
1325 return mutable->tunnel_hlen;
1327 mutable->tunnel_hlen += sizeof(struct iphdr);
1329 mutable->tunnel_type = tnl_ops->tunnel_type;
1330 if (mutable->port_config.flags & TNL_F_IN_KEY_MATCH) {
1331 mutable->tunnel_type |= TNL_T_KEY_MATCH;
1332 mutable->port_config.in_key = 0;
1334 mutable->tunnel_type |= TNL_T_KEY_EXACT;
1336 old_vport = tnl_find_port(mutable->port_config.saddr,
1337 mutable->port_config.daddr,
1338 mutable->port_config.in_key,
1339 mutable->tunnel_type,
1342 if (old_vport && old_vport != cur_vport)
1345 if (mutable->port_config.flags & TNL_F_OUT_KEY_ACTION)
1346 mutable->port_config.out_key = 0;
1351 struct vport *tnl_create(const struct vport_parms *parms,
1352 const struct vport_ops *vport_ops,
1353 const struct tnl_ops *tnl_ops)
1355 struct vport *vport;
1356 struct tnl_vport *tnl_vport;
1357 int initial_frag_id;
1360 vport = vport_alloc(sizeof(struct tnl_vport), vport_ops, parms);
1361 if (IS_ERR(vport)) {
1362 err = PTR_ERR(vport);
1366 tnl_vport = tnl_vport_priv(vport);
1368 strcpy(tnl_vport->name, parms->name);
1369 tnl_vport->tnl_ops = tnl_ops;
1371 tnl_vport->mutable = kzalloc(sizeof(struct tnl_mutable_config), GFP_KERNEL);
1372 if (!tnl_vport->mutable) {
1374 goto error_free_vport;
1377 vport_gen_rand_ether_addr(tnl_vport->mutable->eth_addr);
1378 tnl_vport->mutable->mtu = ETH_DATA_LEN;
1380 get_random_bytes(&initial_frag_id, sizeof(int));
1381 atomic_set(&tnl_vport->frag_id, initial_frag_id);
1383 err = set_config(parms->config, tnl_ops, NULL, tnl_vport->mutable);
1385 goto error_free_mutable;
1387 spin_lock_init(&tnl_vport->cache_lock);
1389 #ifdef NEED_CACHE_TIMEOUT
1390 tnl_vport->cache_exp_interval = MAX_CACHE_EXP -
1391 (net_random() % (MAX_CACHE_EXP / 2));
1394 err = add_port(vport);
1396 goto error_free_mutable;
1401 kfree(tnl_vport->mutable);
1405 return ERR_PTR(err);
1408 int tnl_modify(struct vport *vport, struct odp_port *port)
1410 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1411 struct tnl_mutable_config *mutable;
1414 mutable = kmemdup(tnl_vport->mutable, sizeof(struct tnl_mutable_config), GFP_KERNEL);
1420 err = set_config(port->config, tnl_vport->tnl_ops, vport, mutable);
1426 err = move_port(vport, mutable);
1438 static void free_port_rcu(struct rcu_head *rcu)
1440 struct tnl_vport *tnl_vport = container_of(rcu, struct tnl_vport, rcu);
1442 spin_lock_bh(&tnl_vport->cache_lock);
1443 free_cache(tnl_vport->cache);
1444 spin_unlock_bh(&tnl_vport->cache_lock);
1446 kfree(tnl_vport->mutable);
1447 vport_free(tnl_vport_to_vport(tnl_vport));
1450 int tnl_destroy(struct vport *vport)
1452 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1453 const struct tnl_mutable_config *old_mutable;
1455 if (vport == tnl_find_port(tnl_vport->mutable->port_config.saddr,
1456 tnl_vport->mutable->port_config.daddr,
1457 tnl_vport->mutable->port_config.in_key,
1458 tnl_vport->mutable->tunnel_type,
1462 call_rcu(&tnl_vport->rcu, free_port_rcu);
1467 int tnl_set_mtu(struct vport *vport, int mtu)
1469 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1470 struct tnl_mutable_config *mutable;
1472 mutable = kmemdup(tnl_vport->mutable, sizeof(struct tnl_mutable_config), GFP_KERNEL);
1477 assign_config_rcu(vport, mutable);
1482 int tnl_set_addr(struct vport *vport, const unsigned char *addr)
1484 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1485 struct tnl_mutable_config *mutable;
1487 mutable = kmemdup(tnl_vport->mutable, sizeof(struct tnl_mutable_config), GFP_KERNEL);
1491 memcpy(mutable->eth_addr, addr, ETH_ALEN);
1492 assign_config_rcu(vport, mutable);
1497 const char *tnl_get_name(const struct vport *vport)
1499 const struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1500 return tnl_vport->name;
1503 const unsigned char *tnl_get_addr(const struct vport *vport)
1505 const struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1506 return rcu_dereference(tnl_vport->mutable)->eth_addr;
1509 int tnl_get_mtu(const struct vport *vport)
1511 const struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1512 return rcu_dereference(tnl_vport->mutable)->mtu;
1515 void tnl_free_linked_skbs(struct sk_buff *skb)
1521 struct sk_buff *next = skb->next;