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>
36 #include "vport-generic.h"
37 #include "vport-internal_dev.h"
39 #ifdef NEED_CACHE_TIMEOUT
41 * On kernels where we can't quickly detect changes in the rest of the system
42 * we use an expiration time to invalidate the cache. A shorter expiration
43 * reduces the length of time that we may potentially blackhole packets while
44 * a longer time increases performance by reducing the frequency that the
45 * cache needs to be rebuilt. A variety of factors may cause the cache to be
46 * invalidated before the expiration time but this is the maximum. The time
47 * is expressed in jiffies.
49 #define MAX_CACHE_EXP HZ
53 * Interval to check for and remove caches that are no longer valid. Caches
54 * are checked for validity before they are used for packet encapsulation and
55 * old caches are removed at that time. However, if no packets are sent through
56 * the tunnel then the cache will never be destroyed. Since it holds
57 * references to a number of system objects, the cache will continue to use
58 * system resources by not allowing those objects to be destroyed. The cache
59 * cleaner is periodically run to free invalid caches. It does not
60 * significantly affect system performance. A lower interval will release
61 * resources faster but will itself consume resources by requiring more frequent
62 * checks. A longer interval may result in messages being printed to the kernel
63 * message buffer about unreleased resources. The interval is expressed in
66 #define CACHE_CLEANER_INTERVAL (5 * HZ)
68 #define CACHE_DATA_ALIGN 16
70 /* Protected by RCU. */
71 static struct tbl *port_table __read_mostly;
73 static void cache_cleaner(struct work_struct *work);
74 DECLARE_DELAYED_WORK(cache_cleaner_wq, cache_cleaner);
77 * These are just used as an optimization: they don't require any kind of
78 * synchronization because we could have just as easily read the value before
79 * the port change happened.
81 static unsigned int key_local_remote_ports __read_mostly;
82 static unsigned int key_remote_ports __read_mostly;
83 static unsigned int local_remote_ports __read_mostly;
84 static unsigned int remote_ports __read_mostly;
86 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
87 #define rt_dst(rt) (rt->dst)
89 #define rt_dst(rt) (rt->u.dst)
92 static inline struct vport *tnl_vport_to_vport(const struct tnl_vport *tnl_vport)
94 return vport_from_priv(tnl_vport);
97 static inline struct tnl_vport *tnl_vport_table_cast(const struct tbl_node *node)
99 return container_of(node, struct tnl_vport, tbl_node);
102 static inline void schedule_cache_cleaner(void)
104 schedule_delayed_work(&cache_cleaner_wq, CACHE_CLEANER_INTERVAL);
107 static void free_cache(struct tnl_cache *cache)
112 flow_put(cache->flow);
113 ip_rt_put(cache->rt);
117 static void free_config_rcu(struct rcu_head *rcu)
119 struct tnl_mutable_config *c = container_of(rcu, struct tnl_mutable_config, rcu);
123 static void free_cache_rcu(struct rcu_head *rcu)
125 struct tnl_cache *c = container_of(rcu, struct tnl_cache, rcu);
129 static void assign_config_rcu(struct vport *vport,
130 struct tnl_mutable_config *new_config)
132 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
133 struct tnl_mutable_config *old_config;
135 old_config = tnl_vport->mutable;
136 rcu_assign_pointer(tnl_vport->mutable, new_config);
137 call_rcu(&old_config->rcu, free_config_rcu);
140 static void assign_cache_rcu(struct vport *vport, struct tnl_cache *new_cache)
142 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
143 struct tnl_cache *old_cache;
145 old_cache = tnl_vport->cache;
146 rcu_assign_pointer(tnl_vport->cache, new_cache);
149 call_rcu(&old_cache->rcu, free_cache_rcu);
152 static unsigned int *find_port_pool(const struct tnl_mutable_config *mutable)
154 if (mutable->port_config.flags & TNL_F_IN_KEY_MATCH) {
155 if (mutable->port_config.saddr)
156 return &local_remote_ports;
158 return &remote_ports;
160 if (mutable->port_config.saddr)
161 return &key_local_remote_ports;
163 return &key_remote_ports;
168 LOOKUP_TUNNEL_TYPE = 0,
174 struct port_lookup_key {
175 u32 vals[4]; /* Contains enum lookup_key keys. */
176 const struct tnl_mutable_config *mutable;
180 * Modifies 'target' to store the rcu_dereferenced pointer that was used to do
183 static int port_cmp(const struct tbl_node *node, void *target)
185 const struct tnl_vport *tnl_vport = tnl_vport_table_cast(node);
186 struct port_lookup_key *lookup = target;
188 lookup->mutable = rcu_dereference(tnl_vport->mutable);
190 return (lookup->mutable->tunnel_type == lookup->vals[LOOKUP_TUNNEL_TYPE]) &&
191 lookup->mutable->port_config.daddr == lookup->vals[LOOKUP_DADDR] &&
192 lookup->mutable->port_config.in_key == lookup->vals[LOOKUP_KEY] &&
193 lookup->mutable->port_config.saddr == lookup->vals[LOOKUP_SADDR];
196 static u32 port_hash(struct port_lookup_key *lookup)
198 return jhash2(lookup->vals, ARRAY_SIZE(lookup->vals), 0);
201 static u32 mutable_hash(const struct tnl_mutable_config *mutable)
203 struct port_lookup_key lookup;
205 lookup.vals[LOOKUP_SADDR] = mutable->port_config.saddr;
206 lookup.vals[LOOKUP_DADDR] = mutable->port_config.daddr;
207 lookup.vals[LOOKUP_KEY] = mutable->port_config.in_key;
208 lookup.vals[LOOKUP_TUNNEL_TYPE] = mutable->tunnel_type;
210 return port_hash(&lookup);
213 static void check_table_empty(void)
215 if (tbl_count(port_table) == 0) {
216 struct tbl *old_table = port_table;
218 cancel_delayed_work_sync(&cache_cleaner_wq);
219 rcu_assign_pointer(port_table, NULL);
220 tbl_deferred_destroy(old_table, NULL);
224 static int add_port(struct vport *vport)
226 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
230 struct tbl *new_table;
232 new_table = tbl_create(0);
236 rcu_assign_pointer(port_table, new_table);
237 schedule_cache_cleaner();
239 } else if (tbl_count(port_table) > tbl_n_buckets(port_table)) {
240 struct tbl *old_table = port_table;
241 struct tbl *new_table;
243 new_table = tbl_expand(old_table);
244 if (IS_ERR(new_table))
245 return PTR_ERR(new_table);
247 rcu_assign_pointer(port_table, new_table);
248 tbl_deferred_destroy(old_table, NULL);
251 err = tbl_insert(port_table, &tnl_vport->tbl_node, mutable_hash(tnl_vport->mutable));
257 (*find_port_pool(tnl_vport->mutable))++;
262 static int move_port(struct vport *vport, struct tnl_mutable_config *new_mutable)
265 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
268 hash = mutable_hash(new_mutable);
269 if (hash == tnl_vport->tbl_node.hash)
273 * Ideally we should make this move atomic to avoid having gaps in
274 * finding tunnels or the possibility of failure. However, if we do
275 * find a tunnel it will always be consistent.
277 err = tbl_remove(port_table, &tnl_vport->tbl_node);
281 err = tbl_insert(port_table, &tnl_vport->tbl_node, hash);
288 assign_config_rcu(vport, new_mutable);
293 static int del_port(struct vport *vport)
295 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
298 err = tbl_remove(port_table, &tnl_vport->tbl_node);
303 (*find_port_pool(tnl_vport->mutable))--;
308 struct vport *tnl_find_port(__be32 saddr, __be32 daddr, __be32 key,
310 const struct tnl_mutable_config **mutable)
312 struct port_lookup_key lookup;
313 struct tbl *table = rcu_dereference(port_table);
314 struct tbl_node *tbl_node;
316 if (unlikely(!table))
319 lookup.vals[LOOKUP_SADDR] = saddr;
320 lookup.vals[LOOKUP_DADDR] = daddr;
322 if (tunnel_type & TNL_T_KEY_EXACT) {
323 lookup.vals[LOOKUP_KEY] = key;
324 lookup.vals[LOOKUP_TUNNEL_TYPE] = tunnel_type & ~TNL_T_KEY_MATCH;
326 if (key_local_remote_ports) {
327 tbl_node = tbl_lookup(table, &lookup, port_hash(&lookup), port_cmp);
332 if (key_remote_ports) {
333 lookup.vals[LOOKUP_SADDR] = 0;
335 tbl_node = tbl_lookup(table, &lookup, port_hash(&lookup), port_cmp);
339 lookup.vals[LOOKUP_SADDR] = saddr;
343 if (tunnel_type & TNL_T_KEY_MATCH) {
344 lookup.vals[LOOKUP_KEY] = 0;
345 lookup.vals[LOOKUP_TUNNEL_TYPE] = tunnel_type & ~TNL_T_KEY_EXACT;
347 if (local_remote_ports) {
348 tbl_node = tbl_lookup(table, &lookup, port_hash(&lookup), port_cmp);
354 lookup.vals[LOOKUP_SADDR] = 0;
356 tbl_node = tbl_lookup(table, &lookup, port_hash(&lookup), port_cmp);
365 *mutable = lookup.mutable;
366 return tnl_vport_to_vport(tnl_vport_table_cast(tbl_node));
369 static inline void ecn_decapsulate(struct sk_buff *skb)
371 u8 tos = ip_hdr(skb)->tos;
373 if (INET_ECN_is_ce(tos)) {
374 __be16 protocol = skb->protocol;
375 unsigned int nw_header = skb_network_offset(skb);
377 if (skb->protocol == htons(ETH_P_8021Q)) {
378 if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
381 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
382 nw_header += VLAN_HLEN;
385 if (protocol == htons(ETH_P_IP)) {
386 if (unlikely(!pskb_may_pull(skb, nw_header
387 + sizeof(struct iphdr))))
390 IP_ECN_set_ce((struct iphdr *)(skb->data + nw_header));
392 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
393 else if (protocol == htons(ETH_P_IPV6)) {
394 if (unlikely(!pskb_may_pull(skb, nw_header
395 + sizeof(struct ipv6hdr))))
398 IP6_ECN_set_ce((struct ipv6hdr *)(skb->data + nw_header));
404 /* Called with rcu_read_lock. */
405 void tnl_rcv(struct vport *vport, struct sk_buff *skb)
407 skb->pkt_type = PACKET_HOST;
408 skb->protocol = eth_type_trans(skb, skb->dev);
413 skb_reset_network_header(skb);
415 ecn_decapsulate(skb);
417 skb_push(skb, ETH_HLEN);
418 compute_ip_summed(skb, false);
420 vport_receive(vport, skb);
423 static bool check_ipv4_address(__be32 addr)
425 if (ipv4_is_multicast(addr) || ipv4_is_lbcast(addr)
426 || ipv4_is_loopback(addr) || ipv4_is_zeronet(addr))
432 static bool ipv4_should_icmp(struct sk_buff *skb)
434 struct iphdr *old_iph = ip_hdr(skb);
436 /* Don't respond to L2 broadcast. */
437 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
440 /* Don't respond to L3 broadcast or invalid addresses. */
441 if (!check_ipv4_address(old_iph->daddr) ||
442 !check_ipv4_address(old_iph->saddr))
445 /* Only respond to the first fragment. */
446 if (old_iph->frag_off & htons(IP_OFFSET))
449 /* Don't respond to ICMP error messages. */
450 if (old_iph->protocol == IPPROTO_ICMP) {
451 u8 icmp_type, *icmp_typep;
453 icmp_typep = skb_header_pointer(skb, (u8 *)old_iph +
454 (old_iph->ihl << 2) +
455 offsetof(struct icmphdr, type) -
456 skb->data, sizeof(icmp_type),
462 if (*icmp_typep > NR_ICMP_TYPES
463 || (*icmp_typep <= ICMP_PARAMETERPROB
464 && *icmp_typep != ICMP_ECHOREPLY
465 && *icmp_typep != ICMP_ECHO))
472 static void ipv4_build_icmp(struct sk_buff *skb, struct sk_buff *nskb,
473 unsigned int mtu, unsigned int payload_length)
475 struct iphdr *iph, *old_iph = ip_hdr(skb);
476 struct icmphdr *icmph;
479 iph = (struct iphdr *)skb_put(nskb, sizeof(struct iphdr));
480 icmph = (struct icmphdr *)skb_put(nskb, sizeof(struct icmphdr));
481 payload = skb_put(nskb, payload_length);
485 iph->ihl = sizeof(struct iphdr) >> 2;
486 iph->tos = (old_iph->tos & IPTOS_TOS_MASK) |
487 IPTOS_PREC_INTERNETCONTROL;
488 iph->tot_len = htons(sizeof(struct iphdr)
489 + sizeof(struct icmphdr)
491 get_random_bytes(&iph->id, sizeof(iph->id));
494 iph->protocol = IPPROTO_ICMP;
495 iph->daddr = old_iph->saddr;
496 iph->saddr = old_iph->daddr;
501 icmph->type = ICMP_DEST_UNREACH;
502 icmph->code = ICMP_FRAG_NEEDED;
503 icmph->un.gateway = htonl(mtu);
506 nskb->csum = csum_partial((u8 *)icmph, sizeof(struct icmphdr), 0);
507 nskb->csum = skb_copy_and_csum_bits(skb, (u8 *)old_iph - skb->data,
508 payload, payload_length,
510 icmph->checksum = csum_fold(nskb->csum);
513 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
514 static bool ipv6_should_icmp(struct sk_buff *skb)
516 struct ipv6hdr *old_ipv6h = ipv6_hdr(skb);
518 int payload_off = (u8 *)(old_ipv6h + 1) - skb->data;
519 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
521 /* Check source address is valid. */
522 addr_type = ipv6_addr_type(&old_ipv6h->saddr);
523 if (addr_type & IPV6_ADDR_MULTICAST || addr_type == IPV6_ADDR_ANY)
526 /* Don't reply to unspecified addresses. */
527 if (ipv6_addr_type(&old_ipv6h->daddr) == IPV6_ADDR_ANY)
530 /* Don't respond to ICMP error messages. */
531 payload_off = ipv6_skip_exthdr(skb, payload_off, &nexthdr);
535 if (nexthdr == NEXTHDR_ICMP) {
536 u8 icmp_type, *icmp_typep;
538 icmp_typep = skb_header_pointer(skb, payload_off +
539 offsetof(struct icmp6hdr,
541 sizeof(icmp_type), &icmp_type);
543 if (!icmp_typep || !(*icmp_typep & ICMPV6_INFOMSG_MASK))
550 static void ipv6_build_icmp(struct sk_buff *skb, struct sk_buff *nskb,
551 unsigned int mtu, unsigned int payload_length)
553 struct ipv6hdr *ipv6h, *old_ipv6h = ipv6_hdr(skb);
554 struct icmp6hdr *icmp6h;
557 ipv6h = (struct ipv6hdr *)skb_put(nskb, sizeof(struct ipv6hdr));
558 icmp6h = (struct icmp6hdr *)skb_put(nskb, sizeof(struct icmp6hdr));
559 payload = skb_put(nskb, payload_length);
564 memset(&ipv6h->flow_lbl, 0, sizeof(ipv6h->flow_lbl));
565 ipv6h->payload_len = htons(sizeof(struct icmp6hdr)
567 ipv6h->nexthdr = NEXTHDR_ICMP;
568 ipv6h->hop_limit = IPV6_DEFAULT_HOPLIMIT;
569 ipv6_addr_copy(&ipv6h->daddr, &old_ipv6h->saddr);
570 ipv6_addr_copy(&ipv6h->saddr, &old_ipv6h->daddr);
573 icmp6h->icmp6_type = ICMPV6_PKT_TOOBIG;
574 icmp6h->icmp6_code = 0;
575 icmp6h->icmp6_cksum = 0;
576 icmp6h->icmp6_mtu = htonl(mtu);
578 nskb->csum = csum_partial((u8 *)icmp6h, sizeof(struct icmp6hdr), 0);
579 nskb->csum = skb_copy_and_csum_bits(skb, (u8 *)old_ipv6h - skb->data,
580 payload, payload_length,
582 icmp6h->icmp6_cksum = csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
583 sizeof(struct icmp6hdr)
585 ipv6h->nexthdr, nskb->csum);
589 bool tnl_frag_needed(struct vport *vport, const struct tnl_mutable_config *mutable,
590 struct sk_buff *skb, unsigned int mtu, __be32 flow_key)
592 unsigned int eth_hdr_len = ETH_HLEN;
593 unsigned int total_length = 0, header_length = 0, payload_length;
594 struct ethhdr *eh, *old_eh = eth_hdr(skb);
595 struct sk_buff *nskb;
598 if (skb->protocol == htons(ETH_P_IP)) {
599 if (mtu < IP_MIN_MTU)
602 if (!ipv4_should_icmp(skb))
605 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
606 else if (skb->protocol == htons(ETH_P_IPV6)) {
607 if (mtu < IPV6_MIN_MTU)
611 * In theory we should do PMTUD on IPv6 multicast messages but
612 * we don't have an address to send from so just fragment.
614 if (ipv6_addr_type(&ipv6_hdr(skb)->daddr) & IPV6_ADDR_MULTICAST)
617 if (!ipv6_should_icmp(skb))
625 if (old_eh->h_proto == htons(ETH_P_8021Q))
626 eth_hdr_len = VLAN_ETH_HLEN;
628 payload_length = skb->len - eth_hdr_len;
629 if (skb->protocol == htons(ETH_P_IP)) {
630 header_length = sizeof(struct iphdr) + sizeof(struct icmphdr);
631 total_length = min_t(unsigned int, header_length +
632 payload_length, 576);
634 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
636 header_length = sizeof(struct ipv6hdr) +
637 sizeof(struct icmp6hdr);
638 total_length = min_t(unsigned int, header_length +
639 payload_length, IPV6_MIN_MTU);
643 total_length = min(total_length, mutable->mtu);
644 payload_length = total_length - header_length;
646 nskb = dev_alloc_skb(NET_IP_ALIGN + eth_hdr_len + header_length +
651 skb_reserve(nskb, NET_IP_ALIGN);
653 /* Ethernet / VLAN */
654 eh = (struct ethhdr *)skb_put(nskb, eth_hdr_len);
655 memcpy(eh->h_dest, old_eh->h_source, ETH_ALEN);
656 memcpy(eh->h_source, mutable->eth_addr, ETH_ALEN);
657 nskb->protocol = eh->h_proto = old_eh->h_proto;
658 if (old_eh->h_proto == htons(ETH_P_8021Q)) {
659 struct vlan_ethhdr *vh = (struct vlan_ethhdr *)eh;
661 vh->h_vlan_TCI = vlan_eth_hdr(skb)->h_vlan_TCI;
662 vh->h_vlan_encapsulated_proto = skb->protocol;
664 skb_reset_mac_header(nskb);
667 if (skb->protocol == htons(ETH_P_IP))
668 ipv4_build_icmp(skb, nskb, mtu, payload_length);
669 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
671 ipv6_build_icmp(skb, nskb, mtu, payload_length);
675 * Assume that flow based keys are symmetric with respect to input
676 * and output and use the key that we were going to put on the
677 * outgoing packet for the fake received packet. If the keys are
678 * not symmetric then PMTUD needs to be disabled since we won't have
679 * any way of synthesizing packets.
681 if ((mutable->port_config.flags & (TNL_F_IN_KEY_MATCH | TNL_F_OUT_KEY_ACTION)) ==
682 (TNL_F_IN_KEY_MATCH | TNL_F_OUT_KEY_ACTION))
683 OVS_CB(nskb)->tun_id = flow_key;
685 compute_ip_summed(nskb, false);
686 vport_receive(vport, nskb);
691 static bool check_mtu(struct sk_buff *skb,
693 const struct tnl_mutable_config *mutable,
694 const struct rtable *rt, __be16 *frag_offp)
699 frag_off = (mutable->port_config.flags & TNL_F_PMTUD) ? htons(IP_DF) : 0;
701 mtu = dst_mtu(&rt_dst(rt))
703 - mutable->tunnel_hlen
704 - (eth_hdr(skb)->h_proto == htons(ETH_P_8021Q) ? VLAN_HLEN : 0);
708 if (skb->protocol == htons(ETH_P_IP)) {
709 struct iphdr *old_iph = ip_hdr(skb);
711 frag_off |= old_iph->frag_off & htons(IP_DF);
712 mtu = max(mtu, IP_MIN_MTU);
714 if ((old_iph->frag_off & htons(IP_DF)) &&
715 mtu < ntohs(old_iph->tot_len)) {
716 if (tnl_frag_needed(vport, mutable, skb, mtu, OVS_CB(skb)->tun_id))
720 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
721 else if (skb->protocol == htons(ETH_P_IPV6)) {
722 unsigned int packet_length = skb->len - ETH_HLEN
723 - (eth_hdr(skb)->h_proto == htons(ETH_P_8021Q) ? VLAN_HLEN : 0);
725 mtu = max(mtu, IPV6_MIN_MTU);
727 /* IPv6 requires PMTUD if the packet is above the minimum MTU. */
728 if (packet_length > IPV6_MIN_MTU)
729 frag_off = htons(IP_DF);
731 if (mtu < packet_length) {
732 if (tnl_frag_needed(vport, mutable, skb, mtu, OVS_CB(skb)->tun_id))
738 *frag_offp = frag_off;
746 static void create_tunnel_header(const struct vport *vport,
747 const struct tnl_mutable_config *mutable,
748 const struct rtable *rt, void *header)
750 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
751 struct iphdr *iph = header;
754 iph->ihl = sizeof(struct iphdr) >> 2;
755 iph->frag_off = htons(IP_DF);
756 iph->protocol = tnl_vport->tnl_ops->ipproto;
757 iph->tos = mutable->port_config.tos;
758 iph->daddr = rt->rt_dst;
759 iph->saddr = rt->rt_src;
760 iph->ttl = mutable->port_config.ttl;
762 iph->ttl = dst_metric(&rt_dst(rt), RTAX_HOPLIMIT);
764 tnl_vport->tnl_ops->build_header(vport, mutable, iph + 1);
767 static inline void *get_cached_header(const struct tnl_cache *cache)
769 return (void *)cache + ALIGN(sizeof(struct tnl_cache), CACHE_DATA_ALIGN);
772 static inline bool check_cache_valid(const struct tnl_cache *cache,
773 const struct tnl_mutable_config *mutable)
776 #ifdef NEED_CACHE_TIMEOUT
777 time_before(jiffies, cache->expiration) &&
780 atomic_read(&init_net.ipv4.rt_genid) == cache->rt->rt_genid &&
783 rt_dst(cache->rt).hh->hh_lock.sequence == cache->hh_seq &&
785 mutable->seq == cache->mutable_seq &&
786 (!is_internal_dev(rt_dst(cache->rt).dev) ||
787 (cache->flow && !cache->flow->dead));
790 static int cache_cleaner_cb(struct tbl_node *tbl_node, void *aux)
792 struct tnl_vport *tnl_vport = tnl_vport_table_cast(tbl_node);
793 const struct tnl_mutable_config *mutable = rcu_dereference(tnl_vport->mutable);
794 const struct tnl_cache *cache = rcu_dereference(tnl_vport->cache);
796 if (cache && !check_cache_valid(cache, mutable) &&
797 spin_trylock_bh(&tnl_vport->cache_lock)) {
798 assign_cache_rcu(tnl_vport_to_vport(tnl_vport), NULL);
799 spin_unlock_bh(&tnl_vport->cache_lock);
805 static void cache_cleaner(struct work_struct *work)
807 schedule_cache_cleaner();
810 tbl_foreach(port_table, cache_cleaner_cb, NULL);
814 static inline void create_eth_hdr(struct tnl_cache *cache,
815 const struct rtable *rt)
817 void *cache_data = get_cached_header(cache);
818 int hh_len = rt_dst(rt).hh->hh_len;
819 int hh_off = HH_DATA_ALIGN(rt_dst(rt).hh->hh_len) - hh_len;
825 hh_seq = read_seqbegin(&rt_dst(rt).hh->hh_lock);
826 memcpy(cache_data, (void *)rt_dst(rt).hh->hh_data + hh_off, hh_len);
827 } while (read_seqretry(&rt_dst(rt).hh->hh_lock, hh_seq));
829 cache->hh_seq = hh_seq;
831 read_lock_bh(&rt_dst(rt).hh->hh_lock);
832 memcpy(cache_data, (void *)rt_dst(rt).hh->hh_data + hh_off, hh_len);
833 read_unlock_bh(&rt_dst(rt).hh->hh_lock);
837 static struct tnl_cache *build_cache(struct vport *vport,
838 const struct tnl_mutable_config *mutable,
841 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
842 struct tnl_cache *cache;
846 if (!(mutable->port_config.flags & TNL_F_HDR_CACHE))
850 * If there is no entry in the ARP cache or if this device does not
851 * support hard header caching just fall back to the IP stack.
857 * If lock is contended fall back to directly building the header.
858 * We're not going to help performance by sitting here spinning.
860 if (!spin_trylock_bh(&tnl_vport->cache_lock))
863 cache = tnl_vport->cache;
864 if (check_cache_valid(cache, mutable))
869 cache_len = rt_dst(rt).hh->hh_len + mutable->tunnel_hlen;
871 cache = kzalloc(ALIGN(sizeof(struct tnl_cache), CACHE_DATA_ALIGN) +
872 cache_len, GFP_ATOMIC);
876 cache->len = cache_len;
878 create_eth_hdr(cache, rt);
879 cache_data = get_cached_header(cache) + rt_dst(rt).hh->hh_len;
881 create_tunnel_header(vport, mutable, rt, cache_data);
883 cache->mutable_seq = mutable->seq;
885 #ifdef NEED_CACHE_TIMEOUT
886 cache->expiration = jiffies + tnl_vport->cache_exp_interval;
889 if (is_internal_dev(rt_dst(rt).dev)) {
892 struct dp_port *dp_port;
895 struct odp_flow_key flow_key;
896 struct tbl_node *flow_node;
898 vport = internal_dev_get_vport(rt_dst(rt).dev);
902 dp_port = vport_get_dp_port(vport);
906 skb = alloc_skb(cache->len, GFP_ATOMIC);
910 __skb_put(skb, cache->len);
911 memcpy(skb->data, get_cached_header(cache), cache->len);
913 err = flow_extract(skb, dp_port->port_no, &flow_key, &is_frag);
919 flow_node = tbl_lookup(rcu_dereference(dp_port->dp->table),
920 &flow_key, flow_hash(&flow_key),
923 struct sw_flow *flow = flow_cast(flow_node);
931 assign_cache_rcu(vport, cache);
934 spin_unlock_bh(&tnl_vport->cache_lock);
939 static struct rtable *find_route(struct vport *vport,
940 const struct tnl_mutable_config *mutable,
941 u8 tos, struct tnl_cache **cache)
943 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
944 struct tnl_cache *cur_cache = rcu_dereference(tnl_vport->cache);
949 if (likely(tos == mutable->port_config.tos &&
950 check_cache_valid(cur_cache, mutable))) {
952 return cur_cache->rt;
955 struct flowi fl = { .nl_u = { .ip4_u =
956 { .daddr = mutable->port_config.daddr,
957 .saddr = mutable->port_config.saddr,
959 .proto = tnl_vport->tnl_ops->ipproto };
961 if (unlikely(ip_route_output_key(&init_net, &rt, &fl)))
964 if (likely(tos == mutable->port_config.tos))
965 *cache = build_cache(vport, mutable, rt);
971 static struct sk_buff *check_headroom(struct sk_buff *skb, int headroom)
973 if (skb_headroom(skb) < headroom || skb_header_cloned(skb)) {
974 struct sk_buff *nskb = skb_realloc_headroom(skb, headroom + 16);
975 if (unlikely(!nskb)) {
977 return ERR_PTR(-ENOMEM);
980 set_skb_csum_bits(skb, nskb);
983 skb_set_owner_w(nskb, skb->sk);
992 static inline bool need_linearize(const struct sk_buff *skb)
996 if (unlikely(skb_shinfo(skb)->frag_list))
1000 * Generally speaking we should linearize if there are paged frags.
1001 * However, if all of the refcounts are 1 we know nobody else can
1002 * change them from underneath us and we can skip the linearization.
1004 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1005 if (unlikely(page_count(skb_shinfo(skb)->frags[0].page) > 1))
1011 static struct sk_buff *handle_offloads(struct sk_buff *skb,
1012 const struct tnl_mutable_config *mutable,
1013 const struct rtable *rt)
1018 forward_ip_summed(skb);
1020 err = vswitch_skb_checksum_setup(skb);
1024 min_headroom = LL_RESERVED_SPACE(rt_dst(rt).dev) + rt_dst(rt).header_len
1025 + mutable->tunnel_hlen;
1027 if (skb_is_gso(skb)) {
1028 struct sk_buff *nskb;
1031 * If we are doing GSO on a pskb it is better to make sure that
1032 * the headroom is correct now. We will only have to copy the
1033 * portion in the linear data area and GSO will preserve
1034 * headroom when it creates the segments. This is particularly
1035 * beneficial on Xen where we get a lot of GSO pskbs.
1036 * Conversely, we avoid copying if it is just to get our own
1037 * writable clone because GSO will do the copy for us.
1039 if (skb_headroom(skb) < min_headroom) {
1040 skb = check_headroom(skb, min_headroom);
1041 if (unlikely(IS_ERR(skb))) {
1047 nskb = skb_gso_segment(skb, 0);
1049 if (unlikely(IS_ERR(nskb))) {
1050 err = PTR_ERR(nskb);
1056 skb = check_headroom(skb, min_headroom);
1057 if (unlikely(IS_ERR(skb))) {
1062 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1064 * Pages aren't locked and could change at any time.
1065 * If this happens after we compute the checksum, the
1066 * checksum will be wrong. We linearize now to avoid
1069 if (unlikely(need_linearize(skb))) {
1070 err = __skb_linearize(skb);
1075 err = skb_checksum_help(skb);
1078 } else if (skb->ip_summed == CHECKSUM_COMPLETE)
1079 skb->ip_summed = CHECKSUM_NONE;
1087 return ERR_PTR(err);
1090 static int send_frags(struct sk_buff *skb,
1091 const struct tnl_mutable_config *mutable)
1098 struct sk_buff *next = skb->next;
1099 int frag_len = skb->len - mutable->tunnel_hlen;
1102 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
1104 err = ip_local_out(skb);
1105 if (likely(net_xmit_eval(err) == 0))
1106 sent_len += frag_len;
1119 * There's no point in continuing to send fragments once one has been
1120 * dropped so just free the rest. This may help improve the congestion
1121 * that caused the first packet to be dropped.
1123 tnl_free_linked_skbs(skb);
1127 int tnl_send(struct vport *vport, struct sk_buff *skb)
1129 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1130 const struct tnl_mutable_config *mutable = rcu_dereference(tnl_vport->mutable);
1132 enum vport_err_type err = VPORT_E_TX_ERROR;
1134 struct dst_entry *unattached_dst = NULL;
1135 struct tnl_cache *cache;
1142 /* Validate the protocol headers before we try to use them. */
1143 if (skb->protocol == htons(ETH_P_8021Q)) {
1144 if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
1147 skb->protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
1148 skb_set_network_header(skb, VLAN_ETH_HLEN);
1151 if (skb->protocol == htons(ETH_P_IP)) {
1152 if (unlikely(!pskb_may_pull(skb, skb_network_offset(skb)
1153 + sizeof(struct iphdr))))
1156 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1157 else if (skb->protocol == htons(ETH_P_IPV6)) {
1158 if (unlikely(!pskb_may_pull(skb, skb_network_offset(skb)
1159 + sizeof(struct ipv6hdr))))
1165 if (skb->protocol == htons(ETH_P_IP))
1166 inner_tos = ip_hdr(skb)->tos;
1167 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1168 else if (skb->protocol == htons(ETH_P_IPV6))
1169 inner_tos = ipv6_get_dsfield(ipv6_hdr(skb));
1174 if (mutable->port_config.flags & TNL_F_TOS_INHERIT)
1177 tos = mutable->port_config.tos;
1179 tos = INET_ECN_encapsulate(tos, inner_tos);
1182 rt = find_route(vport, mutable, tos, &cache);
1185 if (unlikely(!cache))
1186 unattached_dst = &rt_dst(rt);
1194 skb = handle_offloads(skb, mutable, rt);
1195 if (unlikely(IS_ERR(skb)))
1199 if (unlikely(!check_mtu(skb, vport, mutable, rt, &frag_off))) {
1200 err = VPORT_E_TX_DROPPED;
1205 * If we are over the MTU, allow the IP stack to handle fragmentation.
1206 * Fragmentation is a slow path anyways.
1208 if (unlikely(skb->len + mutable->tunnel_hlen > dst_mtu(&rt_dst(rt)) &&
1210 unattached_dst = &rt_dst(rt);
1211 dst_hold(unattached_dst);
1216 ttl = mutable->port_config.ttl;
1218 ttl = dst_metric(&rt_dst(rt), RTAX_HOPLIMIT);
1220 if (mutable->port_config.flags & TNL_F_TTL_INHERIT) {
1221 if (skb->protocol == htons(ETH_P_IP))
1222 ttl = ip_hdr(skb)->ttl;
1223 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1224 else if (skb->protocol == htons(ETH_P_IPV6))
1225 ttl = ipv6_hdr(skb)->hop_limit;
1231 struct sk_buff *next_skb = skb->next;
1234 if (likely(cache)) {
1235 skb_push(skb, cache->len);
1236 memcpy(skb->data, get_cached_header(cache), cache->len);
1237 skb_reset_mac_header(skb);
1238 skb_set_network_header(skb, rt_dst(rt).hh->hh_len);
1241 skb_push(skb, mutable->tunnel_hlen);
1242 create_tunnel_header(vport, mutable, rt, skb->data);
1243 skb_reset_network_header(skb);
1246 skb_dst_set(skb, dst_clone(unattached_dst));
1248 skb_dst_set(skb, unattached_dst);
1249 unattached_dst = NULL;
1252 skb_set_transport_header(skb, skb_network_offset(skb) + sizeof(struct iphdr));
1257 iph->frag_off = frag_off;
1258 ip_select_ident(iph, &rt_dst(rt), NULL);
1260 skb = tnl_vport->tnl_ops->update_header(vport, mutable, &rt_dst(rt), skb);
1264 if (likely(cache)) {
1265 int orig_len = skb->len - cache->len;
1266 struct vport *cache_vport = internal_dev_get_vport(rt_dst(rt).dev);
1268 skb->protocol = htons(ETH_P_IP);
1269 iph->tot_len = htons(skb->len - skb_network_offset(skb));
1273 OVS_CB(skb)->flow = cache->flow;
1274 compute_ip_summed(skb, true);
1275 vport_receive(cache_vport, skb);
1276 sent_len += orig_len;
1280 skb->dev = rt_dst(rt).dev;
1281 err = dev_queue_xmit(skb);
1283 if (likely(net_xmit_eval(err) == 0))
1284 sent_len += orig_len;
1287 sent_len += send_frags(skb, mutable);
1293 if (unlikely(sent_len == 0))
1294 vport_record_error(vport, VPORT_E_TX_DROPPED);
1299 tnl_free_linked_skbs(skb);
1301 dst_release(unattached_dst);
1302 vport_record_error(vport, err);
1307 static int set_config(const void __user *uconfig, const struct tnl_ops *tnl_ops,
1308 const struct vport *cur_vport,
1309 struct tnl_mutable_config *mutable)
1311 const struct vport *old_vport;
1312 const struct tnl_mutable_config *old_mutable;
1314 if (copy_from_user(&mutable->port_config, uconfig, sizeof(struct tnl_port_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 char *name, const void __user *config,
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);
1361 if (IS_ERR(vport)) {
1362 err = PTR_ERR(vport);
1366 tnl_vport = tnl_vport_priv(vport);
1368 strcpy(tnl_vport->name, 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(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, const void __user *config)
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(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;