2 * Copyright (c) 2007-2011 Nicira Networks.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include <linux/uaccess.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/if_ether.h>
25 #include <linux/if_vlan.h>
26 #include <net/llc_pdu.h>
27 #include <linux/kernel.h>
28 #include <linux/jhash.h>
29 #include <linux/jiffies.h>
30 #include <linux/llc.h>
31 #include <linux/module.h>
33 #include <linux/rcupdate.h>
34 #include <linux/if_arp.h>
35 #include <linux/if_ether.h>
37 #include <linux/ipv6.h>
38 #include <linux/tcp.h>
39 #include <linux/udp.h>
40 #include <linux/icmp.h>
41 #include <linux/icmpv6.h>
42 #include <linux/rculist.h>
45 #include <net/ndisc.h>
49 static struct kmem_cache *flow_cache;
50 static unsigned int hash_seed __read_mostly;
52 static int check_header(struct sk_buff *skb, int len)
54 if (unlikely(skb->len < len))
56 if (unlikely(!pskb_may_pull(skb, len)))
61 static bool arphdr_ok(struct sk_buff *skb)
63 return pskb_may_pull(skb, skb_network_offset(skb) +
64 sizeof(struct arp_eth_header));
67 static int check_iphdr(struct sk_buff *skb)
69 unsigned int nh_ofs = skb_network_offset(skb);
73 err = check_header(skb, nh_ofs + sizeof(struct iphdr));
77 ip_len = ip_hdrlen(skb);
78 if (unlikely(ip_len < sizeof(struct iphdr) ||
79 skb->len < nh_ofs + ip_len))
82 skb_set_transport_header(skb, nh_ofs + ip_len);
86 static bool tcphdr_ok(struct sk_buff *skb)
88 int th_ofs = skb_transport_offset(skb);
91 if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
94 tcp_len = tcp_hdrlen(skb);
95 if (unlikely(tcp_len < sizeof(struct tcphdr) ||
96 skb->len < th_ofs + tcp_len))
102 static bool udphdr_ok(struct sk_buff *skb)
104 return pskb_may_pull(skb, skb_transport_offset(skb) +
105 sizeof(struct udphdr));
108 static bool icmphdr_ok(struct sk_buff *skb)
110 return pskb_may_pull(skb, skb_transport_offset(skb) +
111 sizeof(struct icmphdr));
114 u64 ovs_flow_used_time(unsigned long flow_jiffies)
116 struct timespec cur_ts;
119 ktime_get_ts(&cur_ts);
120 idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
121 cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
122 cur_ts.tv_nsec / NSEC_PER_MSEC;
124 return cur_ms - idle_ms;
127 #define SW_FLOW_KEY_OFFSET(field) \
128 (offsetof(struct sw_flow_key, field) + \
129 FIELD_SIZEOF(struct sw_flow_key, field))
132 * skip_exthdr - skip any IPv6 extension headers
133 * @skb: skbuff to parse
134 * @start: offset of first extension header
135 * @nexthdrp: Initially, points to the type of the extension header at @start.
136 * This function updates it to point to the extension header at the final
138 * @frag: Points to the @frag member in a &struct sw_flow_key. This
139 * function sets an appropriate %OVS_FRAG_TYPE_* value.
141 * This is based on ipv6_skip_exthdr() but adds the updates to *@frag.
143 * When there is more than one fragment header, this version reports whether
144 * the final fragment header that it examines is a first fragment.
146 * Returns the final payload offset, or -1 on error.
148 static int skip_exthdr(const struct sk_buff *skb, int start, u8 *nexthdrp,
151 u8 nexthdr = *nexthdrp;
153 while (ipv6_ext_hdr(nexthdr)) {
154 struct ipv6_opt_hdr _hdr, *hp;
157 if (nexthdr == NEXTHDR_NONE)
159 hp = skb_header_pointer(skb, start, sizeof(_hdr), &_hdr);
162 if (nexthdr == NEXTHDR_FRAGMENT) {
163 __be16 _frag_off, *fp;
164 fp = skb_header_pointer(skb,
165 start+offsetof(struct frag_hdr,
172 if (ntohs(*fp) & ~0x7) {
173 *frag = OVS_FRAG_TYPE_LATER;
176 *frag = OVS_FRAG_TYPE_FIRST;
178 } else if (nexthdr == NEXTHDR_AUTH)
179 hdrlen = (hp->hdrlen+2)<<2;
181 hdrlen = ipv6_optlen(hp);
183 nexthdr = hp->nexthdr;
191 static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key,
194 unsigned int nh_ofs = skb_network_offset(skb);
201 *key_lenp = SW_FLOW_KEY_OFFSET(ipv6.label);
203 err = check_header(skb, nh_ofs + sizeof(*nh));
208 nexthdr = nh->nexthdr;
209 payload_ofs = (u8 *)(nh + 1) - skb->data;
211 key->ip.proto = NEXTHDR_NONE;
212 key->ip.tos = ipv6_get_dsfield(nh);
213 key->ip.ttl = nh->hop_limit;
214 key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
215 key->ipv6.addr.src = nh->saddr;
216 key->ipv6.addr.dst = nh->daddr;
218 payload_ofs = skip_exthdr(skb, payload_ofs, &nexthdr, &key->ip.frag);
219 if (unlikely(payload_ofs < 0))
222 nh_len = payload_ofs - nh_ofs;
223 skb_set_transport_header(skb, nh_ofs + nh_len);
224 key->ip.proto = nexthdr;
228 static bool icmp6hdr_ok(struct sk_buff *skb)
230 return pskb_may_pull(skb, skb_transport_offset(skb) +
231 sizeof(struct icmp6hdr));
234 #define TCP_FLAGS_OFFSET 13
235 #define TCP_FLAG_MASK 0x3f
237 void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb)
241 if (flow->key.eth.type == htons(ETH_P_IP) &&
242 flow->key.ip.proto == IPPROTO_TCP) {
243 u8 *tcp = (u8 *)tcp_hdr(skb);
244 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
247 spin_lock(&flow->lock);
248 flow->used = jiffies;
249 flow->packet_count++;
250 flow->byte_count += skb->len;
251 flow->tcp_flags |= tcp_flags;
252 spin_unlock(&flow->lock);
255 struct sw_flow_actions *ovs_flow_actions_alloc(const struct nlattr *actions)
257 int actions_len = nla_len(actions);
258 struct sw_flow_actions *sfa;
260 /* At least DP_MAX_PORTS actions are required to be able to flood a
261 * packet to every port. Factor of 2 allows for setting VLAN tags,
263 if (actions_len > 2 * DP_MAX_PORTS * nla_total_size(4))
264 return ERR_PTR(-EINVAL);
266 sfa = kmalloc(sizeof(*sfa) + actions_len, GFP_KERNEL);
268 return ERR_PTR(-ENOMEM);
270 sfa->actions_len = actions_len;
271 memcpy(sfa->actions, nla_data(actions), actions_len);
275 struct sw_flow *ovs_flow_alloc(void)
277 struct sw_flow *flow;
279 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
281 return ERR_PTR(-ENOMEM);
283 spin_lock_init(&flow->lock);
284 atomic_set(&flow->refcnt, 1);
285 flow->sf_acts = NULL;
291 static struct hlist_head *find_bucket(struct flow_table *table, u32 hash)
293 return flex_array_get(table->buckets,
294 (hash & (table->n_buckets - 1)));
297 static struct flex_array *alloc_buckets(unsigned int n_buckets)
299 struct flex_array *buckets;
302 buckets = flex_array_alloc(sizeof(struct hlist_head *),
303 n_buckets, GFP_KERNEL);
307 err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
309 flex_array_free(buckets);
313 for (i = 0; i < n_buckets; i++)
314 INIT_HLIST_HEAD((struct hlist_head *)
315 flex_array_get(buckets, i));
320 static void free_buckets(struct flex_array *buckets)
322 flex_array_free(buckets);
325 struct flow_table *ovs_flow_tbl_alloc(int new_size)
327 struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL);
332 table->buckets = alloc_buckets(new_size);
334 if (!table->buckets) {
338 table->n_buckets = new_size;
344 static void flow_free(struct sw_flow *flow)
350 void ovs_flow_tbl_destroy(struct flow_table *table)
357 for (i = 0; i < table->n_buckets; i++) {
358 struct sw_flow *flow;
359 struct hlist_head *head = flex_array_get(table->buckets, i);
360 struct hlist_node *node, *n;
362 hlist_for_each_entry_safe(flow, node, n, head, hash_node) {
363 hlist_del_init_rcu(&flow->hash_node);
368 free_buckets(table->buckets);
372 static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
374 struct flow_table *table = container_of(rcu, struct flow_table, rcu);
376 ovs_flow_tbl_destroy(table);
379 void ovs_flow_tbl_deferred_destroy(struct flow_table *table)
384 call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb);
387 struct sw_flow *ovs_flow_tbl_next(struct flow_table *table, u32 *bucket, u32 *last)
389 struct sw_flow *flow;
390 struct hlist_head *head;
391 struct hlist_node *n;
394 while (*bucket < table->n_buckets) {
396 head = flex_array_get(table->buckets, *bucket);
397 hlist_for_each_entry_rcu(flow, n, head, hash_node) {
412 struct flow_table *ovs_flow_tbl_expand(struct flow_table *table)
414 struct flow_table *new_table;
415 int n_buckets = table->n_buckets * 2;
418 new_table = ovs_flow_tbl_alloc(n_buckets);
420 return ERR_PTR(-ENOMEM);
422 for (i = 0; i < table->n_buckets; i++) {
423 struct sw_flow *flow;
424 struct hlist_head *head;
425 struct hlist_node *n, *pos;
427 head = flex_array_get(table->buckets, i);
429 hlist_for_each_entry_safe(flow, n, pos, head, hash_node) {
430 hlist_del_init_rcu(&flow->hash_node);
431 ovs_flow_tbl_insert(new_table, flow);
438 /* RCU callback used by ovs_flow_deferred_free. */
439 static void rcu_free_flow_callback(struct rcu_head *rcu)
441 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
447 /* Schedules 'flow' to be freed after the next RCU grace period.
448 * The caller must hold rcu_read_lock for this to be sensible. */
449 void ovs_flow_deferred_free(struct sw_flow *flow)
451 call_rcu(&flow->rcu, rcu_free_flow_callback);
454 void ovs_flow_hold(struct sw_flow *flow)
456 atomic_inc(&flow->refcnt);
459 void ovs_flow_put(struct sw_flow *flow)
464 if (atomic_dec_and_test(&flow->refcnt)) {
465 kfree((struct sf_flow_acts __force *)flow->sf_acts);
466 kmem_cache_free(flow_cache, flow);
470 /* RCU callback used by ovs_flow_deferred_free_acts. */
471 static void rcu_free_acts_callback(struct rcu_head *rcu)
473 struct sw_flow_actions *sf_acts = container_of(rcu,
474 struct sw_flow_actions, rcu);
478 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
479 * The caller must hold rcu_read_lock for this to be sensible. */
480 void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
482 call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
485 static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
488 __be16 eth_type; /* ETH_P_8021Q */
491 struct qtag_prefix *qp;
493 if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
496 if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
500 qp = (struct qtag_prefix *) skb->data;
501 key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
502 __skb_pull(skb, sizeof(struct qtag_prefix));
507 static __be16 parse_ethertype(struct sk_buff *skb)
509 struct llc_snap_hdr {
510 u8 dsap; /* Always 0xAA */
511 u8 ssap; /* Always 0xAA */
516 struct llc_snap_hdr *llc;
519 proto = *(__be16 *) skb->data;
520 __skb_pull(skb, sizeof(__be16));
522 if (ntohs(proto) >= 1536)
525 if (skb->len < sizeof(struct llc_snap_hdr))
526 return htons(ETH_P_802_2);
528 if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
531 llc = (struct llc_snap_hdr *) skb->data;
532 if (llc->dsap != LLC_SAP_SNAP ||
533 llc->ssap != LLC_SAP_SNAP ||
534 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
535 return htons(ETH_P_802_2);
537 __skb_pull(skb, sizeof(struct llc_snap_hdr));
538 return llc->ethertype;
541 static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
542 int *key_lenp, int nh_len)
544 struct icmp6hdr *icmp = icmp6_hdr(skb);
548 /* The ICMPv6 type and code fields use the 16-bit transport port
549 * fields, so we need to store them in 16-bit network byte order.
551 key->ipv6.tp.src = htons(icmp->icmp6_type);
552 key->ipv6.tp.dst = htons(icmp->icmp6_code);
553 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
555 if (icmp->icmp6_code == 0 &&
556 (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
557 icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
558 int icmp_len = skb->len - skb_transport_offset(skb);
562 key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
564 /* In order to process neighbor discovery options, we need the
567 if (unlikely(icmp_len < sizeof(*nd)))
569 if (unlikely(skb_linearize(skb))) {
574 nd = (struct nd_msg *)skb_transport_header(skb);
575 key->ipv6.nd.target = nd->target;
576 key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
578 icmp_len -= sizeof(*nd);
580 while (icmp_len >= 8) {
581 struct nd_opt_hdr *nd_opt =
582 (struct nd_opt_hdr *)(nd->opt + offset);
583 int opt_len = nd_opt->nd_opt_len * 8;
585 if (unlikely(!opt_len || opt_len > icmp_len))
588 /* Store the link layer address if the appropriate
589 * option is provided. It is considered an error if
590 * the same link layer option is specified twice.
592 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
594 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
596 memcpy(key->ipv6.nd.sll,
597 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
598 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
600 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
602 memcpy(key->ipv6.nd.tll,
603 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
614 memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
615 memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
616 memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
624 * ovs_flow_extract - extracts a flow key from an Ethernet frame.
625 * @skb: sk_buff that contains the frame, with skb->data pointing to the
627 * @in_port: port number on which @skb was received.
628 * @key: output flow key
629 * @key_lenp: length of output flow key
631 * The caller must ensure that skb->len >= ETH_HLEN.
633 * Returns 0 if successful, otherwise a negative errno value.
635 * Initializes @skb header pointers as follows:
637 * - skb->mac_header: the Ethernet header.
639 * - skb->network_header: just past the Ethernet header, or just past the
640 * VLAN header, to the first byte of the Ethernet payload.
642 * - skb->transport_header: If key->dl_type is ETH_P_IP or ETH_P_IPV6
643 * on output, then just past the IP header, if one is present and
644 * of a correct length, otherwise the same as skb->network_header.
645 * For other key->dl_type values it is left untouched.
647 int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key,
651 int key_len = SW_FLOW_KEY_OFFSET(eth);
654 memset(key, 0, sizeof(*key));
656 key->phy.priority = skb->priority;
657 key->phy.tun_id = OVS_CB(skb)->tun_id;
658 key->phy.in_port = in_port;
660 skb_reset_mac_header(skb);
662 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
663 * header in the linear data area.
666 memcpy(key->eth.src, eth->h_source, ETH_ALEN);
667 memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
669 __skb_pull(skb, 2 * ETH_ALEN);
671 if (vlan_tx_tag_present(skb))
672 key->eth.tci = htons(vlan_get_tci(skb));
673 else if (eth->h_proto == htons(ETH_P_8021Q))
674 if (unlikely(parse_vlan(skb, key)))
677 key->eth.type = parse_ethertype(skb);
678 if (unlikely(key->eth.type == htons(0)))
681 skb_reset_network_header(skb);
682 __skb_push(skb, skb->data - skb_mac_header(skb));
685 if (key->eth.type == htons(ETH_P_IP)) {
689 key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
691 error = check_iphdr(skb);
692 if (unlikely(error)) {
693 if (error == -EINVAL) {
694 skb->transport_header = skb->network_header;
701 key->ipv4.addr.src = nh->saddr;
702 key->ipv4.addr.dst = nh->daddr;
704 key->ip.proto = nh->protocol;
705 key->ip.tos = nh->tos;
706 key->ip.ttl = nh->ttl;
708 offset = nh->frag_off & htons(IP_OFFSET);
710 key->ip.frag = OVS_FRAG_TYPE_LATER;
713 if (nh->frag_off & htons(IP_MF) ||
714 skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
715 key->ip.frag = OVS_FRAG_TYPE_FIRST;
717 /* Transport layer. */
718 if (key->ip.proto == IPPROTO_TCP) {
719 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
720 if (tcphdr_ok(skb)) {
721 struct tcphdr *tcp = tcp_hdr(skb);
722 key->ipv4.tp.src = tcp->source;
723 key->ipv4.tp.dst = tcp->dest;
725 } else if (key->ip.proto == IPPROTO_UDP) {
726 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
727 if (udphdr_ok(skb)) {
728 struct udphdr *udp = udp_hdr(skb);
729 key->ipv4.tp.src = udp->source;
730 key->ipv4.tp.dst = udp->dest;
732 } else if (key->ip.proto == IPPROTO_ICMP) {
733 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
734 if (icmphdr_ok(skb)) {
735 struct icmphdr *icmp = icmp_hdr(skb);
736 /* The ICMP type and code fields use the 16-bit
737 * transport port fields, so we need to store
738 * them in 16-bit network byte order. */
739 key->ipv4.tp.src = htons(icmp->type);
740 key->ipv4.tp.dst = htons(icmp->code);
744 } else if (key->eth.type == htons(ETH_P_ARP) && arphdr_ok(skb)) {
745 struct arp_eth_header *arp;
747 arp = (struct arp_eth_header *)skb_network_header(skb);
749 if (arp->ar_hrd == htons(ARPHRD_ETHER)
750 && arp->ar_pro == htons(ETH_P_IP)
751 && arp->ar_hln == ETH_ALEN
752 && arp->ar_pln == 4) {
754 /* We only match on the lower 8 bits of the opcode. */
755 if (ntohs(arp->ar_op) <= 0xff)
756 key->ip.proto = ntohs(arp->ar_op);
758 if (key->ip.proto == ARPOP_REQUEST
759 || key->ip.proto == ARPOP_REPLY) {
760 memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
761 memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
762 memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
763 memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
764 key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
767 } else if (key->eth.type == htons(ETH_P_IPV6)) {
768 int nh_len; /* IPv6 Header + Extensions */
770 nh_len = parse_ipv6hdr(skb, key, &key_len);
771 if (unlikely(nh_len < 0)) {
772 if (nh_len == -EINVAL)
773 skb->transport_header = skb->network_header;
779 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
781 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
782 key->ip.frag = OVS_FRAG_TYPE_FIRST;
784 /* Transport layer. */
785 if (key->ip.proto == NEXTHDR_TCP) {
786 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
787 if (tcphdr_ok(skb)) {
788 struct tcphdr *tcp = tcp_hdr(skb);
789 key->ipv6.tp.src = tcp->source;
790 key->ipv6.tp.dst = tcp->dest;
792 } else if (key->ip.proto == NEXTHDR_UDP) {
793 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
794 if (udphdr_ok(skb)) {
795 struct udphdr *udp = udp_hdr(skb);
796 key->ipv6.tp.src = udp->source;
797 key->ipv6.tp.dst = udp->dest;
799 } else if (key->ip.proto == NEXTHDR_ICMP) {
800 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
801 if (icmp6hdr_ok(skb)) {
802 error = parse_icmpv6(skb, key, &key_len, nh_len);
814 u32 ovs_flow_hash(const struct sw_flow_key *key, int key_len)
816 return jhash2((u32 *)key, DIV_ROUND_UP(key_len, sizeof(u32)), hash_seed);
819 struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *table,
820 struct sw_flow_key *key, int key_len)
822 struct sw_flow *flow;
823 struct hlist_node *n;
824 struct hlist_head *head;
827 hash = ovs_flow_hash(key, key_len);
829 head = find_bucket(table, hash);
830 hlist_for_each_entry_rcu(flow, n, head, hash_node) {
832 if (flow->hash == hash &&
833 !memcmp(&flow->key, key, key_len)) {
840 void ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow)
842 struct hlist_head *head;
844 head = find_bucket(table, flow->hash);
845 hlist_add_head_rcu(&flow->hash_node, head);
849 void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
851 if (!hlist_unhashed(&flow->hash_node)) {
852 hlist_del_init_rcu(&flow->hash_node);
854 BUG_ON(table->count < 0);
858 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
859 const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
860 [OVS_KEY_ATTR_ENCAP] = -1,
861 [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
862 [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
863 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
864 [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
865 [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
866 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
867 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
868 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
869 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
870 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
871 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
872 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
873 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
876 [OVS_KEY_ATTR_TUN_ID] = sizeof(__be64),
879 static int ipv4_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
880 const struct nlattr *a[], u64 *attrs)
882 const struct ovs_key_icmp *icmp_key;
883 const struct ovs_key_tcp *tcp_key;
884 const struct ovs_key_udp *udp_key;
886 switch (swkey->ip.proto) {
888 if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
890 *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
892 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
893 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
894 swkey->ipv4.tp.src = tcp_key->tcp_src;
895 swkey->ipv4.tp.dst = tcp_key->tcp_dst;
899 if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
901 *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
903 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
904 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
905 swkey->ipv4.tp.src = udp_key->udp_src;
906 swkey->ipv4.tp.dst = udp_key->udp_dst;
910 if (!(*attrs & (1 << OVS_KEY_ATTR_ICMP)))
912 *attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
914 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
915 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
916 swkey->ipv4.tp.src = htons(icmp_key->icmp_type);
917 swkey->ipv4.tp.dst = htons(icmp_key->icmp_code);
924 static int ipv6_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
925 const struct nlattr *a[], u64 *attrs)
927 const struct ovs_key_icmpv6 *icmpv6_key;
928 const struct ovs_key_tcp *tcp_key;
929 const struct ovs_key_udp *udp_key;
931 switch (swkey->ip.proto) {
933 if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
935 *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
937 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
938 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
939 swkey->ipv6.tp.src = tcp_key->tcp_src;
940 swkey->ipv6.tp.dst = tcp_key->tcp_dst;
944 if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
946 *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
948 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
949 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
950 swkey->ipv6.tp.src = udp_key->udp_src;
951 swkey->ipv6.tp.dst = udp_key->udp_dst;
955 if (!(*attrs & (1 << OVS_KEY_ATTR_ICMPV6)))
957 *attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
959 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
960 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
961 swkey->ipv6.tp.src = htons(icmpv6_key->icmpv6_type);
962 swkey->ipv6.tp.dst = htons(icmpv6_key->icmpv6_code);
964 if (swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_SOLICITATION) ||
965 swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
966 const struct ovs_key_nd *nd_key;
968 if (!(*attrs & (1 << OVS_KEY_ATTR_ND)))
970 *attrs &= ~(1 << OVS_KEY_ATTR_ND);
972 *key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
973 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
974 memcpy(&swkey->ipv6.nd.target, nd_key->nd_target,
975 sizeof(swkey->ipv6.nd.target));
976 memcpy(swkey->ipv6.nd.sll, nd_key->nd_sll, ETH_ALEN);
977 memcpy(swkey->ipv6.nd.tll, nd_key->nd_tll, ETH_ALEN);
985 static int parse_flow_nlattrs(const struct nlattr *attr,
986 const struct nlattr *a[], u64 *attrsp)
988 const struct nlattr *nla;
993 nla_for_each_nested(nla, attr, rem) {
994 u16 type = nla_type(nla);
997 if (type > OVS_KEY_ATTR_MAX || attrs & (1ULL << type))
1000 expected_len = ovs_key_lens[type];
1001 if (nla_len(nla) != expected_len && expected_len != -1)
1004 attrs |= 1ULL << type;
1015 * ovs_flow_from_nlattrs - parses Netlink attributes into a flow key.
1016 * @swkey: receives the extracted flow key.
1017 * @key_lenp: number of bytes used in @swkey.
1018 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1021 int ovs_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_lenp,
1022 const struct nlattr *attr)
1024 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1025 const struct ovs_key_ethernet *eth_key;
1030 memset(swkey, 0, sizeof(struct sw_flow_key));
1031 key_len = SW_FLOW_KEY_OFFSET(eth);
1033 err = parse_flow_nlattrs(attr, a, &attrs);
1037 /* Metadata attributes. */
1038 if (attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
1039 swkey->phy.priority = nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]);
1040 attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
1042 if (attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
1043 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1044 if (in_port >= DP_MAX_PORTS)
1046 swkey->phy.in_port = in_port;
1047 attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
1049 swkey->phy.in_port = USHRT_MAX;
1052 if (attrs & (1ULL << OVS_KEY_ATTR_TUN_ID)) {
1053 swkey->phy.tun_id = nla_get_be64(a[OVS_KEY_ATTR_TUN_ID]);
1054 attrs &= ~(1ULL << OVS_KEY_ATTR_TUN_ID);
1057 /* Data attributes. */
1058 if (!(attrs & (1 << OVS_KEY_ATTR_ETHERNET)))
1060 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
1062 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1063 memcpy(swkey->eth.src, eth_key->eth_src, ETH_ALEN);
1064 memcpy(swkey->eth.dst, eth_key->eth_dst, ETH_ALEN);
1066 if (attrs & (1u << OVS_KEY_ATTR_ETHERTYPE) &&
1067 nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q)) {
1068 const struct nlattr *encap;
1071 if (attrs != ((1 << OVS_KEY_ATTR_VLAN) |
1072 (1 << OVS_KEY_ATTR_ETHERTYPE) |
1073 (1 << OVS_KEY_ATTR_ENCAP)))
1076 encap = a[OVS_KEY_ATTR_ENCAP];
1077 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1078 if (tci & htons(VLAN_TAG_PRESENT)) {
1079 swkey->eth.tci = tci;
1081 err = parse_flow_nlattrs(encap, a, &attrs);
1085 /* Corner case for truncated 802.1Q header. */
1089 swkey->eth.type = htons(ETH_P_8021Q);
1090 *key_lenp = key_len;
1097 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
1098 swkey->eth.type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1099 if (ntohs(swkey->eth.type) < 1536)
1101 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1103 swkey->eth.type = htons(ETH_P_802_2);
1106 if (swkey->eth.type == htons(ETH_P_IP)) {
1107 const struct ovs_key_ipv4 *ipv4_key;
1109 if (!(attrs & (1 << OVS_KEY_ATTR_IPV4)))
1111 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
1113 key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
1114 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1115 if (ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX)
1117 swkey->ip.proto = ipv4_key->ipv4_proto;
1118 swkey->ip.tos = ipv4_key->ipv4_tos;
1119 swkey->ip.ttl = ipv4_key->ipv4_ttl;
1120 swkey->ip.frag = ipv4_key->ipv4_frag;
1121 swkey->ipv4.addr.src = ipv4_key->ipv4_src;
1122 swkey->ipv4.addr.dst = ipv4_key->ipv4_dst;
1124 if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1125 err = ipv4_flow_from_nlattrs(swkey, &key_len, a, &attrs);
1129 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1130 const struct ovs_key_ipv6 *ipv6_key;
1132 if (!(attrs & (1 << OVS_KEY_ATTR_IPV6)))
1134 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
1136 key_len = SW_FLOW_KEY_OFFSET(ipv6.label);
1137 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1138 if (ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX)
1140 swkey->ipv6.label = ipv6_key->ipv6_label;
1141 swkey->ip.proto = ipv6_key->ipv6_proto;
1142 swkey->ip.tos = ipv6_key->ipv6_tclass;
1143 swkey->ip.ttl = ipv6_key->ipv6_hlimit;
1144 swkey->ip.frag = ipv6_key->ipv6_frag;
1145 memcpy(&swkey->ipv6.addr.src, ipv6_key->ipv6_src,
1146 sizeof(swkey->ipv6.addr.src));
1147 memcpy(&swkey->ipv6.addr.dst, ipv6_key->ipv6_dst,
1148 sizeof(swkey->ipv6.addr.dst));
1150 if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1151 err = ipv6_flow_from_nlattrs(swkey, &key_len, a, &attrs);
1155 } else if (swkey->eth.type == htons(ETH_P_ARP)) {
1156 const struct ovs_key_arp *arp_key;
1158 if (!(attrs & (1 << OVS_KEY_ATTR_ARP)))
1160 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
1162 key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
1163 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1164 swkey->ipv4.addr.src = arp_key->arp_sip;
1165 swkey->ipv4.addr.dst = arp_key->arp_tip;
1166 if (arp_key->arp_op & htons(0xff00))
1168 swkey->ip.proto = ntohs(arp_key->arp_op);
1169 memcpy(swkey->ipv4.arp.sha, arp_key->arp_sha, ETH_ALEN);
1170 memcpy(swkey->ipv4.arp.tha, arp_key->arp_tha, ETH_ALEN);
1175 *key_lenp = key_len;
1181 * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
1182 * @in_port: receives the extracted input port.
1183 * @tun_id: receives the extracted tunnel ID.
1184 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1187 * This parses a series of Netlink attributes that form a flow key, which must
1188 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1189 * get the metadata, that is, the parts of the flow key that cannot be
1190 * extracted from the packet itself.
1192 int ovs_flow_metadata_from_nlattrs(u32 *priority, u16 *in_port, __be64 *tun_id,
1193 const struct nlattr *attr)
1195 const struct nlattr *nla;
1198 *in_port = USHRT_MAX;
1202 nla_for_each_nested(nla, attr, rem) {
1203 int type = nla_type(nla);
1205 if (type <= OVS_KEY_ATTR_MAX && ovs_key_lens[type] > 0) {
1206 if (nla_len(nla) != ovs_key_lens[type])
1210 case OVS_KEY_ATTR_PRIORITY:
1211 *priority = nla_get_u32(nla);
1214 case OVS_KEY_ATTR_TUN_ID:
1215 *tun_id = nla_get_be64(nla);
1218 case OVS_KEY_ATTR_IN_PORT:
1219 if (nla_get_u32(nla) >= DP_MAX_PORTS)
1221 *in_port = nla_get_u32(nla);
1231 int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey, struct sk_buff *skb)
1233 struct ovs_key_ethernet *eth_key;
1234 struct nlattr *nla, *encap;
1236 if (swkey->phy.priority)
1237 NLA_PUT_U32(skb, OVS_KEY_ATTR_PRIORITY, swkey->phy.priority);
1239 if (swkey->phy.tun_id != cpu_to_be64(0))
1240 NLA_PUT_BE64(skb, OVS_KEY_ATTR_TUN_ID, swkey->phy.tun_id);
1242 if (swkey->phy.in_port != USHRT_MAX)
1243 NLA_PUT_U32(skb, OVS_KEY_ATTR_IN_PORT, swkey->phy.in_port);
1245 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1247 goto nla_put_failure;
1248 eth_key = nla_data(nla);
1249 memcpy(eth_key->eth_src, swkey->eth.src, ETH_ALEN);
1250 memcpy(eth_key->eth_dst, swkey->eth.dst, ETH_ALEN);
1252 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1253 NLA_PUT_BE16(skb, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_P_8021Q));
1254 NLA_PUT_BE16(skb, OVS_KEY_ATTR_VLAN, swkey->eth.tci);
1255 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1256 if (!swkey->eth.tci)
1262 if (swkey->eth.type == htons(ETH_P_802_2))
1265 NLA_PUT_BE16(skb, OVS_KEY_ATTR_ETHERTYPE, swkey->eth.type);
1267 if (swkey->eth.type == htons(ETH_P_IP)) {
1268 struct ovs_key_ipv4 *ipv4_key;
1270 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1272 goto nla_put_failure;
1273 ipv4_key = nla_data(nla);
1274 ipv4_key->ipv4_src = swkey->ipv4.addr.src;
1275 ipv4_key->ipv4_dst = swkey->ipv4.addr.dst;
1276 ipv4_key->ipv4_proto = swkey->ip.proto;
1277 ipv4_key->ipv4_tos = swkey->ip.tos;
1278 ipv4_key->ipv4_ttl = swkey->ip.ttl;
1279 ipv4_key->ipv4_frag = swkey->ip.frag;
1280 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1281 struct ovs_key_ipv6 *ipv6_key;
1283 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1285 goto nla_put_failure;
1286 ipv6_key = nla_data(nla);
1287 memcpy(ipv6_key->ipv6_src, &swkey->ipv6.addr.src,
1288 sizeof(ipv6_key->ipv6_src));
1289 memcpy(ipv6_key->ipv6_dst, &swkey->ipv6.addr.dst,
1290 sizeof(ipv6_key->ipv6_dst));
1291 ipv6_key->ipv6_label = swkey->ipv6.label;
1292 ipv6_key->ipv6_proto = swkey->ip.proto;
1293 ipv6_key->ipv6_tclass = swkey->ip.tos;
1294 ipv6_key->ipv6_hlimit = swkey->ip.ttl;
1295 ipv6_key->ipv6_frag = swkey->ip.frag;
1296 } else if (swkey->eth.type == htons(ETH_P_ARP)) {
1297 struct ovs_key_arp *arp_key;
1299 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1301 goto nla_put_failure;
1302 arp_key = nla_data(nla);
1303 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1304 arp_key->arp_sip = swkey->ipv4.addr.src;
1305 arp_key->arp_tip = swkey->ipv4.addr.dst;
1306 arp_key->arp_op = htons(swkey->ip.proto);
1307 memcpy(arp_key->arp_sha, swkey->ipv4.arp.sha, ETH_ALEN);
1308 memcpy(arp_key->arp_tha, swkey->ipv4.arp.tha, ETH_ALEN);
1311 if ((swkey->eth.type == htons(ETH_P_IP) ||
1312 swkey->eth.type == htons(ETH_P_IPV6)) &&
1313 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1315 if (swkey->ip.proto == IPPROTO_TCP) {
1316 struct ovs_key_tcp *tcp_key;
1318 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1320 goto nla_put_failure;
1321 tcp_key = nla_data(nla);
1322 if (swkey->eth.type == htons(ETH_P_IP)) {
1323 tcp_key->tcp_src = swkey->ipv4.tp.src;
1324 tcp_key->tcp_dst = swkey->ipv4.tp.dst;
1325 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1326 tcp_key->tcp_src = swkey->ipv6.tp.src;
1327 tcp_key->tcp_dst = swkey->ipv6.tp.dst;
1329 } else if (swkey->ip.proto == IPPROTO_UDP) {
1330 struct ovs_key_udp *udp_key;
1332 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1334 goto nla_put_failure;
1335 udp_key = nla_data(nla);
1336 if (swkey->eth.type == htons(ETH_P_IP)) {
1337 udp_key->udp_src = swkey->ipv4.tp.src;
1338 udp_key->udp_dst = swkey->ipv4.tp.dst;
1339 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1340 udp_key->udp_src = swkey->ipv6.tp.src;
1341 udp_key->udp_dst = swkey->ipv6.tp.dst;
1343 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1344 swkey->ip.proto == IPPROTO_ICMP) {
1345 struct ovs_key_icmp *icmp_key;
1347 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1349 goto nla_put_failure;
1350 icmp_key = nla_data(nla);
1351 icmp_key->icmp_type = ntohs(swkey->ipv4.tp.src);
1352 icmp_key->icmp_code = ntohs(swkey->ipv4.tp.dst);
1353 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1354 swkey->ip.proto == IPPROTO_ICMPV6) {
1355 struct ovs_key_icmpv6 *icmpv6_key;
1357 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1358 sizeof(*icmpv6_key));
1360 goto nla_put_failure;
1361 icmpv6_key = nla_data(nla);
1362 icmpv6_key->icmpv6_type = ntohs(swkey->ipv6.tp.src);
1363 icmpv6_key->icmpv6_code = ntohs(swkey->ipv6.tp.dst);
1365 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1366 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1367 struct ovs_key_nd *nd_key;
1369 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1371 goto nla_put_failure;
1372 nd_key = nla_data(nla);
1373 memcpy(nd_key->nd_target, &swkey->ipv6.nd.target,
1374 sizeof(nd_key->nd_target));
1375 memcpy(nd_key->nd_sll, swkey->ipv6.nd.sll, ETH_ALEN);
1376 memcpy(nd_key->nd_tll, swkey->ipv6.nd.tll, ETH_ALEN);
1383 nla_nest_end(skb, encap);
1391 /* Initializes the flow module.
1392 * Returns zero if successful or a negative error code. */
1393 int ovs_flow_init(void)
1395 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
1397 if (flow_cache == NULL)
1400 get_random_bytes(&hash_seed, sizeof(hash_seed));
1405 /* Uninitializes the flow module. */
1406 void ovs_flow_exit(void)
1408 kmem_cache_destroy(flow_cache);