2 * Copyright (c) 2010, 2011 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>
38 #include "vport-generic.h"
39 #include "vport-internal_dev.h"
41 #ifdef NEED_CACHE_TIMEOUT
43 * On kernels where we can't quickly detect changes in the rest of the system
44 * we use an expiration time to invalidate the cache. A shorter expiration
45 * reduces the length of time that we may potentially blackhole packets while
46 * a longer time increases performance by reducing the frequency that the
47 * cache needs to be rebuilt. A variety of factors may cause the cache to be
48 * invalidated before the expiration time but this is the maximum. The time
49 * is expressed in jiffies.
51 #define MAX_CACHE_EXP HZ
55 * Interval to check for and remove caches that are no longer valid. Caches
56 * are checked for validity before they are used for packet encapsulation and
57 * old caches are removed at that time. However, if no packets are sent through
58 * the tunnel then the cache will never be destroyed. Since it holds
59 * references to a number of system objects, the cache will continue to use
60 * system resources by not allowing those objects to be destroyed. The cache
61 * cleaner is periodically run to free invalid caches. It does not
62 * significantly affect system performance. A lower interval will release
63 * resources faster but will itself consume resources by requiring more frequent
64 * checks. A longer interval may result in messages being printed to the kernel
65 * message buffer about unreleased resources. The interval is expressed in
68 #define CACHE_CLEANER_INTERVAL (5 * HZ)
70 #define CACHE_DATA_ALIGN 16
72 static struct tbl __rcu *port_table __read_mostly;
74 static void cache_cleaner(struct work_struct *work);
75 static 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 /* This is analogous to rtnl_dereference for the tunnel cache. It checks that
104 * cache_lock is held, so it is only for update side code.
106 static inline struct tnl_cache *cache_dereference(struct tnl_vport *tnl_vport)
108 return rcu_dereference_protected(tnl_vport->cache,
109 lockdep_is_held(&tnl_vport->cache_lock));
112 static inline void schedule_cache_cleaner(void)
114 schedule_delayed_work(&cache_cleaner_wq, CACHE_CLEANER_INTERVAL);
117 static void free_cache(struct tnl_cache *cache)
122 flow_put(cache->flow);
123 ip_rt_put(cache->rt);
127 static void free_config_rcu(struct rcu_head *rcu)
129 struct tnl_mutable_config *c = container_of(rcu, struct tnl_mutable_config, rcu);
133 static void free_cache_rcu(struct rcu_head *rcu)
135 struct tnl_cache *c = container_of(rcu, struct tnl_cache, rcu);
139 static void assign_config_rcu(struct vport *vport,
140 struct tnl_mutable_config *new_config)
142 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
143 struct tnl_mutable_config *old_config;
145 old_config = rtnl_dereference(tnl_vport->mutable);
146 rcu_assign_pointer(tnl_vport->mutable, new_config);
147 call_rcu(&old_config->rcu, free_config_rcu);
150 static void assign_cache_rcu(struct vport *vport, struct tnl_cache *new_cache)
152 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
153 struct tnl_cache *old_cache;
155 old_cache = cache_dereference(tnl_vport);
156 rcu_assign_pointer(tnl_vport->cache, new_cache);
159 call_rcu(&old_cache->rcu, free_cache_rcu);
162 static unsigned int *find_port_pool(const struct tnl_mutable_config *mutable)
164 if (mutable->flags & TNL_F_IN_KEY_MATCH) {
166 return &local_remote_ports;
168 return &remote_ports;
171 return &key_local_remote_ports;
173 return &key_remote_ports;
177 struct port_lookup_key {
178 const struct tnl_mutable_config *mutable;
186 * Modifies 'target' to store the rcu_dereferenced pointer that was used to do
189 static int port_cmp(const struct tbl_node *node, void *target)
191 const struct tnl_vport *tnl_vport = tnl_vport_table_cast(node);
192 struct port_lookup_key *lookup = target;
194 lookup->mutable = rcu_dereference_rtnl(tnl_vport->mutable);
196 return (lookup->mutable->tunnel_type == lookup->tunnel_type &&
197 lookup->mutable->daddr == lookup->daddr &&
198 lookup->mutable->in_key == lookup->key &&
199 lookup->mutable->saddr == lookup->saddr);
202 static u32 port_hash(struct port_lookup_key *k)
204 u32 x = jhash_3words((__force u32)k->saddr, (__force u32)k->daddr,
206 return jhash_2words((__force u64)k->key >> 32, (__force u32)k->key, x);
209 static u32 mutable_hash(const struct tnl_mutable_config *mutable)
211 struct port_lookup_key lookup;
213 lookup.saddr = mutable->saddr;
214 lookup.daddr = mutable->daddr;
215 lookup.key = mutable->in_key;
216 lookup.tunnel_type = mutable->tunnel_type;
218 return port_hash(&lookup);
221 static void check_table_empty(void)
223 struct tbl *old_table = rtnl_dereference(port_table);
225 if (tbl_count(old_table) == 0) {
226 cancel_delayed_work_sync(&cache_cleaner_wq);
227 rcu_assign_pointer(port_table, NULL);
228 tbl_deferred_destroy(old_table, NULL);
232 static int add_port(struct vport *vport)
234 struct tbl *cur_table = rtnl_dereference(port_table);
235 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
239 struct tbl *new_table;
241 new_table = tbl_create(TBL_MIN_BUCKETS);
245 rcu_assign_pointer(port_table, new_table);
246 schedule_cache_cleaner();
248 } else if (tbl_count(cur_table) > tbl_n_buckets(cur_table)) {
249 struct tbl *new_table;
251 new_table = tbl_expand(cur_table);
252 if (IS_ERR(new_table))
253 return PTR_ERR(new_table);
255 rcu_assign_pointer(port_table, new_table);
256 tbl_deferred_destroy(cur_table, NULL);
259 err = tbl_insert(rtnl_dereference(port_table), &tnl_vport->tbl_node,
260 mutable_hash(rtnl_dereference(tnl_vport->mutable)));
266 (*find_port_pool(rtnl_dereference(tnl_vport->mutable)))++;
271 static int move_port(struct vport *vport, struct tnl_mutable_config *new_mutable)
274 struct tbl *cur_table = rtnl_dereference(port_table);
275 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
278 hash = mutable_hash(new_mutable);
279 if (hash == tnl_vport->tbl_node.hash)
283 * Ideally we should make this move atomic to avoid having gaps in
284 * finding tunnels or the possibility of failure. However, if we do
285 * find a tunnel it will always be consistent.
287 err = tbl_remove(cur_table, &tnl_vport->tbl_node);
291 err = tbl_insert(cur_table, &tnl_vport->tbl_node, hash);
293 (*find_port_pool(rtnl_dereference(tnl_vport->mutable)))--;
299 (*find_port_pool(rtnl_dereference(tnl_vport->mutable)))--;
300 assign_config_rcu(vport, new_mutable);
301 (*find_port_pool(rtnl_dereference(tnl_vport->mutable)))++;
306 static int del_port(struct vport *vport)
308 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
311 err = tbl_remove(rtnl_dereference(port_table), &tnl_vport->tbl_node);
316 (*find_port_pool(rtnl_dereference(tnl_vport->mutable)))--;
321 struct vport *tnl_find_port(__be32 saddr, __be32 daddr, __be64 key,
323 const struct tnl_mutable_config **mutable)
325 struct port_lookup_key lookup;
326 struct tbl *table = rcu_dereference_rtnl(port_table);
327 struct tbl_node *tbl_node;
329 if (unlikely(!table))
332 lookup.saddr = saddr;
333 lookup.daddr = daddr;
335 if (tunnel_type & TNL_T_KEY_EXACT) {
337 lookup.tunnel_type = tunnel_type & ~TNL_T_KEY_MATCH;
339 if (key_local_remote_ports) {
340 tbl_node = tbl_lookup(table, &lookup, port_hash(&lookup), port_cmp);
345 if (key_remote_ports) {
348 tbl_node = tbl_lookup(table, &lookup, port_hash(&lookup), port_cmp);
352 lookup.saddr = saddr;
356 if (tunnel_type & TNL_T_KEY_MATCH) {
358 lookup.tunnel_type = tunnel_type & ~TNL_T_KEY_EXACT;
360 if (local_remote_ports) {
361 tbl_node = tbl_lookup(table, &lookup, port_hash(&lookup), port_cmp);
369 tbl_node = tbl_lookup(table, &lookup, port_hash(&lookup), port_cmp);
378 *mutable = lookup.mutable;
379 return tnl_vport_to_vport(tnl_vport_table_cast(tbl_node));
382 static inline void ecn_decapsulate(struct sk_buff *skb)
384 /* This is accessing the outer IP header of the tunnel, which we've
385 * already validated to be OK. skb->data is currently set to the start
386 * of the inner Ethernet header, and we've validated ETH_HLEN.
388 if (unlikely(INET_ECN_is_ce(ip_hdr(skb)->tos))) {
389 __be16 protocol = skb->protocol;
391 skb_set_network_header(skb, ETH_HLEN);
393 if (skb->protocol == htons(ETH_P_8021Q)) {
394 if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
397 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
398 skb_set_network_header(skb, VLAN_ETH_HLEN);
401 if (protocol == htons(ETH_P_IP)) {
402 if (unlikely(!pskb_may_pull(skb, skb_network_offset(skb)
403 + sizeof(struct iphdr))))
406 IP_ECN_set_ce(ip_hdr(skb));
408 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
409 else if (protocol == htons(ETH_P_IPV6)) {
410 if (unlikely(!pskb_may_pull(skb, skb_network_offset(skb)
411 + sizeof(struct ipv6hdr))))
414 IP6_ECN_set_ce(ipv6_hdr(skb));
420 /* Called with rcu_read_lock. */
421 void tnl_rcv(struct vport *vport, struct sk_buff *skb)
423 /* Packets received by this function are in the following state:
424 * - skb->data points to the inner Ethernet header.
425 * - The inner Ethernet header is in the linear data area.
426 * - skb->csum does not include the inner Ethernet header.
427 * - The layer pointers point at the outer headers.
430 struct ethhdr *eh = (struct ethhdr *)skb->data;
432 if (likely(ntohs(eh->h_proto) >= 1536))
433 skb->protocol = eh->h_proto;
435 skb->protocol = htons(ETH_P_802_2);
439 skb_clear_rxhash(skb);
442 ecn_decapsulate(skb);
443 compute_ip_summed(skb, false);
444 vlan_set_tci(skb, 0);
446 vport_receive(vport, skb);
449 static bool check_ipv4_address(__be32 addr)
451 if (ipv4_is_multicast(addr) || ipv4_is_lbcast(addr)
452 || ipv4_is_loopback(addr) || ipv4_is_zeronet(addr))
458 static bool ipv4_should_icmp(struct sk_buff *skb)
460 struct iphdr *old_iph = ip_hdr(skb);
462 /* Don't respond to L2 broadcast. */
463 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
466 /* Don't respond to L3 broadcast or invalid addresses. */
467 if (!check_ipv4_address(old_iph->daddr) ||
468 !check_ipv4_address(old_iph->saddr))
471 /* Only respond to the first fragment. */
472 if (old_iph->frag_off & htons(IP_OFFSET))
475 /* Don't respond to ICMP error messages. */
476 if (old_iph->protocol == IPPROTO_ICMP) {
477 u8 icmp_type, *icmp_typep;
479 icmp_typep = skb_header_pointer(skb, (u8 *)old_iph +
480 (old_iph->ihl << 2) +
481 offsetof(struct icmphdr, type) -
482 skb->data, sizeof(icmp_type),
488 if (*icmp_typep > NR_ICMP_TYPES
489 || (*icmp_typep <= ICMP_PARAMETERPROB
490 && *icmp_typep != ICMP_ECHOREPLY
491 && *icmp_typep != ICMP_ECHO))
498 static void ipv4_build_icmp(struct sk_buff *skb, struct sk_buff *nskb,
499 unsigned int mtu, unsigned int payload_length)
501 struct iphdr *iph, *old_iph = ip_hdr(skb);
502 struct icmphdr *icmph;
505 iph = (struct iphdr *)skb_put(nskb, sizeof(struct iphdr));
506 icmph = (struct icmphdr *)skb_put(nskb, sizeof(struct icmphdr));
507 payload = skb_put(nskb, payload_length);
511 iph->ihl = sizeof(struct iphdr) >> 2;
512 iph->tos = (old_iph->tos & IPTOS_TOS_MASK) |
513 IPTOS_PREC_INTERNETCONTROL;
514 iph->tot_len = htons(sizeof(struct iphdr)
515 + sizeof(struct icmphdr)
517 get_random_bytes(&iph->id, sizeof(iph->id));
520 iph->protocol = IPPROTO_ICMP;
521 iph->daddr = old_iph->saddr;
522 iph->saddr = old_iph->daddr;
527 icmph->type = ICMP_DEST_UNREACH;
528 icmph->code = ICMP_FRAG_NEEDED;
529 icmph->un.gateway = htonl(mtu);
532 nskb->csum = csum_partial((u8 *)icmph, sizeof(struct icmphdr), 0);
533 nskb->csum = skb_copy_and_csum_bits(skb, (u8 *)old_iph - skb->data,
534 payload, payload_length,
536 icmph->checksum = csum_fold(nskb->csum);
539 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
540 static bool ipv6_should_icmp(struct sk_buff *skb)
542 struct ipv6hdr *old_ipv6h = ipv6_hdr(skb);
544 int payload_off = (u8 *)(old_ipv6h + 1) - skb->data;
545 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
547 /* Check source address is valid. */
548 addr_type = ipv6_addr_type(&old_ipv6h->saddr);
549 if (addr_type & IPV6_ADDR_MULTICAST || addr_type == IPV6_ADDR_ANY)
552 /* Don't reply to unspecified addresses. */
553 if (ipv6_addr_type(&old_ipv6h->daddr) == IPV6_ADDR_ANY)
556 /* Don't respond to ICMP error messages. */
557 payload_off = ipv6_skip_exthdr(skb, payload_off, &nexthdr);
561 if (nexthdr == NEXTHDR_ICMP) {
562 u8 icmp_type, *icmp_typep;
564 icmp_typep = skb_header_pointer(skb, payload_off +
565 offsetof(struct icmp6hdr,
567 sizeof(icmp_type), &icmp_type);
569 if (!icmp_typep || !(*icmp_typep & ICMPV6_INFOMSG_MASK))
576 static void ipv6_build_icmp(struct sk_buff *skb, struct sk_buff *nskb,
577 unsigned int mtu, unsigned int payload_length)
579 struct ipv6hdr *ipv6h, *old_ipv6h = ipv6_hdr(skb);
580 struct icmp6hdr *icmp6h;
583 ipv6h = (struct ipv6hdr *)skb_put(nskb, sizeof(struct ipv6hdr));
584 icmp6h = (struct icmp6hdr *)skb_put(nskb, sizeof(struct icmp6hdr));
585 payload = skb_put(nskb, payload_length);
590 memset(&ipv6h->flow_lbl, 0, sizeof(ipv6h->flow_lbl));
591 ipv6h->payload_len = htons(sizeof(struct icmp6hdr)
593 ipv6h->nexthdr = NEXTHDR_ICMP;
594 ipv6h->hop_limit = IPV6_DEFAULT_HOPLIMIT;
595 ipv6_addr_copy(&ipv6h->daddr, &old_ipv6h->saddr);
596 ipv6_addr_copy(&ipv6h->saddr, &old_ipv6h->daddr);
599 icmp6h->icmp6_type = ICMPV6_PKT_TOOBIG;
600 icmp6h->icmp6_code = 0;
601 icmp6h->icmp6_cksum = 0;
602 icmp6h->icmp6_mtu = htonl(mtu);
604 nskb->csum = csum_partial((u8 *)icmp6h, sizeof(struct icmp6hdr), 0);
605 nskb->csum = skb_copy_and_csum_bits(skb, (u8 *)old_ipv6h - skb->data,
606 payload, payload_length,
608 icmp6h->icmp6_cksum = csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
609 sizeof(struct icmp6hdr)
611 ipv6h->nexthdr, nskb->csum);
615 bool tnl_frag_needed(struct vport *vport, const struct tnl_mutable_config *mutable,
616 struct sk_buff *skb, unsigned int mtu, __be64 flow_key)
618 unsigned int eth_hdr_len = ETH_HLEN;
619 unsigned int total_length = 0, header_length = 0, payload_length;
620 struct ethhdr *eh, *old_eh = eth_hdr(skb);
621 struct sk_buff *nskb;
624 if (skb->protocol == htons(ETH_P_IP)) {
625 if (mtu < IP_MIN_MTU)
628 if (!ipv4_should_icmp(skb))
631 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
632 else if (skb->protocol == htons(ETH_P_IPV6)) {
633 if (mtu < IPV6_MIN_MTU)
637 * In theory we should do PMTUD on IPv6 multicast messages but
638 * we don't have an address to send from so just fragment.
640 if (ipv6_addr_type(&ipv6_hdr(skb)->daddr) & IPV6_ADDR_MULTICAST)
643 if (!ipv6_should_icmp(skb))
651 if (old_eh->h_proto == htons(ETH_P_8021Q))
652 eth_hdr_len = VLAN_ETH_HLEN;
654 payload_length = skb->len - eth_hdr_len;
655 if (skb->protocol == htons(ETH_P_IP)) {
656 header_length = sizeof(struct iphdr) + sizeof(struct icmphdr);
657 total_length = min_t(unsigned int, header_length +
658 payload_length, 576);
660 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
662 header_length = sizeof(struct ipv6hdr) +
663 sizeof(struct icmp6hdr);
664 total_length = min_t(unsigned int, header_length +
665 payload_length, IPV6_MIN_MTU);
669 payload_length = total_length - header_length;
671 nskb = dev_alloc_skb(NET_IP_ALIGN + eth_hdr_len + header_length +
676 skb_reserve(nskb, NET_IP_ALIGN);
678 /* Ethernet / VLAN */
679 eh = (struct ethhdr *)skb_put(nskb, eth_hdr_len);
680 memcpy(eh->h_dest, old_eh->h_source, ETH_ALEN);
681 memcpy(eh->h_source, mutable->eth_addr, ETH_ALEN);
682 nskb->protocol = eh->h_proto = old_eh->h_proto;
683 if (old_eh->h_proto == htons(ETH_P_8021Q)) {
684 struct vlan_ethhdr *vh = (struct vlan_ethhdr *)eh;
686 vh->h_vlan_TCI = vlan_eth_hdr(skb)->h_vlan_TCI;
687 vh->h_vlan_encapsulated_proto = skb->protocol;
689 vlan_set_tci(nskb, vlan_get_tci(skb));
690 skb_reset_mac_header(nskb);
693 if (skb->protocol == htons(ETH_P_IP))
694 ipv4_build_icmp(skb, nskb, mtu, payload_length);
695 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
697 ipv6_build_icmp(skb, nskb, mtu, payload_length);
701 * Assume that flow based keys are symmetric with respect to input
702 * and output and use the key that we were going to put on the
703 * outgoing packet for the fake received packet. If the keys are
704 * not symmetric then PMTUD needs to be disabled since we won't have
705 * any way of synthesizing packets.
707 if ((mutable->flags & (TNL_F_IN_KEY_MATCH | TNL_F_OUT_KEY_ACTION)) ==
708 (TNL_F_IN_KEY_MATCH | TNL_F_OUT_KEY_ACTION))
709 OVS_CB(nskb)->tun_id = flow_key;
711 compute_ip_summed(nskb, false);
712 vport_receive(vport, nskb);
717 static bool check_mtu(struct sk_buff *skb,
719 const struct tnl_mutable_config *mutable,
720 const struct rtable *rt, __be16 *frag_offp)
722 bool pmtud = mutable->flags & TNL_F_PMTUD;
725 unsigned int packet_length = skb->len - ETH_HLEN;
727 /* Allow for one level of tagging in the packet length. */
728 if (!vlan_tx_tag_present(skb) &&
729 eth_hdr(skb)->h_proto == htons(ETH_P_8021Q))
730 packet_length -= VLAN_HLEN;
735 frag_off = htons(IP_DF);
737 /* The tag needs to go in packet regardless of where it
738 * currently is, so subtract it from the MTU.
740 if (vlan_tx_tag_present(skb) ||
741 eth_hdr(skb)->h_proto == htons(ETH_P_8021Q))
742 vlan_header = VLAN_HLEN;
744 mtu = dst_mtu(&rt_dst(rt))
746 - mutable->tunnel_hlen
750 if (skb->protocol == htons(ETH_P_IP)) {
751 struct iphdr *iph = ip_hdr(skb);
753 frag_off |= iph->frag_off & htons(IP_DF);
755 if (pmtud && iph->frag_off & htons(IP_DF)) {
756 mtu = max(mtu, IP_MIN_MTU);
758 if (packet_length > mtu &&
759 tnl_frag_needed(vport, mutable, skb, mtu,
760 OVS_CB(skb)->tun_id))
764 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
765 else if (skb->protocol == htons(ETH_P_IPV6)) {
766 /* IPv6 requires PMTUD if the packet is above the minimum MTU. */
767 if (packet_length > IPV6_MIN_MTU)
768 frag_off = htons(IP_DF);
771 mtu = max(mtu, IPV6_MIN_MTU);
773 if (packet_length > mtu &&
774 tnl_frag_needed(vport, mutable, skb, mtu,
775 OVS_CB(skb)->tun_id))
781 *frag_offp = frag_off;
785 static void create_tunnel_header(const struct vport *vport,
786 const struct tnl_mutable_config *mutable,
787 const struct rtable *rt, void *header)
789 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
790 struct iphdr *iph = header;
793 iph->ihl = sizeof(struct iphdr) >> 2;
794 iph->frag_off = htons(IP_DF);
795 iph->protocol = tnl_vport->tnl_ops->ipproto;
796 iph->tos = mutable->tos;
797 iph->daddr = rt->rt_dst;
798 iph->saddr = rt->rt_src;
799 iph->ttl = mutable->ttl;
801 iph->ttl = dst_metric(&rt_dst(rt), RTAX_HOPLIMIT);
803 tnl_vport->tnl_ops->build_header(vport, mutable, iph + 1);
806 static inline void *get_cached_header(const struct tnl_cache *cache)
808 return (void *)cache + ALIGN(sizeof(struct tnl_cache), CACHE_DATA_ALIGN);
811 static inline bool check_cache_valid(const struct tnl_cache *cache,
812 const struct tnl_mutable_config *mutable)
815 #ifdef NEED_CACHE_TIMEOUT
816 time_before(jiffies, cache->expiration) &&
819 atomic_read(&init_net.ipv4.rt_genid) == cache->rt->rt_genid &&
822 rt_dst(cache->rt).hh->hh_lock.sequence == cache->hh_seq &&
824 mutable->seq == cache->mutable_seq &&
825 (!is_internal_dev(rt_dst(cache->rt).dev) ||
826 (cache->flow && !cache->flow->dead));
829 static int cache_cleaner_cb(struct tbl_node *tbl_node, void *aux)
831 struct tnl_vport *tnl_vport = tnl_vport_table_cast(tbl_node);
832 const struct tnl_mutable_config *mutable = rcu_dereference(tnl_vport->mutable);
833 const struct tnl_cache *cache = rcu_dereference(tnl_vport->cache);
835 if (cache && !check_cache_valid(cache, mutable) &&
836 spin_trylock_bh(&tnl_vport->cache_lock)) {
837 assign_cache_rcu(tnl_vport_to_vport(tnl_vport), NULL);
838 spin_unlock_bh(&tnl_vport->cache_lock);
844 static void cache_cleaner(struct work_struct *work)
846 schedule_cache_cleaner();
849 tbl_foreach(rcu_dereference(port_table), cache_cleaner_cb, NULL);
853 static inline void create_eth_hdr(struct tnl_cache *cache,
854 const struct rtable *rt)
856 void *cache_data = get_cached_header(cache);
857 int hh_len = rt_dst(rt).hh->hh_len;
858 int hh_off = HH_DATA_ALIGN(rt_dst(rt).hh->hh_len) - hh_len;
864 hh_seq = read_seqbegin(&rt_dst(rt).hh->hh_lock);
865 memcpy(cache_data, (void *)rt_dst(rt).hh->hh_data + hh_off, hh_len);
866 } while (read_seqretry(&rt_dst(rt).hh->hh_lock, hh_seq));
868 cache->hh_seq = hh_seq;
870 read_lock_bh(&rt_dst(rt).hh->hh_lock);
871 memcpy(cache_data, (void *)rt_dst(rt).hh->hh_data + hh_off, hh_len);
872 read_unlock_bh(&rt_dst(rt).hh->hh_lock);
876 static struct tnl_cache *build_cache(struct vport *vport,
877 const struct tnl_mutable_config *mutable,
880 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
881 struct tnl_cache *cache;
885 if (!(mutable->flags & TNL_F_HDR_CACHE))
889 * If there is no entry in the ARP cache or if this device does not
890 * support hard header caching just fall back to the IP stack.
896 * If lock is contended fall back to directly building the header.
897 * We're not going to help performance by sitting here spinning.
899 if (!spin_trylock_bh(&tnl_vport->cache_lock))
902 cache = cache_dereference(tnl_vport);
903 if (check_cache_valid(cache, mutable))
908 cache_len = rt_dst(rt).hh->hh_len + mutable->tunnel_hlen;
910 cache = kzalloc(ALIGN(sizeof(struct tnl_cache), CACHE_DATA_ALIGN) +
911 cache_len, GFP_ATOMIC);
915 cache->len = cache_len;
917 create_eth_hdr(cache, rt);
918 cache_data = get_cached_header(cache) + rt_dst(rt).hh->hh_len;
920 create_tunnel_header(vport, mutable, rt, cache_data);
922 cache->mutable_seq = mutable->seq;
924 #ifdef NEED_CACHE_TIMEOUT
925 cache->expiration = jiffies + tnl_vport->cache_exp_interval;
928 if (is_internal_dev(rt_dst(rt).dev)) {
929 struct sw_flow_key flow_key;
930 struct tbl_node *flow_node;
931 struct vport *dst_vport;
936 dst_vport = internal_dev_get_vport(rt_dst(rt).dev);
940 skb = alloc_skb(cache->len, GFP_ATOMIC);
944 __skb_put(skb, cache->len);
945 memcpy(skb->data, get_cached_header(cache), cache->len);
947 err = flow_extract(skb, dst_vport->port_no, &flow_key, &is_frag);
953 flow_node = tbl_lookup(rcu_dereference(dst_vport->dp->table),
954 &flow_key, flow_hash(&flow_key),
957 struct sw_flow *flow = flow_cast(flow_node);
965 assign_cache_rcu(vport, cache);
968 spin_unlock_bh(&tnl_vport->cache_lock);
973 static struct rtable *find_route(struct vport *vport,
974 const struct tnl_mutable_config *mutable,
975 u8 tos, struct tnl_cache **cache)
977 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
978 struct tnl_cache *cur_cache = rcu_dereference(tnl_vport->cache);
983 if (likely(tos == mutable->tos && check_cache_valid(cur_cache, mutable))) {
985 return cur_cache->rt;
988 struct flowi fl = { .nl_u = { .ip4_u =
989 { .daddr = mutable->daddr,
990 .saddr = mutable->saddr,
992 .proto = tnl_vport->tnl_ops->ipproto };
994 if (unlikely(ip_route_output_key(&init_net, &rt, &fl)))
997 if (likely(tos == mutable->tos))
998 *cache = build_cache(vport, mutable, rt);
1004 static struct sk_buff *check_headroom(struct sk_buff *skb, int headroom)
1006 if (skb_headroom(skb) < headroom || skb_header_cloned(skb)) {
1007 struct sk_buff *nskb = skb_realloc_headroom(skb, headroom + 16);
1008 if (unlikely(!nskb)) {
1010 return ERR_PTR(-ENOMEM);
1013 set_skb_csum_bits(skb, nskb);
1016 skb_set_owner_w(nskb, skb->sk);
1025 static inline bool need_linearize(const struct sk_buff *skb)
1029 if (unlikely(skb_shinfo(skb)->frag_list))
1033 * Generally speaking we should linearize if there are paged frags.
1034 * However, if all of the refcounts are 1 we know nobody else can
1035 * change them from underneath us and we can skip the linearization.
1037 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1038 if (unlikely(page_count(skb_shinfo(skb)->frags[i].page) > 1))
1044 static struct sk_buff *handle_offloads(struct sk_buff *skb,
1045 const struct tnl_mutable_config *mutable,
1046 const struct rtable *rt)
1051 forward_ip_summed(skb);
1053 err = vswitch_skb_checksum_setup(skb);
1057 min_headroom = LL_RESERVED_SPACE(rt_dst(rt).dev) + rt_dst(rt).header_len
1058 + mutable->tunnel_hlen
1059 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
1061 skb = check_headroom(skb, min_headroom);
1067 if (skb_is_gso(skb)) {
1068 struct sk_buff *nskb;
1070 nskb = skb_gso_segment(skb, 0);
1073 err = PTR_ERR(nskb);
1078 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1079 /* Pages aren't locked and could change at any time.
1080 * If this happens after we compute the checksum, the
1081 * checksum will be wrong. We linearize now to avoid
1084 if (unlikely(need_linearize(skb))) {
1085 err = __skb_linearize(skb);
1090 err = skb_checksum_help(skb);
1093 } else if (skb->ip_summed == CHECKSUM_COMPLETE)
1094 skb->ip_summed = CHECKSUM_NONE;
1101 return ERR_PTR(err);
1104 static int send_frags(struct sk_buff *skb,
1105 const struct tnl_mutable_config *mutable)
1112 struct sk_buff *next = skb->next;
1113 int frag_len = skb->len - mutable->tunnel_hlen;
1116 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
1118 err = ip_local_out(skb);
1119 if (likely(net_xmit_eval(err) == 0))
1120 sent_len += frag_len;
1133 * There's no point in continuing to send fragments once one has been
1134 * dropped so just free the rest. This may help improve the congestion
1135 * that caused the first packet to be dropped.
1137 tnl_free_linked_skbs(skb);
1141 int tnl_send(struct vport *vport, struct sk_buff *skb)
1143 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1144 const struct tnl_mutable_config *mutable = rcu_dereference(tnl_vport->mutable);
1146 enum vport_err_type err = VPORT_E_TX_ERROR;
1148 struct dst_entry *unattached_dst = NULL;
1149 struct tnl_cache *cache;
1151 __be16 frag_off = 0;
1156 /* Validate the protocol headers before we try to use them. */
1157 if (skb->protocol == htons(ETH_P_8021Q) &&
1158 !vlan_tx_tag_present(skb)) {
1159 if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
1162 skb->protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
1163 skb_set_network_header(skb, VLAN_ETH_HLEN);
1166 if (skb->protocol == htons(ETH_P_IP)) {
1167 if (unlikely(!pskb_may_pull(skb, skb_network_offset(skb)
1168 + sizeof(struct iphdr))))
1171 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1172 else if (skb->protocol == htons(ETH_P_IPV6)) {
1173 if (unlikely(!pskb_may_pull(skb, skb_network_offset(skb)
1174 + sizeof(struct ipv6hdr))))
1180 if (skb->protocol == htons(ETH_P_IP))
1181 inner_tos = ip_hdr(skb)->tos;
1182 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1183 else if (skb->protocol == htons(ETH_P_IPV6))
1184 inner_tos = ipv6_get_dsfield(ipv6_hdr(skb));
1189 if (mutable->flags & TNL_F_TOS_INHERIT)
1194 tos = INET_ECN_encapsulate(tos, inner_tos);
1197 rt = find_route(vport, mutable, tos, &cache);
1200 if (unlikely(!cache))
1201 unattached_dst = &rt_dst(rt);
1207 skb_clear_rxhash(skb);
1210 skb = handle_offloads(skb, mutable, rt);
1215 if (unlikely(!check_mtu(skb, vport, mutable, rt, &frag_off))) {
1216 err = VPORT_E_TX_DROPPED;
1221 * If we are over the MTU, allow the IP stack to handle fragmentation.
1222 * Fragmentation is a slow path anyways.
1224 if (unlikely(skb->len + mutable->tunnel_hlen > dst_mtu(&rt_dst(rt)) &&
1226 unattached_dst = &rt_dst(rt);
1227 dst_hold(unattached_dst);
1234 ttl = dst_metric(&rt_dst(rt), RTAX_HOPLIMIT);
1236 if (mutable->flags & TNL_F_TTL_INHERIT) {
1237 if (skb->protocol == htons(ETH_P_IP))
1238 ttl = ip_hdr(skb)->ttl;
1239 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1240 else if (skb->protocol == htons(ETH_P_IPV6))
1241 ttl = ipv6_hdr(skb)->hop_limit;
1247 struct sk_buff *next_skb = skb->next;
1250 if (unlikely(vlan_deaccel_tag(skb)))
1253 if (likely(cache)) {
1254 skb_push(skb, cache->len);
1255 memcpy(skb->data, get_cached_header(cache), cache->len);
1256 skb_reset_mac_header(skb);
1257 skb_set_network_header(skb, rt_dst(rt).hh->hh_len);
1260 skb_push(skb, mutable->tunnel_hlen);
1261 create_tunnel_header(vport, mutable, rt, skb->data);
1262 skb_reset_network_header(skb);
1265 skb_dst_set(skb, dst_clone(unattached_dst));
1267 skb_dst_set(skb, unattached_dst);
1268 unattached_dst = NULL;
1271 skb_set_transport_header(skb, skb_network_offset(skb) + sizeof(struct iphdr));
1276 iph->frag_off = frag_off;
1277 ip_select_ident(iph, &rt_dst(rt), NULL);
1279 skb = tnl_vport->tnl_ops->update_header(vport, mutable, &rt_dst(rt), skb);
1283 if (likely(cache)) {
1284 int orig_len = skb->len - cache->len;
1285 struct vport *cache_vport = internal_dev_get_vport(rt_dst(rt).dev);
1287 skb->protocol = htons(ETH_P_IP);
1289 iph->tot_len = htons(skb->len - skb_network_offset(skb));
1293 OVS_CB(skb)->flow = cache->flow;
1294 compute_ip_summed(skb, true);
1295 vport_receive(cache_vport, skb);
1296 sent_len += orig_len;
1300 skb->dev = rt_dst(rt).dev;
1301 xmit_err = dev_queue_xmit(skb);
1303 if (likely(net_xmit_eval(xmit_err) == 0))
1304 sent_len += orig_len;
1307 sent_len += send_frags(skb, mutable);
1313 if (unlikely(sent_len == 0))
1314 vport_record_error(vport, VPORT_E_TX_DROPPED);
1319 tnl_free_linked_skbs(skb);
1321 dst_release(unattached_dst);
1322 vport_record_error(vport, err);
1327 static const struct nla_policy tnl_policy[ODP_TUNNEL_ATTR_MAX + 1] = {
1328 [ODP_TUNNEL_ATTR_FLAGS] = { .type = NLA_U32 },
1329 [ODP_TUNNEL_ATTR_DST_IPV4] = { .type = NLA_U32 },
1330 [ODP_TUNNEL_ATTR_SRC_IPV4] = { .type = NLA_U32 },
1331 [ODP_TUNNEL_ATTR_OUT_KEY] = { .type = NLA_U64 },
1332 [ODP_TUNNEL_ATTR_IN_KEY] = { .type = NLA_U64 },
1333 [ODP_TUNNEL_ATTR_TOS] = { .type = NLA_U8 },
1334 [ODP_TUNNEL_ATTR_TTL] = { .type = NLA_U8 },
1337 /* Sets ODP_TUNNEL_ATTR_* fields in 'mutable', which must initially be zeroed. */
1338 static int tnl_set_config(struct nlattr *options, const struct tnl_ops *tnl_ops,
1339 const struct vport *cur_vport,
1340 struct tnl_mutable_config *mutable)
1342 const struct vport *old_vport;
1343 const struct tnl_mutable_config *old_mutable;
1344 struct nlattr *a[ODP_TUNNEL_ATTR_MAX + 1];
1350 err = nla_parse_nested(a, ODP_TUNNEL_ATTR_MAX, options, tnl_policy);
1354 if (!a[ODP_TUNNEL_ATTR_FLAGS] || !a[ODP_TUNNEL_ATTR_DST_IPV4])
1357 mutable->flags = nla_get_u32(a[ODP_TUNNEL_ATTR_FLAGS]) & TNL_F_PUBLIC;
1359 if (a[ODP_TUNNEL_ATTR_SRC_IPV4])
1360 mutable->saddr = nla_get_be32(a[ODP_TUNNEL_ATTR_SRC_IPV4]);
1361 mutable->daddr = nla_get_be32(a[ODP_TUNNEL_ATTR_DST_IPV4]);
1363 if (a[ODP_TUNNEL_ATTR_TOS]) {
1364 mutable->tos = nla_get_u8(a[ODP_TUNNEL_ATTR_TOS]);
1365 if (mutable->tos != RT_TOS(mutable->tos))
1369 if (a[ODP_TUNNEL_ATTR_TTL])
1370 mutable->ttl = nla_get_u8(a[ODP_TUNNEL_ATTR_TTL]);
1372 mutable->tunnel_hlen = tnl_ops->hdr_len(mutable);
1373 if (mutable->tunnel_hlen < 0)
1374 return mutable->tunnel_hlen;
1376 mutable->tunnel_hlen += sizeof(struct iphdr);
1378 mutable->tunnel_type = tnl_ops->tunnel_type;
1379 if (!a[ODP_TUNNEL_ATTR_IN_KEY]) {
1380 mutable->tunnel_type |= TNL_T_KEY_MATCH;
1381 mutable->flags |= TNL_F_IN_KEY_MATCH;
1383 mutable->tunnel_type |= TNL_T_KEY_EXACT;
1384 mutable->in_key = nla_get_be64(a[ODP_TUNNEL_ATTR_IN_KEY]);
1387 if (!a[ODP_TUNNEL_ATTR_OUT_KEY])
1388 mutable->flags |= TNL_F_OUT_KEY_ACTION;
1390 mutable->out_key = nla_get_be64(a[ODP_TUNNEL_ATTR_OUT_KEY]);
1392 old_vport = tnl_find_port(mutable->saddr, mutable->daddr,
1393 mutable->in_key, mutable->tunnel_type,
1396 if (old_vport && old_vport != cur_vport)
1402 struct vport *tnl_create(const struct vport_parms *parms,
1403 const struct vport_ops *vport_ops,
1404 const struct tnl_ops *tnl_ops)
1406 struct vport *vport;
1407 struct tnl_vport *tnl_vport;
1408 struct tnl_mutable_config *mutable;
1409 int initial_frag_id;
1412 vport = vport_alloc(sizeof(struct tnl_vport), vport_ops, parms);
1413 if (IS_ERR(vport)) {
1414 err = PTR_ERR(vport);
1418 tnl_vport = tnl_vport_priv(vport);
1420 strcpy(tnl_vport->name, parms->name);
1421 tnl_vport->tnl_ops = tnl_ops;
1423 mutable = kzalloc(sizeof(struct tnl_mutable_config), GFP_KERNEL);
1426 goto error_free_vport;
1429 vport_gen_rand_ether_addr(mutable->eth_addr);
1431 get_random_bytes(&initial_frag_id, sizeof(int));
1432 atomic_set(&tnl_vport->frag_id, initial_frag_id);
1434 err = tnl_set_config(parms->options, tnl_ops, NULL, mutable);
1436 goto error_free_mutable;
1438 spin_lock_init(&tnl_vport->cache_lock);
1440 #ifdef NEED_CACHE_TIMEOUT
1441 tnl_vport->cache_exp_interval = MAX_CACHE_EXP -
1442 (net_random() % (MAX_CACHE_EXP / 2));
1445 rcu_assign_pointer(tnl_vport->mutable, mutable);
1447 err = add_port(vport);
1449 goto error_free_mutable;
1458 return ERR_PTR(err);
1461 int tnl_set_options(struct vport *vport, struct nlattr *options)
1463 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1464 const struct tnl_mutable_config *old_mutable;
1465 struct tnl_mutable_config *mutable;
1468 mutable = kzalloc(sizeof(struct tnl_mutable_config), GFP_KERNEL);
1474 /* Copy fields whose values should be retained. */
1475 old_mutable = rtnl_dereference(tnl_vport->mutable);
1476 mutable->seq = old_mutable->seq + 1;
1477 memcpy(mutable->eth_addr, old_mutable->eth_addr, ETH_ALEN);
1479 /* Parse the others configured by userspace. */
1480 err = tnl_set_config(options, tnl_vport->tnl_ops, vport, mutable);
1484 err = move_port(vport, mutable);
1496 int tnl_get_options(const struct vport *vport, struct sk_buff *skb)
1498 const struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1499 const struct tnl_mutable_config *mutable = rcu_dereference_rtnl(tnl_vport->mutable);
1501 NLA_PUT_U32(skb, ODP_TUNNEL_ATTR_FLAGS, mutable->flags & TNL_F_PUBLIC);
1502 NLA_PUT_BE32(skb, ODP_TUNNEL_ATTR_DST_IPV4, mutable->daddr);
1504 if (!(mutable->flags & TNL_F_IN_KEY_MATCH))
1505 NLA_PUT_BE64(skb, ODP_TUNNEL_ATTR_IN_KEY, mutable->in_key);
1506 if (!(mutable->flags & TNL_F_OUT_KEY_ACTION))
1507 NLA_PUT_BE64(skb, ODP_TUNNEL_ATTR_OUT_KEY, mutable->out_key);
1509 NLA_PUT_BE32(skb, ODP_TUNNEL_ATTR_SRC_IPV4, mutable->saddr);
1511 NLA_PUT_U8(skb, ODP_TUNNEL_ATTR_TOS, mutable->tos);
1513 NLA_PUT_U8(skb, ODP_TUNNEL_ATTR_TTL, mutable->ttl);
1521 static void free_port_rcu(struct rcu_head *rcu)
1523 struct tnl_vport *tnl_vport = container_of(rcu,
1524 struct tnl_vport, rcu);
1526 free_cache((struct tnl_cache __force *)tnl_vport->cache);
1527 kfree((struct tnl_mutable __force *)tnl_vport->mutable);
1528 vport_free(tnl_vport_to_vport(tnl_vport));
1531 int tnl_destroy(struct vport *vport)
1533 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1534 const struct tnl_mutable_config *mutable, *old_mutable;
1536 mutable = rtnl_dereference(tnl_vport->mutable);
1538 if (vport == tnl_find_port(mutable->saddr, mutable->daddr,
1539 mutable->in_key, mutable->tunnel_type,
1543 call_rcu(&tnl_vport->rcu, free_port_rcu);
1548 int tnl_set_addr(struct vport *vport, const unsigned char *addr)
1550 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1551 struct tnl_mutable_config *mutable;
1553 mutable = kmemdup(rtnl_dereference(tnl_vport->mutable),
1554 sizeof(struct tnl_mutable_config), GFP_KERNEL);
1558 memcpy(mutable->eth_addr, addr, ETH_ALEN);
1559 assign_config_rcu(vport, mutable);
1564 const char *tnl_get_name(const struct vport *vport)
1566 const struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1567 return tnl_vport->name;
1570 const unsigned char *tnl_get_addr(const struct vport *vport)
1572 const struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1573 return rcu_dereference_rtnl(tnl_vport->mutable)->eth_addr;
1576 void tnl_free_linked_skbs(struct sk_buff *skb)
1582 struct sk_buff *next = skb->next;