X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=datapath%2Ftunnel.c;h=bfb09ce27957e1a4aaaf59de203d8b11b1df5c61;hb=03e1125c312b223a28eb75454cc418a044df3ade;hp=8bd0ee9cb096d5d43ee076f362aaef36a7775058;hpb=a9a29d22d8f485bb90bb49ae9ddff8c3c1ab04f5;p=openvswitch diff --git a/datapath/tunnel.c b/datapath/tunnel.c index 8bd0ee9c..bfb09ce2 100644 --- a/datapath/tunnel.c +++ b/datapath/tunnel.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2007-2011 Nicira Networks. + * Copyright (c) 2007-2012 Nicira, Inc. * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public @@ -16,6 +16,8 @@ * 02110-1301, USA */ +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + #include #include #include @@ -98,6 +100,7 @@ static unsigned int key_remote_ports __read_mostly; static unsigned int key_multicast_ports __read_mostly; static unsigned int local_remote_ports __read_mostly; static unsigned int remote_ports __read_mostly; +static unsigned int null_ports __read_mostly; static unsigned int multicast_ports __read_mostly; #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36) @@ -109,7 +112,7 @@ static unsigned int multicast_ports __read_mostly; #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,1,0) static struct hh_cache *rt_hh(struct rtable *rt) { - struct neighbour *neigh = dst_get_neighbour(&rt->dst); + struct neighbour *neigh = dst_get_neighbour_noref(&rt->dst); if (!neigh || !(neigh->nud_state & NUD_CONNECTED) || !neigh->hh.hh_len) return NULL; @@ -143,7 +146,7 @@ static void free_cache(struct tnl_cache *cache) if (!cache) return; - flow_put(cache->flow); + ovs_flow_put(cache->flow); ip_rt_put(cache->rt); kfree(cache); } @@ -169,7 +172,7 @@ static void free_mutable_rtnl(struct tnl_mutable_config *mutable) ASSERT_RTNL(); if (ipv4_is_multicast(mutable->key.daddr) && mutable->mlink) { struct in_device *in_dev; - in_dev = inetdev_by_index(&init_net, mutable->mlink); + in_dev = inetdev_by_index(port_key_get_net(&mutable->key), mutable->mlink); if (in_dev) ip_mc_dec_group(in_dev, mutable->key.daddr); } @@ -216,8 +219,10 @@ static unsigned int *find_port_pool(const struct tnl_mutable_config *mutable) return &key_local_remote_ports; else if (is_multicast) return &key_multicast_ports; - else + else if (mutable->key.daddr) return &key_remote_ports; + else + return &null_ports; } } @@ -299,14 +304,15 @@ static struct vport *port_table_lookup(struct port_lookup_key *key, return NULL; } -struct vport *tnl_find_port(__be32 saddr, __be32 daddr, __be64 key, - int tunnel_type, - const struct tnl_mutable_config **mutable) +struct vport *ovs_tnl_find_port(struct net *net, __be32 saddr, __be32 daddr, + __be64 key, int tunnel_type, + const struct tnl_mutable_config **mutable) { struct port_lookup_key lookup; struct vport *vport; bool is_multicast = ipv4_is_multicast(saddr); + port_key_set_net(&lookup, net); lookup.saddr = saddr; lookup.daddr = daddr; @@ -361,12 +367,20 @@ struct vport *tnl_find_port(__be32 saddr, __be32 daddr, __be64 key, } } + if (null_ports) { + lookup.daddr = 0; + lookup.saddr = 0; + lookup.tunnel_type = tunnel_type; + vport = port_table_lookup(&lookup, mutable); + if (vport) + return vport; + } return NULL; } -static void ecn_decapsulate(struct sk_buff *skb, u8 tos) +static void ecn_decapsulate(struct sk_buff *skb) { - if (unlikely(INET_ECN_is_ce(tos))) { + if (unlikely(INET_ECN_is_ce(OVS_CB(skb)->tun_key->ipv4_tos))) { __be16 protocol = skb->protocol; skb_set_network_header(skb, ETH_HLEN); @@ -399,7 +413,7 @@ static void ecn_decapsulate(struct sk_buff *skb, u8 tos) } /** - * tnl_rcv - ingress point for generic tunnel code + * ovs_tnl_rcv - ingress point for generic tunnel code * * @vport: port this packet was received on * @skb: received packet @@ -413,7 +427,7 @@ static void ecn_decapsulate(struct sk_buff *skb, u8 tos) * - skb->csum does not include the inner Ethernet header. * - The layer pointers are undefined. */ -void tnl_rcv(struct vport *vport, struct sk_buff *skb, u8 tos) +void ovs_tnl_rcv(struct vport *vport, struct sk_buff *skb) { struct ethhdr *eh; @@ -430,7 +444,7 @@ void tnl_rcv(struct vport *vport, struct sk_buff *skb, u8 tos) skb_clear_rxhash(skb); secpath_reset(skb); - ecn_decapsulate(skb, tos); + ecn_decapsulate(skb); vlan_set_tci(skb, 0); if (unlikely(compute_ip_summed(skb, false))) { @@ -438,7 +452,7 @@ void tnl_rcv(struct vport *vport, struct sk_buff *skb, u8 tos) return; } - vport_receive(vport, skb); + ovs_vport_receive(vport, skb); } static bool check_ipv4_address(__be32 addr) @@ -538,6 +552,7 @@ static bool ipv6_should_icmp(struct sk_buff *skb) int addr_type; int payload_off = (u8 *)(old_ipv6h + 1) - skb->data; u8 nexthdr = ipv6_hdr(skb)->nexthdr; + __be16 frag_off; /* Check source address is valid. */ addr_type = ipv6_addr_type(&old_ipv6h->saddr); @@ -549,7 +564,7 @@ static bool ipv6_should_icmp(struct sk_buff *skb) return false; /* Don't respond to ICMP error messages. */ - payload_off = ipv6_skip_exthdr(skb, payload_off, &nexthdr); + payload_off = ipv6_skip_exthdr(skb, payload_off, &nexthdr, &frag_off); if (payload_off < 0) return false; @@ -587,8 +602,8 @@ static void ipv6_build_icmp(struct sk_buff *skb, struct sk_buff *nskb, + payload_length); ipv6h->nexthdr = NEXTHDR_ICMP; ipv6h->hop_limit = IPV6_DEFAULT_HOPLIMIT; - ipv6_addr_copy(&ipv6h->daddr, &old_ipv6h->saddr); - ipv6_addr_copy(&ipv6h->saddr, &old_ipv6h->daddr); + ipv6h->daddr = old_ipv6h->saddr; + ipv6h->saddr = old_ipv6h->daddr; /* ICMPv6 */ icmp6h->icmp6_type = ICMPV6_PKT_TOOBIG; @@ -607,9 +622,9 @@ static void ipv6_build_icmp(struct sk_buff *skb, struct sk_buff *nskb, } #endif /* IPv6 */ -bool tnl_frag_needed(struct vport *vport, - const struct tnl_mutable_config *mutable, - struct sk_buff *skb, unsigned int mtu, __be64 flow_key) +bool ovs_tnl_frag_needed(struct vport *vport, + const struct tnl_mutable_config *mutable, + struct sk_buff *skb, unsigned int mtu) { unsigned int eth_hdr_len = ETH_HLEN; unsigned int total_length = 0, header_length = 0, payload_length; @@ -693,23 +708,12 @@ bool tnl_frag_needed(struct vport *vport, ipv6_build_icmp(skb, nskb, mtu, payload_length); #endif - /* - * Assume that flow based keys are symmetric with respect to input - * and output and use the key that we were going to put on the - * outgoing packet for the fake received packet. If the keys are - * not symmetric then PMTUD needs to be disabled since we won't have - * any way of synthesizing packets. - */ - if ((mutable->flags & (TNL_F_IN_KEY_MATCH | TNL_F_OUT_KEY_ACTION)) == - (TNL_F_IN_KEY_MATCH | TNL_F_OUT_KEY_ACTION)) - OVS_CB(nskb)->tun_id = flow_key; - if (unlikely(compute_ip_summed(nskb, false))) { kfree_skb(nskb); return false; } - vport_receive(vport, nskb); + ovs_vport_receive(vport, nskb); return true; } @@ -717,14 +721,26 @@ bool tnl_frag_needed(struct vport *vport, static bool check_mtu(struct sk_buff *skb, struct vport *vport, const struct tnl_mutable_config *mutable, - const struct rtable *rt, __be16 *frag_offp) + const struct rtable *rt, __be16 *frag_offp, + int tunnel_hlen) { - bool df_inherit = mutable->flags & TNL_F_DF_INHERIT; - bool pmtud = mutable->flags & TNL_F_PMTUD; - __be16 frag_off = mutable->flags & TNL_F_DF_DEFAULT ? htons(IP_DF) : 0; + bool df_inherit; + bool pmtud; + __be16 frag_off; int mtu = 0; unsigned int packet_length = skb->len - ETH_HLEN; + if (OVS_CB(skb)->tun_key->ipv4_dst) { + df_inherit = false; + pmtud = false; + frag_off = OVS_CB(skb)->tun_key->tun_flags & OVS_FLOW_TNL_F_DONT_FRAGMENT ? + htons(IP_DF) : 0; + } else { + df_inherit = mutable->flags & TNL_F_DF_INHERIT; + pmtud = mutable->flags & TNL_F_PMTUD; + frag_off = mutable->flags & TNL_F_DF_DEFAULT ? htons(IP_DF) : 0; + } + /* Allow for one level of tagging in the packet length. */ if (!vlan_tx_tag_present(skb) && eth_hdr(skb)->h_proto == htons(ETH_P_8021Q)) @@ -742,7 +758,7 @@ static bool check_mtu(struct sk_buff *skb, mtu = dst_mtu(&rt_dst(rt)) - ETH_HLEN - - mutable->tunnel_hlen + - tunnel_hlen - vlan_header; } @@ -756,8 +772,7 @@ static bool check_mtu(struct sk_buff *skb, mtu = max(mtu, IP_MIN_MTU); if (packet_length > mtu && - tnl_frag_needed(vport, mutable, skb, mtu, - OVS_CB(skb)->tun_id)) + ovs_tnl_frag_needed(vport, mutable, skb, mtu)) return false; } } @@ -773,8 +788,7 @@ static bool check_mtu(struct sk_buff *skb, mtu = max(mtu, IPV6_MIN_MTU); if (packet_length > mtu && - tnl_frag_needed(vport, mutable, skb, mtu, - OVS_CB(skb)->tun_id)) + ovs_tnl_frag_needed(vport, mutable, skb, mtu)) return false; } } @@ -786,6 +800,7 @@ static bool check_mtu(struct sk_buff *skb, static void create_tunnel_header(const struct vport *vport, const struct tnl_mutable_config *mutable, + const struct ovs_key_ipv4_tunnel *tun_key, const struct rtable *rt, void *header) { struct tnl_vport *tnl_vport = tnl_vport_priv(vport); @@ -802,7 +817,7 @@ static void create_tunnel_header(const struct vport *vport, if (!iph->ttl) iph->ttl = ip4_dst_hoplimit(&rt_dst(rt)); - tnl_vport->tnl_ops->build_header(vport, mutable, iph + 1); + tnl_vport->tnl_ops->build_header(vport, mutable, tun_key, iph + 1); } static void *get_cached_header(const struct tnl_cache *cache) @@ -810,6 +825,13 @@ static void *get_cached_header(const struct tnl_cache *cache) return (void *)cache + ALIGN(sizeof(struct tnl_cache), CACHE_DATA_ALIGN); } +#ifdef HAVE_RT_GENID +static inline int rt_genid(struct net *net) +{ + return atomic_read(&net->ipv4.rt_genid); +} +#endif + static bool check_cache_valid(const struct tnl_cache *cache, const struct tnl_mutable_config *mutable) { @@ -824,13 +846,13 @@ static bool check_cache_valid(const struct tnl_cache *cache, time_before(jiffies, cache->expiration) && #endif #ifdef HAVE_RT_GENID - atomic_read(&init_net.ipv4.rt_genid) == cache->rt->rt_genid && + rt_genid(dev_net(rt_dst(cache->rt).dev)) == cache->rt->rt_genid && #endif #ifdef HAVE_HH_SEQ hh->hh_lock.sequence == cache->hh_seq && #endif mutable->seq == cache->mutable_seq && - (!is_internal_dev(rt_dst(cache->rt).dev) || + (!ovs_is_internal_dev(rt_dst(cache->rt).dev) || (cache->flow && !cache->flow->dead)); } @@ -857,7 +879,7 @@ static void cache_cleaner(struct work_struct *work) for (i = 0; i < PORT_TABLE_SIZE; i++) { struct hlist_node *n; struct hlist_head *bucket; - struct tnl_vport *tnl_vport; + struct tnl_vport *tnl_vport; bucket = &port_table[i]; hlist_for_each_entry_rcu(tnl_vport, n, bucket, hash_node) @@ -896,14 +918,22 @@ static struct tnl_cache *build_cache(struct vport *vport, struct rtable *rt) { struct tnl_vport *tnl_vport = tnl_vport_priv(vport); + static const struct ovs_key_ipv4_tunnel tun_key; struct tnl_cache *cache; void *cache_data; int cache_len; struct hh_cache *hh; + int tunnel_hlen; if (!(mutable->flags & TNL_F_HDR_CACHE)) return NULL; + tunnel_hlen = tnl_vport->tnl_ops->hdr_len(mutable, &tun_key); + if (tunnel_hlen < 0) + return NULL; + + tunnel_hlen += sizeof(struct iphdr); + /* * If there is no entry in the ARP cache or if this device does not * support hard header caching just fall back to the IP stack. @@ -926,7 +956,7 @@ static struct tnl_cache *build_cache(struct vport *vport, else cache = NULL; - cache_len = LL_RESERVED_SPACE(rt_dst(rt).dev) + mutable->tunnel_hlen; + cache_len = LL_RESERVED_SPACE(rt_dst(rt).dev) + tunnel_hlen; cache = kzalloc(ALIGN(sizeof(struct tnl_cache), CACHE_DATA_ALIGN) + cache_len, GFP_ATOMIC); @@ -935,9 +965,9 @@ static struct tnl_cache *build_cache(struct vport *vport, create_eth_hdr(cache, hh); cache_data = get_cached_header(cache) + cache->hh_len; - cache->len = cache->hh_len + mutable->tunnel_hlen; + cache->len = cache->hh_len + tunnel_hlen; - create_tunnel_header(vport, mutable, rt, cache_data); + create_tunnel_header(vport, mutable, &tun_key, rt, cache_data); cache->mutable_seq = mutable->seq; cache->rt = rt; @@ -945,7 +975,7 @@ static struct tnl_cache *build_cache(struct vport *vport, cache->expiration = jiffies + tnl_vport->cache_exp_interval; #endif - if (is_internal_dev(rt_dst(rt).dev)) { + if (ovs_is_internal_dev(rt_dst(rt).dev)) { struct sw_flow_key flow_key; struct vport *dst_vport; struct sk_buff *skb; @@ -953,7 +983,7 @@ static struct tnl_cache *build_cache(struct vport *vport, int flow_key_len; struct sw_flow *flow; - dst_vport = internal_dev_get_vport(rt_dst(rt).dev); + dst_vport = ovs_internal_dev_get_vport(rt_dst(rt).dev); if (!dst_vport) goto done; @@ -964,18 +994,18 @@ static struct tnl_cache *build_cache(struct vport *vport, __skb_put(skb, cache->len); memcpy(skb->data, get_cached_header(cache), cache->len); - err = flow_extract(skb, dst_vport->port_no, &flow_key, - &flow_key_len); + err = ovs_flow_extract(skb, dst_vport->port_no, &flow_key, + &flow_key_len); consume_skb(skb); if (err) goto done; - flow = flow_tbl_lookup(rcu_dereference(dst_vport->dp->table), - &flow_key, flow_key_len); + flow = ovs_flow_tbl_lookup(rcu_dereference(dst_vport->dp->table), + &flow_key, flow_key_len); if (flow) { cache->flow = flow; - flow_hold(flow); + ovs_flow_hold(flow); } } @@ -989,33 +1019,38 @@ unlock: } static struct rtable *__find_route(const struct tnl_mutable_config *mutable, - u8 ipproto, u8 tos) + __be32 saddr, __be32 daddr, u8 ipproto, + u8 tos) { + /* Tunnel configuration keeps DSCP part of TOS bits, But Linux + * router expect RT_TOS bits only. */ + #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39) struct flowi fl = { .nl_u = { .ip4_u = { - .daddr = mutable->key.daddr, - .saddr = mutable->key.saddr, - .tos = tos } }, - .proto = ipproto }; + .daddr = daddr, + .saddr = saddr, + .tos = RT_TOS(tos) } }, + .proto = ipproto }; struct rtable *rt; - if (unlikely(ip_route_output_key(&init_net, &rt, &fl))) + if (unlikely(ip_route_output_key(port_key_get_net(&mutable->key), &rt, &fl))) return ERR_PTR(-EADDRNOTAVAIL); return rt; #else - struct flowi4 fl = { .daddr = mutable->key.daddr, - .saddr = mutable->key.saddr, - .flowi4_tos = tos, + struct flowi4 fl = { .daddr = daddr, + .saddr = saddr, + .flowi4_tos = RT_TOS(tos), .flowi4_proto = ipproto }; - return ip_route_output_key(&init_net, &fl); + return ip_route_output_key(port_key_get_net(&mutable->key), &fl); #endif } static struct rtable *find_route(struct vport *vport, const struct tnl_mutable_config *mutable, - u8 tos, struct tnl_cache **cache) + __be32 saddr, __be32 daddr, u8 tos, + struct tnl_cache **cache) { struct tnl_vport *tnl_vport = tnl_vport_priv(vport); struct tnl_cache *cur_cache = rcu_dereference(tnl_vport->cache); @@ -1023,18 +1058,18 @@ static struct rtable *find_route(struct vport *vport, *cache = NULL; tos = RT_TOS(tos); - if (likely(tos == mutable->tos && - check_cache_valid(cur_cache, mutable))) { + if (tos == RT_TOS(mutable->tos) && + check_cache_valid(cur_cache, mutable)) { *cache = cur_cache; return cur_cache->rt; } else { struct rtable *rt; - rt = __find_route(mutable, tnl_vport->tnl_ops->ipproto, tos); + rt = __find_route(mutable, saddr, daddr, + tnl_vport->tnl_ops->ipproto, tos); if (IS_ERR(rt)) return NULL; - - if (likely(tos == mutable->tos)) + if (likely(tos == RT_TOS(mutable->tos))) *cache = build_cache(vport, mutable, rt); return rt; @@ -1062,13 +1097,14 @@ static bool need_linearize(const struct sk_buff *skb) static struct sk_buff *handle_offloads(struct sk_buff *skb, const struct tnl_mutable_config *mutable, - const struct rtable *rt) + const struct rtable *rt, + int tunnel_hlen) { int min_headroom; int err; min_headroom = LL_RESERVED_SPACE(rt_dst(rt).dev) + rt_dst(rt).header_len - + mutable->tunnel_hlen + + tunnel_hlen + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0); if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) { @@ -1123,14 +1159,14 @@ error: } static int send_frags(struct sk_buff *skb, - const struct tnl_mutable_config *mutable) + int tunnel_hlen) { int sent_len; sent_len = 0; while (skb) { struct sk_buff *next = skb->next; - int frag_len = skb->len - mutable->tunnel_hlen; + int frag_len = skb->len - tunnel_hlen; int err; skb->next = NULL; @@ -1151,23 +1187,25 @@ free_frags: * dropped so just free the rest. This may help improve the congestion * that caused the first packet to be dropped. */ - tnl_free_linked_skbs(skb); + ovs_tnl_free_linked_skbs(skb); return sent_len; } -int tnl_send(struct vport *vport, struct sk_buff *skb) +int ovs_tnl_send(struct vport *vport, struct sk_buff *skb) { struct tnl_vport *tnl_vport = tnl_vport_priv(vport); const struct tnl_mutable_config *mutable = rcu_dereference(tnl_vport->mutable); - enum vport_err_type err = VPORT_E_TX_ERROR; struct rtable *rt; struct dst_entry *unattached_dst = NULL; struct tnl_cache *cache; + struct ovs_key_ipv4_tunnel tun_key; int sent_len = 0; + int tunnel_hlen; __be16 frag_off = 0; + __be32 daddr; + __be32 saddr; u8 ttl; - u8 inner_tos; u8 tos; /* Validate the protocol headers before we try to use them. */ @@ -1193,25 +1231,70 @@ int tnl_send(struct vport *vport, struct sk_buff *skb) } #endif - /* ToS */ - if (skb->protocol == htons(ETH_P_IP)) - inner_tos = ip_hdr(skb)->tos; + /* If OVS_CB(skb)->tun_key is NULL, point it at the local tun_key here, + * and zero it out. + */ + if (!OVS_CB(skb)->tun_key) { + memset(&tun_key, 0, sizeof(tun_key)); + OVS_CB(skb)->tun_key = &tun_key; + } + + tunnel_hlen = tnl_vport->tnl_ops->hdr_len(mutable, OVS_CB(skb)->tun_key); + if (unlikely(tunnel_hlen < 0)) { + err = VPORT_E_TX_DROPPED; + goto error_free; + } + tunnel_hlen += sizeof(struct iphdr); + + if (OVS_CB(skb)->tun_key->ipv4_dst) { + daddr = OVS_CB(skb)->tun_key->ipv4_dst; + saddr = OVS_CB(skb)->tun_key->ipv4_src; + tos = OVS_CB(skb)->tun_key->ipv4_tos; + ttl = OVS_CB(skb)->tun_key->ipv4_ttl; + } else { + u8 inner_tos; + daddr = mutable->key.daddr; + saddr = mutable->key.saddr; + + if (unlikely(!daddr)) { + /* Trying to sent packet from Null-port without + * tunnel info? Drop this packet. */ + err = VPORT_E_TX_DROPPED; + goto error_free; + } + + /* ToS */ + if (skb->protocol == htons(ETH_P_IP)) + inner_tos = ip_hdr(skb)->tos; #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) - else if (skb->protocol == htons(ETH_P_IPV6)) - inner_tos = ipv6_get_dsfield(ipv6_hdr(skb)); + else if (skb->protocol == htons(ETH_P_IPV6)) + inner_tos = ipv6_get_dsfield(ipv6_hdr(skb)); #endif - else - inner_tos = 0; + else + inner_tos = 0; - if (mutable->flags & TNL_F_TOS_INHERIT) - tos = inner_tos; - else - tos = mutable->tos; + if (mutable->flags & TNL_F_TOS_INHERIT) + tos = inner_tos; + else + tos = mutable->tos; + + tos = INET_ECN_encapsulate(tos, inner_tos); - tos = INET_ECN_encapsulate(tos, inner_tos); + /* TTL */ + ttl = mutable->ttl; + if (mutable->flags & TNL_F_TTL_INHERIT) { + if (skb->protocol == htons(ETH_P_IP)) + ttl = ip_hdr(skb)->ttl; +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) + else if (skb->protocol == htons(ETH_P_IPV6)) + ttl = ipv6_hdr(skb)->hop_limit; +#endif + } + + } /* Route lookup */ - rt = find_route(vport, mutable, tos, &cache); + rt = find_route(vport, mutable, saddr, daddr, tos, &cache); if (unlikely(!rt)) goto error_free; if (unlikely(!cache)) @@ -1224,12 +1307,12 @@ int tnl_send(struct vport *vport, struct sk_buff *skb) skb_clear_rxhash(skb); /* Offloading */ - skb = handle_offloads(skb, mutable, rt); + skb = handle_offloads(skb, mutable, rt, tunnel_hlen); if (IS_ERR(skb)) goto error; /* MTU */ - if (unlikely(!check_mtu(skb, vport, mutable, rt, &frag_off))) { + if (unlikely(!check_mtu(skb, vport, mutable, rt, &frag_off, tunnel_hlen))) { err = VPORT_E_TX_DROPPED; goto error_free; } @@ -1238,25 +1321,19 @@ int tnl_send(struct vport *vport, struct sk_buff *skb) * If we are over the MTU, allow the IP stack to handle fragmentation. * Fragmentation is a slow path anyways. */ - if (unlikely(skb->len + mutable->tunnel_hlen > dst_mtu(&rt_dst(rt)) && + if (unlikely(skb->len + tunnel_hlen > dst_mtu(&rt_dst(rt)) && cache)) { unattached_dst = &rt_dst(rt); dst_hold(unattached_dst); cache = NULL; } - /* TTL */ - ttl = mutable->ttl; - if (!ttl) - ttl = ip4_dst_hoplimit(&rt_dst(rt)); - - if (mutable->flags & TNL_F_TTL_INHERIT) { - if (skb->protocol == htons(ETH_P_IP)) - ttl = ip_hdr(skb)->ttl; -#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) - else if (skb->protocol == htons(ETH_P_IPV6)) - ttl = ipv6_hdr(skb)->hop_limit; -#endif + /* TTL Fixup. */ + if (!OVS_CB(skb)->tun_key->ipv4_dst) { + if (!(mutable->flags & TNL_F_TTL_INHERIT)) { + if (!ttl) + ttl = ip4_dst_hoplimit(&rt_dst(rt)); + } } while (skb) { @@ -1274,8 +1351,8 @@ int tnl_send(struct vport *vport, struct sk_buff *skb) skb_set_network_header(skb, cache->hh_len); } else { - skb_push(skb, mutable->tunnel_hlen); - create_tunnel_header(vport, mutable, rt, skb->data); + skb_push(skb, tunnel_hlen); + create_tunnel_header(vport, mutable, OVS_CB(skb)->tun_key, rt, skb->data); skb_reset_network_header(skb); if (next_skb) @@ -1294,7 +1371,7 @@ int tnl_send(struct vport *vport, struct sk_buff *skb) ip_select_ident(iph, &rt_dst(rt), NULL); skb = tnl_vport->tnl_ops->update_header(vport, mutable, - &rt_dst(rt), skb); + &rt_dst(rt), skb, tunnel_hlen); if (unlikely(!skb)) goto next; @@ -1302,7 +1379,7 @@ int tnl_send(struct vport *vport, struct sk_buff *skb) int orig_len = skb->len - cache->len; struct vport *cache_vport; - cache_vport = internal_dev_get_vport(rt_dst(rt).dev); + cache_vport = ovs_internal_dev_get_vport(rt_dst(rt).dev); skb->protocol = htons(ETH_P_IP); iph = ip_hdr(skb); iph->tot_len = htons(skb->len - skb_network_offset(skb)); @@ -1315,7 +1392,7 @@ int tnl_send(struct vport *vport, struct sk_buff *skb) } OVS_CB(skb)->flow = cache->flow; - vport_receive(cache_vport, skb); + ovs_vport_receive(cache_vport, skb); sent_len += orig_len; } else { int xmit_err; @@ -1327,21 +1404,21 @@ int tnl_send(struct vport *vport, struct sk_buff *skb) sent_len += orig_len; } } else - sent_len += send_frags(skb, mutable); + sent_len += send_frags(skb, tunnel_hlen); next: skb = next_skb; } if (unlikely(sent_len == 0)) - vport_record_error(vport, VPORT_E_TX_DROPPED); + ovs_vport_record_error(vport, VPORT_E_TX_DROPPED); goto out; error_free: - tnl_free_linked_skbs(skb); + ovs_tnl_free_linked_skbs(skb); error: - vport_record_error(vport, err); + ovs_vport_record_error(vport, err); out: dst_release(unattached_dst); return sent_len; @@ -1359,7 +1436,8 @@ static const struct nla_policy tnl_policy[OVS_TUNNEL_ATTR_MAX + 1] = { /* Sets OVS_TUNNEL_ATTR_* fields in 'mutable', which must initially be * zeroed. */ -static int tnl_set_config(struct nlattr *options, const struct tnl_ops *tnl_ops, +static int tnl_set_config(struct net *net, struct nlattr *options, + const struct tnl_ops *tnl_ops, const struct vport *cur_vport, struct tnl_mutable_config *mutable) { @@ -1368,8 +1446,10 @@ static int tnl_set_config(struct nlattr *options, const struct tnl_ops *tnl_ops, struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1]; int err; + port_key_set_net(&mutable->key, net); + mutable->key.tunnel_type = tnl_ops->tunnel_type; if (!options) - return -EINVAL; + goto out; err = nla_parse_nested(a, OVS_TUNNEL_ATTR_MAX, options, tnl_policy); if (err) @@ -1379,8 +1459,8 @@ static int tnl_set_config(struct nlattr *options, const struct tnl_ops *tnl_ops, return -EINVAL; mutable->flags = nla_get_u32(a[OVS_TUNNEL_ATTR_FLAGS]) & TNL_F_PUBLIC; - mutable->key.daddr = nla_get_be32(a[OVS_TUNNEL_ATTR_DST_IPV4]); + if (a[OVS_TUNNEL_ATTR_SRC_IPV4]) { if (ipv4_is_multicast(mutable->key.daddr)) return -EINVAL; @@ -1389,14 +1469,14 @@ static int tnl_set_config(struct nlattr *options, const struct tnl_ops *tnl_ops, if (a[OVS_TUNNEL_ATTR_TOS]) { mutable->tos = nla_get_u8(a[OVS_TUNNEL_ATTR_TOS]); - if (mutable->tos != RT_TOS(mutable->tos)) + /* Reject ToS config with ECN bits set. */ + if (mutable->tos & INET_ECN_MASK) return -EINVAL; } if (a[OVS_TUNNEL_ATTR_TTL]) mutable->ttl = nla_get_u8(a[OVS_TUNNEL_ATTR_TTL]); - mutable->key.tunnel_type = tnl_ops->tunnel_type; if (!a[OVS_TUNNEL_ATTR_IN_KEY]) { mutable->key.tunnel_type |= TNL_T_KEY_MATCH; mutable->flags |= TNL_F_IN_KEY_MATCH; @@ -1410,22 +1490,13 @@ static int tnl_set_config(struct nlattr *options, const struct tnl_ops *tnl_ops, else mutable->out_key = nla_get_be64(a[OVS_TUNNEL_ATTR_OUT_KEY]); - mutable->tunnel_hlen = tnl_ops->hdr_len(mutable); - if (mutable->tunnel_hlen < 0) - return mutable->tunnel_hlen; - - mutable->tunnel_hlen += sizeof(struct iphdr); - - old_vport = port_table_lookup(&mutable->key, &old_mutable); - if (old_vport && old_vport != cur_vport) - return -EEXIST; - mutable->mlink = 0; if (ipv4_is_multicast(mutable->key.daddr)) { struct net_device *dev; struct rtable *rt; - rt = __find_route(mutable, tnl_ops->ipproto, mutable->tos); + rt = __find_route(mutable, mutable->key.saddr, mutable->key.daddr, + tnl_ops->ipproto, mutable->tos); if (IS_ERR(rt)) return -EADDRNOTAVAIL; dev = rt_dst(rt).dev; @@ -1436,12 +1507,17 @@ static int tnl_set_config(struct nlattr *options, const struct tnl_ops *tnl_ops, ip_mc_inc_group(__in_dev_get_rtnl(dev), mutable->key.daddr); } +out: + old_vport = port_table_lookup(&mutable->key, &old_mutable); + if (old_vport && old_vport != cur_vport) + return -EEXIST; + return 0; } -struct vport *tnl_create(const struct vport_parms *parms, - const struct vport_ops *vport_ops, - const struct tnl_ops *tnl_ops) +struct vport *ovs_tnl_create(const struct vport_parms *parms, + const struct vport_ops *vport_ops, + const struct tnl_ops *tnl_ops) { struct vport *vport; struct tnl_vport *tnl_vport; @@ -1449,7 +1525,7 @@ struct vport *tnl_create(const struct vport_parms *parms, int initial_frag_id; int err; - vport = vport_alloc(sizeof(struct tnl_vport), vport_ops, parms); + vport = ovs_vport_alloc(sizeof(struct tnl_vport), vport_ops, parms); if (IS_ERR(vport)) { err = PTR_ERR(vport); goto error; @@ -1471,7 +1547,8 @@ struct vport *tnl_create(const struct vport_parms *parms, get_random_bytes(&initial_frag_id, sizeof(int)); atomic_set(&tnl_vport->frag_id, initial_frag_id); - err = tnl_set_config(parms->options, tnl_ops, NULL, mutable); + err = tnl_set_config(ovs_dp_get_net(parms->dp), parms->options, tnl_ops, + NULL, mutable); if (err) goto error_free_mutable; @@ -1491,18 +1568,22 @@ error_free_mutable: free_mutable_rtnl(mutable); kfree(mutable); error_free_vport: - vport_free(vport); + ovs_vport_free(vport); error: return ERR_PTR(err); } -int tnl_set_options(struct vport *vport, struct nlattr *options) +int ovs_tnl_set_options(struct vport *vport, struct nlattr *options) { struct tnl_vport *tnl_vport = tnl_vport_priv(vport); const struct tnl_mutable_config *old_mutable; struct tnl_mutable_config *mutable; int err; + old_mutable = rtnl_dereference(tnl_vport->mutable); + if (!old_mutable->key.daddr) + return -EINVAL; + mutable = kzalloc(sizeof(struct tnl_mutable_config), GFP_KERNEL); if (!mutable) { err = -ENOMEM; @@ -1510,12 +1591,12 @@ int tnl_set_options(struct vport *vport, struct nlattr *options) } /* Copy fields whose values should be retained. */ - old_mutable = rtnl_dereference(tnl_vport->mutable); mutable->seq = old_mutable->seq + 1; memcpy(mutable->eth_addr, old_mutable->eth_addr, ETH_ALEN); /* Parse the others configured by userspace. */ - err = tnl_set_config(options, tnl_vport->tnl_ops, vport, mutable); + err = tnl_set_config(ovs_dp_get_net(vport->dp), options, tnl_vport->tnl_ops, + vport, mutable); if (err) goto error_free; @@ -1533,24 +1614,29 @@ error: return err; } -int tnl_get_options(const struct vport *vport, struct sk_buff *skb) +int ovs_tnl_get_options(const struct vport *vport, struct sk_buff *skb) { const struct tnl_vport *tnl_vport = tnl_vport_priv(vport); const struct tnl_mutable_config *mutable = rcu_dereference_rtnl(tnl_vport->mutable); - NLA_PUT_U32(skb, OVS_TUNNEL_ATTR_FLAGS, mutable->flags & TNL_F_PUBLIC); - NLA_PUT_BE32(skb, OVS_TUNNEL_ATTR_DST_IPV4, mutable->key.daddr); - - if (!(mutable->flags & TNL_F_IN_KEY_MATCH)) - NLA_PUT_BE64(skb, OVS_TUNNEL_ATTR_IN_KEY, mutable->key.in_key); - if (!(mutable->flags & TNL_F_OUT_KEY_ACTION)) - NLA_PUT_BE64(skb, OVS_TUNNEL_ATTR_OUT_KEY, mutable->out_key); - if (mutable->key.saddr) - NLA_PUT_BE32(skb, OVS_TUNNEL_ATTR_SRC_IPV4, mutable->key.saddr); - if (mutable->tos) - NLA_PUT_U8(skb, OVS_TUNNEL_ATTR_TOS, mutable->tos); - if (mutable->ttl) - NLA_PUT_U8(skb, OVS_TUNNEL_ATTR_TTL, mutable->ttl); + if (nla_put_u32(skb, OVS_TUNNEL_ATTR_FLAGS, + mutable->flags & TNL_F_PUBLIC) || + nla_put_be32(skb, OVS_TUNNEL_ATTR_DST_IPV4, mutable->key.daddr)) + goto nla_put_failure; + + if (!(mutable->flags & TNL_F_IN_KEY_MATCH) && + nla_put_be64(skb, OVS_TUNNEL_ATTR_IN_KEY, mutable->key.in_key)) + goto nla_put_failure; + if (!(mutable->flags & TNL_F_OUT_KEY_ACTION) && + nla_put_be64(skb, OVS_TUNNEL_ATTR_OUT_KEY, mutable->out_key)) + goto nla_put_failure; + if (mutable->key.saddr && + nla_put_be32(skb, OVS_TUNNEL_ATTR_SRC_IPV4, mutable->key.saddr)) + goto nla_put_failure; + if (mutable->tos && nla_put_u8(skb, OVS_TUNNEL_ATTR_TOS, mutable->tos)) + goto nla_put_failure; + if (mutable->ttl && nla_put_u8(skb, OVS_TUNNEL_ATTR_TTL, mutable->ttl)) + goto nla_put_failure; return 0; @@ -1565,10 +1651,10 @@ static void free_port_rcu(struct rcu_head *rcu) free_cache((struct tnl_cache __force *)tnl_vport->cache); kfree((struct tnl_mutable __force *)tnl_vport->mutable); - vport_free(tnl_vport_to_vport(tnl_vport)); + ovs_vport_free(tnl_vport_to_vport(tnl_vport)); } -void tnl_destroy(struct vport *vport) +void ovs_tnl_destroy(struct vport *vport) { struct tnl_vport *tnl_vport = tnl_vport_priv(vport); struct tnl_mutable_config *mutable; @@ -1579,7 +1665,7 @@ void tnl_destroy(struct vport *vport) call_rcu(&tnl_vport->rcu, free_port_rcu); } -int tnl_set_addr(struct vport *vport, const unsigned char *addr) +int ovs_tnl_set_addr(struct vport *vport, const unsigned char *addr) { struct tnl_vport *tnl_vport = tnl_vport_priv(vport); struct tnl_mutable_config *old_mutable, *mutable; @@ -1597,19 +1683,19 @@ int tnl_set_addr(struct vport *vport, const unsigned char *addr) return 0; } -const char *tnl_get_name(const struct vport *vport) +const char *ovs_tnl_get_name(const struct vport *vport) { const struct tnl_vport *tnl_vport = tnl_vport_priv(vport); return tnl_vport->name; } -const unsigned char *tnl_get_addr(const struct vport *vport) +const unsigned char *ovs_tnl_get_addr(const struct vport *vport) { const struct tnl_vport *tnl_vport = tnl_vport_priv(vport); return rcu_dereference_rtnl(tnl_vport->mutable)->eth_addr; } -void tnl_free_linked_skbs(struct sk_buff *skb) +void ovs_tnl_free_linked_skbs(struct sk_buff *skb) { while (skb) { struct sk_buff *next = skb->next; @@ -1618,12 +1704,12 @@ void tnl_free_linked_skbs(struct sk_buff *skb) } } -int tnl_init(void) +int ovs_tnl_init(void) { int i; port_table = kmalloc(PORT_TABLE_SIZE * sizeof(struct hlist_head *), - GFP_KERNEL); + GFP_KERNEL); if (!port_table) return -ENOMEM; @@ -1633,21 +1719,7 @@ int tnl_init(void) return 0; } -void tnl_exit(void) +void ovs_tnl_exit(void) { - int i; - - for (i = 0; i < PORT_TABLE_SIZE; i++) { - struct tnl_vport *tnl_vport; - struct hlist_head *hash_head; - struct hlist_node *n; - - hash_head = &port_table[i]; - hlist_for_each_entry(tnl_vport, n, hash_head, hash_node) { - BUG(); - goto out; - } - } -out: kfree(port_table); }