2 * Copyright (c) 2007-2013 Nicira, Inc.
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>
36 #include <linux/ipv6.h>
37 #include <linux/sctp.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;
51 static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask,
52 struct sw_flow_key_range *range, u8 val);
54 static void update_range__(struct sw_flow_match *match,
55 size_t offset, size_t size, bool is_mask)
57 struct sw_flow_key_range *range = NULL;
58 size_t start = offset;
59 size_t end = offset + size;
62 range = &match->range;
64 range = &match->mask->range;
69 if (range->start == range->end) {
75 if (range->start > start)
82 #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
84 update_range__(match, offsetof(struct sw_flow_key, field), \
85 sizeof((match)->key->field), is_mask); \
88 (match)->mask->key.field = value; \
90 (match)->key->field = value; \
94 #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
96 update_range__(match, offsetof(struct sw_flow_key, field), \
100 memcpy(&(match)->mask->key.field, value_p, len);\
102 memcpy(&(match)->key->field, value_p, len); \
106 void ovs_match_init(struct sw_flow_match *match,
107 struct sw_flow_key *key,
108 struct sw_flow_mask *mask)
110 memset(match, 0, sizeof(*match));
114 memset(key, 0, sizeof(*key));
117 memset(&mask->key, 0, sizeof(mask->key));
118 mask->range.start = mask->range.end = 0;
122 static bool ovs_match_validate(const struct sw_flow_match *match,
123 u64 key_attrs, u64 mask_attrs)
125 u64 key_expected = 1ULL << OVS_KEY_ATTR_ETHERNET;
126 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
128 /* The following mask attributes allowed only if they
129 * pass the validation tests. */
130 mask_allowed &= ~((1ULL << OVS_KEY_ATTR_IPV4)
131 | (1ULL << OVS_KEY_ATTR_IPV6)
132 | (1ULL << OVS_KEY_ATTR_TCP)
133 | (1ULL << OVS_KEY_ATTR_UDP)
134 | (1ULL << OVS_KEY_ATTR_SCTP)
135 | (1ULL << OVS_KEY_ATTR_ICMP)
136 | (1ULL << OVS_KEY_ATTR_ICMPV6)
137 | (1ULL << OVS_KEY_ATTR_ARP)
138 | (1ULL << OVS_KEY_ATTR_ND));
140 /* Always allowed mask fields. */
141 mask_allowed |= ((1ULL << OVS_KEY_ATTR_TUNNEL)
142 | (1ULL << OVS_KEY_ATTR_IN_PORT)
143 | (1ULL << OVS_KEY_ATTR_ETHERTYPE));
145 /* Check key attributes. */
146 if (match->key->eth.type == htons(ETH_P_ARP)
147 || match->key->eth.type == htons(ETH_P_RARP)) {
148 key_expected |= 1ULL << OVS_KEY_ATTR_ARP;
149 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
150 mask_allowed |= 1ULL << OVS_KEY_ATTR_ARP;
153 if (match->key->eth.type == htons(ETH_P_IP)) {
154 key_expected |= 1ULL << OVS_KEY_ATTR_IPV4;
155 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
156 mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV4;
158 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
159 if (match->key->ip.proto == IPPROTO_UDP) {
160 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
161 if (match->mask && (match->mask->key.ip.proto == 0xff))
162 mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
165 if (match->key->ip.proto == IPPROTO_SCTP) {
166 key_expected |= 1ULL << OVS_KEY_ATTR_SCTP;
167 if (match->mask && (match->mask->key.ip.proto == 0xff))
168 mask_allowed |= 1ULL << OVS_KEY_ATTR_SCTP;
171 if (match->key->ip.proto == IPPROTO_TCP) {
172 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
173 if (match->mask && (match->mask->key.ip.proto == 0xff))
174 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
177 if (match->key->ip.proto == IPPROTO_ICMP) {
178 key_expected |= 1ULL << OVS_KEY_ATTR_ICMP;
179 if (match->mask && (match->mask->key.ip.proto == 0xff))
180 mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMP;
185 if (match->key->eth.type == htons(ETH_P_IPV6)) {
186 key_expected |= 1ULL << OVS_KEY_ATTR_IPV6;
187 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
188 mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV6;
190 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
191 if (match->key->ip.proto == IPPROTO_UDP) {
192 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
193 if (match->mask && (match->mask->key.ip.proto == 0xff))
194 mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
197 if (match->key->ip.proto == IPPROTO_SCTP) {
198 key_expected |= 1ULL << OVS_KEY_ATTR_SCTP;
199 if (match->mask && (match->mask->key.ip.proto == 0xff))
200 mask_allowed |= 1ULL << OVS_KEY_ATTR_SCTP;
203 if (match->key->ip.proto == IPPROTO_TCP) {
204 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
205 if (match->mask && (match->mask->key.ip.proto == 0xff))
206 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
209 if (match->key->ip.proto == IPPROTO_ICMPV6) {
210 key_expected |= 1ULL << OVS_KEY_ATTR_ICMPV6;
211 if (match->mask && (match->mask->key.ip.proto == 0xff))
212 mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMPV6;
214 if (match->key->ipv6.tp.src ==
215 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
216 match->key->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
217 key_expected |= 1ULL << OVS_KEY_ATTR_ND;
218 if (match->mask && (match->mask->key.ipv6.tp.src == htons(0xffff)))
219 mask_allowed |= 1ULL << OVS_KEY_ATTR_ND;
225 if ((key_attrs & key_expected) != key_expected) {
226 /* Key attributes check failed. */
227 OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n",
228 key_attrs, key_expected);
232 if ((mask_attrs & mask_allowed) != mask_attrs) {
233 /* Mask attributes check failed. */
234 OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n",
235 mask_attrs, mask_allowed);
242 static int check_header(struct sk_buff *skb, int len)
244 if (unlikely(skb->len < len))
246 if (unlikely(!pskb_may_pull(skb, len)))
251 static bool arphdr_ok(struct sk_buff *skb)
253 return pskb_may_pull(skb, skb_network_offset(skb) +
254 sizeof(struct arp_eth_header));
257 static int check_iphdr(struct sk_buff *skb)
259 unsigned int nh_ofs = skb_network_offset(skb);
263 err = check_header(skb, nh_ofs + sizeof(struct iphdr));
267 ip_len = ip_hdrlen(skb);
268 if (unlikely(ip_len < sizeof(struct iphdr) ||
269 skb->len < nh_ofs + ip_len))
272 skb_set_transport_header(skb, nh_ofs + ip_len);
276 static bool tcphdr_ok(struct sk_buff *skb)
278 int th_ofs = skb_transport_offset(skb);
281 if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
284 tcp_len = tcp_hdrlen(skb);
285 if (unlikely(tcp_len < sizeof(struct tcphdr) ||
286 skb->len < th_ofs + tcp_len))
292 static bool udphdr_ok(struct sk_buff *skb)
294 return pskb_may_pull(skb, skb_transport_offset(skb) +
295 sizeof(struct udphdr));
298 static bool sctphdr_ok(struct sk_buff *skb)
300 return pskb_may_pull(skb, skb_transport_offset(skb) +
301 sizeof(struct sctphdr));
304 static bool icmphdr_ok(struct sk_buff *skb)
306 return pskb_may_pull(skb, skb_transport_offset(skb) +
307 sizeof(struct icmphdr));
310 u64 ovs_flow_used_time(unsigned long flow_jiffies)
312 struct timespec cur_ts;
315 ktime_get_ts(&cur_ts);
316 idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
317 cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
318 cur_ts.tv_nsec / NSEC_PER_MSEC;
320 return cur_ms - idle_ms;
323 static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
325 unsigned int nh_ofs = skb_network_offset(skb);
333 err = check_header(skb, nh_ofs + sizeof(*nh));
338 nexthdr = nh->nexthdr;
339 payload_ofs = (u8 *)(nh + 1) - skb->data;
341 key->ip.proto = NEXTHDR_NONE;
342 key->ip.tos = ipv6_get_dsfield(nh);
343 key->ip.ttl = nh->hop_limit;
344 key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
345 key->ipv6.addr.src = nh->saddr;
346 key->ipv6.addr.dst = nh->daddr;
348 payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
349 if (unlikely(payload_ofs < 0))
353 if (frag_off & htons(~0x7))
354 key->ip.frag = OVS_FRAG_TYPE_LATER;
356 key->ip.frag = OVS_FRAG_TYPE_FIRST;
359 nh_len = payload_ofs - nh_ofs;
360 skb_set_transport_header(skb, nh_ofs + nh_len);
361 key->ip.proto = nexthdr;
365 static bool icmp6hdr_ok(struct sk_buff *skb)
367 return pskb_may_pull(skb, skb_transport_offset(skb) +
368 sizeof(struct icmp6hdr));
371 void ovs_flow_key_mask(struct sw_flow_key *dst, const struct sw_flow_key *src,
372 const struct sw_flow_mask *mask)
374 u8 *m = (u8 *)&mask->key + mask->range.start;
375 u8 *s = (u8 *)src + mask->range.start;
376 u8 *d = (u8 *)dst + mask->range.start;
379 memset(dst, 0, sizeof(*dst));
380 for (i = 0; i < ovs_sw_flow_mask_size_roundup(mask); i++) {
386 #define TCP_FLAGS_OFFSET 13
387 #define TCP_FLAG_MASK 0x3f
389 void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb)
393 if ((flow->key.eth.type == htons(ETH_P_IP) ||
394 flow->key.eth.type == htons(ETH_P_IPV6)) &&
395 flow->key.ip.proto == IPPROTO_TCP &&
396 likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) {
397 u8 *tcp = (u8 *)tcp_hdr(skb);
398 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
401 spin_lock(&flow->lock);
402 flow->used = jiffies;
403 flow->packet_count++;
404 flow->byte_count += skb->len;
405 flow->tcp_flags |= tcp_flags;
406 spin_unlock(&flow->lock);
409 struct sw_flow_actions *ovs_flow_actions_alloc(int size)
411 struct sw_flow_actions *sfa;
413 if (size > MAX_ACTIONS_BUFSIZE)
414 return ERR_PTR(-EINVAL);
416 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
418 return ERR_PTR(-ENOMEM);
420 sfa->actions_len = 0;
424 struct sw_flow *ovs_flow_alloc(void)
426 struct sw_flow *flow;
428 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
430 return ERR_PTR(-ENOMEM);
432 spin_lock_init(&flow->lock);
433 flow->sf_acts = NULL;
439 static struct hlist_head *find_bucket(struct flow_table *table, u32 hash)
441 hash = jhash_1word(hash, table->hash_seed);
442 return flex_array_get(table->buckets,
443 (hash & (table->n_buckets - 1)));
446 static struct flex_array *alloc_buckets(unsigned int n_buckets)
448 struct flex_array *buckets;
451 buckets = flex_array_alloc(sizeof(struct hlist_head),
452 n_buckets, GFP_KERNEL);
456 err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
458 flex_array_free(buckets);
462 for (i = 0; i < n_buckets; i++)
463 INIT_HLIST_HEAD((struct hlist_head *)
464 flex_array_get(buckets, i));
469 static void free_buckets(struct flex_array *buckets)
471 flex_array_free(buckets);
474 static struct flow_table *__flow_tbl_alloc(int new_size)
476 struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL);
481 table->buckets = alloc_buckets(new_size);
483 if (!table->buckets) {
487 table->n_buckets = new_size;
490 table->keep_flows = false;
491 get_random_bytes(&table->hash_seed, sizeof(u32));
492 table->mask_list = NULL;
497 static void __flow_tbl_destroy(struct flow_table *table)
501 if (table->keep_flows)
504 for (i = 0; i < table->n_buckets; i++) {
505 struct sw_flow *flow;
506 struct hlist_head *head = flex_array_get(table->buckets, i);
507 struct hlist_node *n;
508 int ver = table->node_ver;
510 hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
511 hlist_del(&flow->hash_node[ver]);
512 ovs_flow_free(flow, false);
516 BUG_ON(!list_empty(table->mask_list));
517 kfree(table->mask_list);
520 free_buckets(table->buckets);
524 struct flow_table *ovs_flow_tbl_alloc(int new_size)
526 struct flow_table *table = __flow_tbl_alloc(new_size);
531 table->mask_list = kmalloc(sizeof(struct list_head), GFP_KERNEL);
532 if (!table->mask_list) {
533 table->keep_flows = true;
534 __flow_tbl_destroy(table);
537 INIT_LIST_HEAD(table->mask_list);
542 static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
544 struct flow_table *table = container_of(rcu, struct flow_table, rcu);
546 __flow_tbl_destroy(table);
549 void ovs_flow_tbl_destroy(struct flow_table *table, bool deferred)
555 call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb);
557 __flow_tbl_destroy(table);
560 struct sw_flow *ovs_flow_dump_next(struct flow_table *table, u32 *bucket, u32 *last)
562 struct sw_flow *flow;
563 struct hlist_head *head;
567 ver = table->node_ver;
568 while (*bucket < table->n_buckets) {
570 head = flex_array_get(table->buckets, *bucket);
571 hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
586 static void __tbl_insert(struct flow_table *table, struct sw_flow *flow)
588 struct hlist_head *head;
590 head = find_bucket(table, flow->hash);
591 hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
596 static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new)
601 old_ver = old->node_ver;
602 new->node_ver = !old_ver;
604 /* Insert in new table. */
605 for (i = 0; i < old->n_buckets; i++) {
606 struct sw_flow *flow;
607 struct hlist_head *head;
609 head = flex_array_get(old->buckets, i);
611 hlist_for_each_entry(flow, head, hash_node[old_ver])
612 __tbl_insert(new, flow);
615 new->mask_list = old->mask_list;
616 old->keep_flows = true;
619 static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets)
621 struct flow_table *new_table;
623 new_table = __flow_tbl_alloc(n_buckets);
625 return ERR_PTR(-ENOMEM);
627 flow_table_copy_flows(table, new_table);
632 struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table)
634 return __flow_tbl_rehash(table, table->n_buckets);
637 struct flow_table *ovs_flow_tbl_expand(struct flow_table *table)
639 return __flow_tbl_rehash(table, table->n_buckets * 2);
642 static void __flow_free(struct sw_flow *flow)
644 kfree((struct sf_flow_acts __force *)flow->sf_acts);
645 kmem_cache_free(flow_cache, flow);
648 static void rcu_free_flow_callback(struct rcu_head *rcu)
650 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
655 void ovs_flow_free(struct sw_flow *flow, bool deferred)
660 ovs_sw_flow_mask_del_ref(flow->mask, deferred);
663 call_rcu(&flow->rcu, rcu_free_flow_callback);
668 /* RCU callback used by ovs_flow_deferred_free_acts. */
669 static void rcu_free_acts_callback(struct rcu_head *rcu)
671 struct sw_flow_actions *sf_acts = container_of(rcu,
672 struct sw_flow_actions, rcu);
676 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
677 * The caller must hold rcu_read_lock for this to be sensible. */
678 void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
680 call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
683 static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
686 __be16 eth_type; /* ETH_P_8021Q */
689 struct qtag_prefix *qp;
691 if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
694 if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
698 qp = (struct qtag_prefix *) skb->data;
699 key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
700 __skb_pull(skb, sizeof(struct qtag_prefix));
705 static __be16 parse_ethertype(struct sk_buff *skb)
707 struct llc_snap_hdr {
708 u8 dsap; /* Always 0xAA */
709 u8 ssap; /* Always 0xAA */
714 struct llc_snap_hdr *llc;
717 proto = *(__be16 *) skb->data;
718 __skb_pull(skb, sizeof(__be16));
720 if (ntohs(proto) >= ETH_P_802_3_MIN)
723 if (skb->len < sizeof(struct llc_snap_hdr))
724 return htons(ETH_P_802_2);
726 if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
729 llc = (struct llc_snap_hdr *) skb->data;
730 if (llc->dsap != LLC_SAP_SNAP ||
731 llc->ssap != LLC_SAP_SNAP ||
732 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
733 return htons(ETH_P_802_2);
735 __skb_pull(skb, sizeof(struct llc_snap_hdr));
737 if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
738 return llc->ethertype;
740 return htons(ETH_P_802_2);
743 static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
746 struct icmp6hdr *icmp = icmp6_hdr(skb);
748 /* The ICMPv6 type and code fields use the 16-bit transport port
749 * fields, so we need to store them in 16-bit network byte order.
751 key->ipv6.tp.src = htons(icmp->icmp6_type);
752 key->ipv6.tp.dst = htons(icmp->icmp6_code);
754 if (icmp->icmp6_code == 0 &&
755 (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
756 icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
757 int icmp_len = skb->len - skb_transport_offset(skb);
761 /* In order to process neighbor discovery options, we need the
764 if (unlikely(icmp_len < sizeof(*nd)))
767 if (unlikely(skb_linearize(skb)))
770 nd = (struct nd_msg *)skb_transport_header(skb);
771 key->ipv6.nd.target = nd->target;
773 icmp_len -= sizeof(*nd);
775 while (icmp_len >= 8) {
776 struct nd_opt_hdr *nd_opt =
777 (struct nd_opt_hdr *)(nd->opt + offset);
778 int opt_len = nd_opt->nd_opt_len * 8;
780 if (unlikely(!opt_len || opt_len > icmp_len))
783 /* Store the link layer address if the appropriate
784 * option is provided. It is considered an error if
785 * the same link layer option is specified twice.
787 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
789 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
791 memcpy(key->ipv6.nd.sll,
792 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
793 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
795 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
797 memcpy(key->ipv6.nd.tll,
798 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
809 memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
810 memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
811 memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
817 * ovs_flow_extract - extracts a flow key from an Ethernet frame.
818 * @skb: sk_buff that contains the frame, with skb->data pointing to the
820 * @in_port: port number on which @skb was received.
821 * @key: output flow key
823 * The caller must ensure that skb->len >= ETH_HLEN.
825 * Returns 0 if successful, otherwise a negative errno value.
827 * Initializes @skb header pointers as follows:
829 * - skb->mac_header: the Ethernet header.
831 * - skb->network_header: just past the Ethernet header, or just past the
832 * VLAN header, to the first byte of the Ethernet payload.
834 * - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
835 * on output, then just past the IP header, if one is present and
836 * of a correct length, otherwise the same as skb->network_header.
837 * For other key->eth.type values it is left untouched.
839 int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
844 memset(key, 0, sizeof(*key));
846 key->phy.priority = skb->priority;
847 if (OVS_CB(skb)->tun_key)
848 memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key));
849 key->phy.in_port = in_port;
850 key->phy.skb_mark = skb_get_mark(skb);
852 skb_reset_mac_header(skb);
854 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
855 * header in the linear data area.
858 memcpy(key->eth.src, eth->h_source, ETH_ALEN);
859 memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
861 __skb_pull(skb, 2 * ETH_ALEN);
862 /* We are going to push all headers that we pull, so no need to
863 * update skb->csum here. */
865 if (vlan_tx_tag_present(skb))
866 key->eth.tci = htons(vlan_get_tci(skb));
867 else if (eth->h_proto == htons(ETH_P_8021Q))
868 if (unlikely(parse_vlan(skb, key)))
871 key->eth.type = parse_ethertype(skb);
872 if (unlikely(key->eth.type == htons(0)))
875 skb_reset_network_header(skb);
876 __skb_push(skb, skb->data - skb_mac_header(skb));
879 if (key->eth.type == htons(ETH_P_IP)) {
883 error = check_iphdr(skb);
884 if (unlikely(error)) {
885 if (error == -EINVAL) {
886 skb->transport_header = skb->network_header;
893 key->ipv4.addr.src = nh->saddr;
894 key->ipv4.addr.dst = nh->daddr;
896 key->ip.proto = nh->protocol;
897 key->ip.tos = nh->tos;
898 key->ip.ttl = nh->ttl;
900 offset = nh->frag_off & htons(IP_OFFSET);
902 key->ip.frag = OVS_FRAG_TYPE_LATER;
905 if (nh->frag_off & htons(IP_MF) ||
906 skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
907 key->ip.frag = OVS_FRAG_TYPE_FIRST;
909 /* Transport layer. */
910 if (key->ip.proto == IPPROTO_TCP) {
911 if (tcphdr_ok(skb)) {
912 struct tcphdr *tcp = tcp_hdr(skb);
913 key->ipv4.tp.src = tcp->source;
914 key->ipv4.tp.dst = tcp->dest;
916 } else if (key->ip.proto == IPPROTO_UDP) {
917 if (udphdr_ok(skb)) {
918 struct udphdr *udp = udp_hdr(skb);
919 key->ipv4.tp.src = udp->source;
920 key->ipv4.tp.dst = udp->dest;
922 } else if (key->ip.proto == IPPROTO_SCTP) {
923 if (sctphdr_ok(skb)) {
924 struct sctphdr *sctp = sctp_hdr(skb);
925 key->ipv4.tp.src = sctp->source;
926 key->ipv4.tp.dst = sctp->dest;
928 } else if (key->ip.proto == IPPROTO_ICMP) {
929 if (icmphdr_ok(skb)) {
930 struct icmphdr *icmp = icmp_hdr(skb);
931 /* The ICMP type and code fields use the 16-bit
932 * transport port fields, so we need to store
933 * them in 16-bit network byte order. */
934 key->ipv4.tp.src = htons(icmp->type);
935 key->ipv4.tp.dst = htons(icmp->code);
939 } else if ((key->eth.type == htons(ETH_P_ARP) ||
940 key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) {
941 struct arp_eth_header *arp;
943 arp = (struct arp_eth_header *)skb_network_header(skb);
945 if (arp->ar_hrd == htons(ARPHRD_ETHER)
946 && arp->ar_pro == htons(ETH_P_IP)
947 && arp->ar_hln == ETH_ALEN
948 && arp->ar_pln == 4) {
950 /* We only match on the lower 8 bits of the opcode. */
951 if (ntohs(arp->ar_op) <= 0xff)
952 key->ip.proto = ntohs(arp->ar_op);
953 memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
954 memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
955 memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
956 memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
958 } else if (key->eth.type == htons(ETH_P_IPV6)) {
959 int nh_len; /* IPv6 Header + Extensions */
961 nh_len = parse_ipv6hdr(skb, key);
962 if (unlikely(nh_len < 0)) {
963 if (nh_len == -EINVAL) {
964 skb->transport_header = skb->network_header;
972 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
974 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
975 key->ip.frag = OVS_FRAG_TYPE_FIRST;
977 /* Transport layer. */
978 if (key->ip.proto == NEXTHDR_TCP) {
979 if (tcphdr_ok(skb)) {
980 struct tcphdr *tcp = tcp_hdr(skb);
981 key->ipv6.tp.src = tcp->source;
982 key->ipv6.tp.dst = tcp->dest;
984 } else if (key->ip.proto == NEXTHDR_UDP) {
985 if (udphdr_ok(skb)) {
986 struct udphdr *udp = udp_hdr(skb);
987 key->ipv6.tp.src = udp->source;
988 key->ipv6.tp.dst = udp->dest;
990 } else if (key->ip.proto == NEXTHDR_SCTP) {
991 if (sctphdr_ok(skb)) {
992 struct sctphdr *sctp = sctp_hdr(skb);
993 key->ipv6.tp.src = sctp->source;
994 key->ipv6.tp.dst = sctp->dest;
996 } else if (key->ip.proto == NEXTHDR_ICMP) {
997 if (icmp6hdr_ok(skb)) {
998 error = parse_icmpv6(skb, key, nh_len);
1008 static u32 ovs_flow_hash(const struct sw_flow_key *key, int key_start,
1011 return jhash2((u32 *)((u8 *)key + key_start),
1012 DIV_ROUND_UP(key_end - key_start, sizeof(u32)), 0);
1015 static int flow_key_start(const struct sw_flow_key *key)
1017 if (key->tun_key.ipv4_dst)
1020 return offsetof(struct sw_flow_key, phy);
1023 static bool __cmp_key(const struct sw_flow_key *key1,
1024 const struct sw_flow_key *key2, int key_start, int key_end)
1026 return !memcmp((u8 *)key1 + key_start,
1027 (u8 *)key2 + key_start, (key_end - key_start));
1030 static bool __flow_cmp_key(const struct sw_flow *flow,
1031 const struct sw_flow_key *key, int key_start, int key_end)
1033 return __cmp_key(&flow->key, key, key_start, key_end);
1036 static bool __flow_cmp_unmasked_key(const struct sw_flow *flow,
1037 const struct sw_flow_key *key, int key_start, int key_end)
1039 return __cmp_key(&flow->unmasked_key, key, key_start, key_end);
1042 bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
1043 const struct sw_flow_key *key, int key_end)
1046 key_start = flow_key_start(key);
1048 return __flow_cmp_unmasked_key(flow, key, key_start, key_end);
1052 struct sw_flow *ovs_flow_lookup_unmasked_key(struct flow_table *table,
1053 struct sw_flow_match *match)
1055 struct sw_flow_key *unmasked = match->key;
1056 int key_end = match->range.end;
1057 struct sw_flow *flow;
1059 flow = ovs_flow_lookup(table, unmasked);
1060 if (flow && (!ovs_flow_cmp_unmasked_key(flow, unmasked, key_end)))
1066 static struct sw_flow *ovs_masked_flow_lookup(struct flow_table *table,
1067 const struct sw_flow_key *flow_key,
1068 struct sw_flow_mask *mask)
1070 struct sw_flow *flow;
1071 struct hlist_head *head;
1072 int key_start = mask->range.start;
1073 int key_end = mask->range.end;
1075 struct sw_flow_key masked_key;
1077 ovs_flow_key_mask(&masked_key, flow_key, mask);
1078 hash = ovs_flow_hash(&masked_key, key_start, key_end);
1079 head = find_bucket(table, hash);
1080 hlist_for_each_entry_rcu(flow, head, hash_node[table->node_ver]) {
1081 if (flow->mask == mask &&
1082 __flow_cmp_key(flow, &masked_key, key_start, key_end))
1088 struct sw_flow *ovs_flow_lookup(struct flow_table *tbl,
1089 const struct sw_flow_key *key)
1091 struct sw_flow *flow = NULL;
1092 struct sw_flow_mask *mask;
1094 list_for_each_entry_rcu(mask, tbl->mask_list, list) {
1095 flow = ovs_masked_flow_lookup(tbl, key, mask);
1096 if (flow) /* Found */
1104 void ovs_flow_insert(struct flow_table *table, struct sw_flow *flow)
1106 flow->hash = ovs_flow_hash(&flow->key, flow->mask->range.start,
1107 flow->mask->range.end);
1108 __tbl_insert(table, flow);
1111 void ovs_flow_remove(struct flow_table *table, struct sw_flow *flow)
1113 BUG_ON(table->count == 0);
1114 hlist_del_rcu(&flow->hash_node[table->node_ver]);
1118 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
1119 const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
1120 [OVS_KEY_ATTR_ENCAP] = -1,
1121 [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
1122 [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
1123 [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
1124 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
1125 [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
1126 [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
1127 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
1128 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
1129 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
1130 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
1131 [OVS_KEY_ATTR_SCTP] = sizeof(struct ovs_key_sctp),
1132 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
1133 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
1134 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
1135 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
1136 [OVS_KEY_ATTR_TUNNEL] = -1,
1139 static bool is_all_zero(const u8 *fp, size_t size)
1146 for (i = 0; i < size; i++)
1153 static int __parse_flow_nlattrs(const struct nlattr *attr,
1154 const struct nlattr *a[],
1155 u64 *attrsp, bool nz)
1157 const struct nlattr *nla;
1162 nla_for_each_nested(nla, attr, rem) {
1163 u16 type = nla_type(nla);
1166 if (type > OVS_KEY_ATTR_MAX) {
1167 OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
1168 type, OVS_KEY_ATTR_MAX);
1171 if (attrs & (1ULL << type)) {
1172 OVS_NLERR("Duplicate key attribute (type %d).\n", type);
1176 expected_len = ovs_key_lens[type];
1177 if (nla_len(nla) != expected_len && expected_len != -1) {
1178 OVS_NLERR("Key attribute has unexpected length (type=%d"
1179 ", length=%d, expected=%d).\n", type,
1180 nla_len(nla), expected_len);
1184 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
1185 attrs |= 1ULL << type;
1190 OVS_NLERR("Message has %d unknown bytes.\n", rem);
1198 static int parse_flow_mask_nlattrs(const struct nlattr *attr,
1199 const struct nlattr *a[], u64 *attrsp)
1201 return __parse_flow_nlattrs(attr, a, attrsp, true);
1204 static int parse_flow_nlattrs(const struct nlattr *attr,
1205 const struct nlattr *a[], u64 *attrsp)
1207 return __parse_flow_nlattrs(attr, a, attrsp, false);
1210 int ovs_ipv4_tun_from_nlattr(const struct nlattr *attr,
1211 struct sw_flow_match *match, bool is_mask)
1216 __be16 tun_flags = 0;
1218 nla_for_each_nested(a, attr, rem) {
1219 int type = nla_type(a);
1220 static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
1221 [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
1222 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
1223 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
1224 [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
1225 [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
1226 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
1227 [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
1230 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
1231 OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n",
1232 type, OVS_TUNNEL_KEY_ATTR_MAX);
1236 if (ovs_tunnel_key_lens[type] != nla_len(a)) {
1237 OVS_NLERR("IPv4 tunnel attribute type has unexpected "
1238 " length (type=%d, length=%d, expected=%d).\n",
1239 type, nla_len(a), ovs_tunnel_key_lens[type]);
1244 case OVS_TUNNEL_KEY_ATTR_ID:
1245 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
1246 nla_get_be64(a), is_mask);
1247 tun_flags |= TUNNEL_KEY;
1249 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1250 SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
1251 nla_get_be32(a), is_mask);
1253 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1254 SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
1255 nla_get_be32(a), is_mask);
1257 case OVS_TUNNEL_KEY_ATTR_TOS:
1258 SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
1259 nla_get_u8(a), is_mask);
1261 case OVS_TUNNEL_KEY_ATTR_TTL:
1262 SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
1263 nla_get_u8(a), is_mask);
1266 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1267 tun_flags |= TUNNEL_DONT_FRAGMENT;
1269 case OVS_TUNNEL_KEY_ATTR_CSUM:
1270 tun_flags |= TUNNEL_CSUM;
1277 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
1280 OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
1285 if (!match->key->tun_key.ipv4_dst) {
1286 OVS_NLERR("IPv4 tunnel destination address is zero.\n");
1291 OVS_NLERR("IPv4 tunnel TTL not specified.\n");
1299 int ovs_ipv4_tun_to_nlattr(struct sk_buff *skb,
1300 const struct ovs_key_ipv4_tunnel *tun_key,
1301 const struct ovs_key_ipv4_tunnel *output)
1305 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
1309 if (output->tun_flags & TUNNEL_KEY &&
1310 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
1312 if (output->ipv4_src &&
1313 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
1315 if (output->ipv4_dst &&
1316 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
1318 if (output->ipv4_tos &&
1319 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
1321 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
1323 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
1324 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
1326 if ((output->tun_flags & TUNNEL_CSUM) &&
1327 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
1330 nla_nest_end(skb, nla);
1335 static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
1336 const struct nlattr **a, bool is_mask)
1338 if (*attrs & (1ULL << OVS_KEY_ATTR_PRIORITY)) {
1339 SW_FLOW_KEY_PUT(match, phy.priority,
1340 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
1341 *attrs &= ~(1ULL << OVS_KEY_ATTR_PRIORITY);
1344 if (*attrs & (1ULL << OVS_KEY_ATTR_IN_PORT)) {
1345 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1348 in_port = 0xffffffff; /* Always exact match in_port. */
1349 else if (in_port >= DP_MAX_PORTS)
1352 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
1353 *attrs &= ~(1ULL << OVS_KEY_ATTR_IN_PORT);
1354 } else if (!is_mask) {
1355 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
1358 if (*attrs & (1ULL << OVS_KEY_ATTR_SKB_MARK)) {
1359 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
1360 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER)
1361 if (!is_mask && mark != 0) {
1362 OVS_NLERR("skb->mark must be zero on this kernel (mark=%d).\n", mark);
1366 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
1367 *attrs &= ~(1ULL << OVS_KEY_ATTR_SKB_MARK);
1369 if (*attrs & (1ULL << OVS_KEY_ATTR_TUNNEL)) {
1370 if (ovs_ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
1373 *attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL);
1378 static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
1379 const struct nlattr **a, bool is_mask)
1382 u64 orig_attrs = attrs;
1384 err = metadata_from_nlattrs(match, &attrs, a, is_mask);
1388 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) {
1389 const struct ovs_key_ethernet *eth_key;
1391 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1392 SW_FLOW_KEY_MEMCPY(match, eth.src,
1393 eth_key->eth_src, ETH_ALEN, is_mask);
1394 SW_FLOW_KEY_MEMCPY(match, eth.dst,
1395 eth_key->eth_dst, ETH_ALEN, is_mask);
1396 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERNET);
1399 if (attrs & (1ULL << OVS_KEY_ATTR_VLAN)) {
1402 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1403 if (!(tci & htons(VLAN_TAG_PRESENT))) {
1405 OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
1407 OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
1412 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
1413 attrs &= ~(1ULL << OVS_KEY_ATTR_VLAN);
1414 } else if (!is_mask)
1415 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
1417 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) {
1420 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1422 /* Always exact match EtherType. */
1423 eth_type = htons(0xffff);
1424 } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
1425 OVS_NLERR("EtherType is less than minimum (type=%x, min=%x).\n",
1426 ntohs(eth_type), ETH_P_802_3_MIN);
1430 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
1431 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1432 } else if (!is_mask) {
1433 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
1436 if (attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1437 const struct ovs_key_ipv4 *ipv4_key;
1439 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1440 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
1441 OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
1442 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
1445 SW_FLOW_KEY_PUT(match, ip.proto,
1446 ipv4_key->ipv4_proto, is_mask);
1447 SW_FLOW_KEY_PUT(match, ip.tos,
1448 ipv4_key->ipv4_tos, is_mask);
1449 SW_FLOW_KEY_PUT(match, ip.ttl,
1450 ipv4_key->ipv4_ttl, is_mask);
1451 SW_FLOW_KEY_PUT(match, ip.frag,
1452 ipv4_key->ipv4_frag, is_mask);
1453 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1454 ipv4_key->ipv4_src, is_mask);
1455 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1456 ipv4_key->ipv4_dst, is_mask);
1457 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV4);
1460 if (attrs & (1ULL << OVS_KEY_ATTR_IPV6)) {
1461 const struct ovs_key_ipv6 *ipv6_key;
1463 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1464 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
1465 OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
1466 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
1469 SW_FLOW_KEY_PUT(match, ipv6.label,
1470 ipv6_key->ipv6_label, is_mask);
1471 SW_FLOW_KEY_PUT(match, ip.proto,
1472 ipv6_key->ipv6_proto, is_mask);
1473 SW_FLOW_KEY_PUT(match, ip.tos,
1474 ipv6_key->ipv6_tclass, is_mask);
1475 SW_FLOW_KEY_PUT(match, ip.ttl,
1476 ipv6_key->ipv6_hlimit, is_mask);
1477 SW_FLOW_KEY_PUT(match, ip.frag,
1478 ipv6_key->ipv6_frag, is_mask);
1479 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
1481 sizeof(match->key->ipv6.addr.src),
1483 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
1485 sizeof(match->key->ipv6.addr.dst),
1488 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV6);
1491 if (attrs & (1ULL << OVS_KEY_ATTR_ARP)) {
1492 const struct ovs_key_arp *arp_key;
1494 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1495 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
1496 OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
1501 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1502 arp_key->arp_sip, is_mask);
1503 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1504 arp_key->arp_tip, is_mask);
1505 SW_FLOW_KEY_PUT(match, ip.proto,
1506 ntohs(arp_key->arp_op), is_mask);
1507 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
1508 arp_key->arp_sha, ETH_ALEN, is_mask);
1509 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
1510 arp_key->arp_tha, ETH_ALEN, is_mask);
1512 attrs &= ~(1ULL << OVS_KEY_ATTR_ARP);
1515 if (attrs & (1ULL << OVS_KEY_ATTR_TCP)) {
1516 const struct ovs_key_tcp *tcp_key;
1518 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
1519 if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1520 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1521 tcp_key->tcp_src, is_mask);
1522 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1523 tcp_key->tcp_dst, is_mask);
1525 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1526 tcp_key->tcp_src, is_mask);
1527 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1528 tcp_key->tcp_dst, is_mask);
1530 attrs &= ~(1ULL << OVS_KEY_ATTR_TCP);
1533 if (attrs & (1ULL << OVS_KEY_ATTR_UDP)) {
1534 const struct ovs_key_udp *udp_key;
1536 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
1537 if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1538 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1539 udp_key->udp_src, is_mask);
1540 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1541 udp_key->udp_dst, is_mask);
1543 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1544 udp_key->udp_src, is_mask);
1545 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1546 udp_key->udp_dst, is_mask);
1548 attrs &= ~(1ULL << OVS_KEY_ATTR_UDP);
1551 if (attrs & (1ULL << OVS_KEY_ATTR_SCTP)) {
1552 const struct ovs_key_sctp *sctp_key;
1554 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
1555 if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1556 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1557 sctp_key->sctp_src, is_mask);
1558 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1559 sctp_key->sctp_dst, is_mask);
1561 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1562 sctp_key->sctp_src, is_mask);
1563 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1564 sctp_key->sctp_dst, is_mask);
1566 attrs &= ~(1ULL << OVS_KEY_ATTR_SCTP);
1569 if (attrs & (1ULL << OVS_KEY_ATTR_ICMP)) {
1570 const struct ovs_key_icmp *icmp_key;
1572 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
1573 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1574 htons(icmp_key->icmp_type), is_mask);
1575 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1576 htons(icmp_key->icmp_code), is_mask);
1577 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMP);
1580 if (attrs & (1ULL << OVS_KEY_ATTR_ICMPV6)) {
1581 const struct ovs_key_icmpv6 *icmpv6_key;
1583 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
1584 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1585 htons(icmpv6_key->icmpv6_type), is_mask);
1586 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1587 htons(icmpv6_key->icmpv6_code), is_mask);
1588 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMPV6);
1591 if (attrs & (1ULL << OVS_KEY_ATTR_ND)) {
1592 const struct ovs_key_nd *nd_key;
1594 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1595 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1597 sizeof(match->key->ipv6.nd.target),
1599 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1600 nd_key->nd_sll, ETH_ALEN, is_mask);
1601 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1602 nd_key->nd_tll, ETH_ALEN, is_mask);
1603 attrs &= ~(1ULL << OVS_KEY_ATTR_ND);
1613 * ovs_match_from_nlattrs - parses Netlink attributes into a flow key and
1614 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1615 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1616 * does not include any don't care bit.
1617 * @match: receives the extracted flow match information.
1618 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1619 * sequence. The fields should of the packet that triggered the creation
1621 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1622 * attribute specifies the mask field of the wildcarded flow.
1624 int ovs_match_from_nlattrs(struct sw_flow_match *match,
1625 const struct nlattr *key,
1626 const struct nlattr *mask)
1628 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1629 const struct nlattr *encap;
1632 bool encap_valid = false;
1635 err = parse_flow_nlattrs(key, a, &key_attrs);
1639 if (key_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) {
1640 encap = a[OVS_KEY_ATTR_ENCAP];
1641 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
1642 if (nla_len(encap)) {
1643 __be16 eth_type = 0; /* ETH_P_8021Q */
1645 if (a[OVS_KEY_ATTR_ETHERTYPE])
1646 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1648 if ((eth_type == htons(ETH_P_8021Q)) && (a[OVS_KEY_ATTR_VLAN])) {
1650 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1651 err = parse_flow_nlattrs(encap, a, &key_attrs);
1653 OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
1662 err = ovs_key_from_nlattrs(match, key_attrs, a, false);
1667 err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
1671 if ((mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) && encap_valid) {
1672 __be16 eth_type = 0;
1674 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
1675 if (a[OVS_KEY_ATTR_ETHERTYPE])
1676 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1677 if (eth_type == htons(0xffff)) {
1678 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1679 encap = a[OVS_KEY_ATTR_ENCAP];
1680 err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
1682 OVS_NLERR("VLAN frames must have an exact match"
1683 " on the TPID (mask=%x).\n",
1692 err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
1696 /* Populate exact match flow's key mask. */
1698 ovs_sw_flow_mask_set(match->mask, &match->range, 0xff);
1701 if (!ovs_match_validate(match, key_attrs, mask_attrs))
1708 * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
1709 * @flow: Receives extracted in_port, priority, tun_key and skb_mark.
1710 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1713 * This parses a series of Netlink attributes that form a flow key, which must
1714 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1715 * get the metadata, that is, the parts of the flow key that cannot be
1716 * extracted from the packet itself.
1719 int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow,
1720 const struct nlattr *attr)
1722 struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key;
1723 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1726 struct sw_flow_match match;
1728 flow->key.phy.in_port = DP_MAX_PORTS;
1729 flow->key.phy.priority = 0;
1730 flow->key.phy.skb_mark = 0;
1731 memset(tun_key, 0, sizeof(flow->key.tun_key));
1733 err = parse_flow_nlattrs(attr, a, &attrs);
1737 memset(&match, 0, sizeof(match));
1738 match.key = &flow->key;
1740 err = metadata_from_nlattrs(&match, &attrs, a, false);
1747 int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey,
1748 const struct sw_flow_key *output, struct sk_buff *skb)
1750 struct ovs_key_ethernet *eth_key;
1751 struct nlattr *nla, *encap;
1752 bool is_mask = (swkey != output);
1754 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1755 goto nla_put_failure;
1757 if ((swkey->tun_key.ipv4_dst || is_mask) &&
1758 ovs_ipv4_tun_to_nlattr(skb, &swkey->tun_key, &output->tun_key))
1759 goto nla_put_failure;
1761 if (swkey->phy.in_port == DP_MAX_PORTS) {
1762 if (is_mask && (output->phy.in_port == 0xffff))
1763 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1764 goto nla_put_failure;
1767 upper_u16 = !is_mask ? 0 : 0xffff;
1769 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1770 (upper_u16 << 16) | output->phy.in_port))
1771 goto nla_put_failure;
1774 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1775 goto nla_put_failure;
1777 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1779 goto nla_put_failure;
1781 eth_key = nla_data(nla);
1782 memcpy(eth_key->eth_src, output->eth.src, ETH_ALEN);
1783 memcpy(eth_key->eth_dst, output->eth.dst, ETH_ALEN);
1785 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1787 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
1788 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1789 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1790 goto nla_put_failure;
1791 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1792 if (!swkey->eth.tci)
1797 if (swkey->eth.type == htons(ETH_P_802_2)) {
1799 * Ethertype 802.2 is represented in the netlink with omitted
1800 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1801 * 0xffff in the mask attribute. Ethertype can also
1804 if (is_mask && output->eth.type)
1805 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1807 goto nla_put_failure;
1811 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1812 goto nla_put_failure;
1814 if (swkey->eth.type == htons(ETH_P_IP)) {
1815 struct ovs_key_ipv4 *ipv4_key;
1817 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1819 goto nla_put_failure;
1820 ipv4_key = nla_data(nla);
1821 ipv4_key->ipv4_src = output->ipv4.addr.src;
1822 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1823 ipv4_key->ipv4_proto = output->ip.proto;
1824 ipv4_key->ipv4_tos = output->ip.tos;
1825 ipv4_key->ipv4_ttl = output->ip.ttl;
1826 ipv4_key->ipv4_frag = output->ip.frag;
1827 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1828 struct ovs_key_ipv6 *ipv6_key;
1830 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1832 goto nla_put_failure;
1833 ipv6_key = nla_data(nla);
1834 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1835 sizeof(ipv6_key->ipv6_src));
1836 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1837 sizeof(ipv6_key->ipv6_dst));
1838 ipv6_key->ipv6_label = output->ipv6.label;
1839 ipv6_key->ipv6_proto = output->ip.proto;
1840 ipv6_key->ipv6_tclass = output->ip.tos;
1841 ipv6_key->ipv6_hlimit = output->ip.ttl;
1842 ipv6_key->ipv6_frag = output->ip.frag;
1843 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1844 swkey->eth.type == htons(ETH_P_RARP)) {
1845 struct ovs_key_arp *arp_key;
1847 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1849 goto nla_put_failure;
1850 arp_key = nla_data(nla);
1851 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1852 arp_key->arp_sip = output->ipv4.addr.src;
1853 arp_key->arp_tip = output->ipv4.addr.dst;
1854 arp_key->arp_op = htons(output->ip.proto);
1855 memcpy(arp_key->arp_sha, output->ipv4.arp.sha, ETH_ALEN);
1856 memcpy(arp_key->arp_tha, output->ipv4.arp.tha, ETH_ALEN);
1859 if ((swkey->eth.type == htons(ETH_P_IP) ||
1860 swkey->eth.type == htons(ETH_P_IPV6)) &&
1861 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1863 if (swkey->ip.proto == IPPROTO_TCP) {
1864 struct ovs_key_tcp *tcp_key;
1866 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1868 goto nla_put_failure;
1869 tcp_key = nla_data(nla);
1870 if (swkey->eth.type == htons(ETH_P_IP)) {
1871 tcp_key->tcp_src = output->ipv4.tp.src;
1872 tcp_key->tcp_dst = output->ipv4.tp.dst;
1873 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1874 tcp_key->tcp_src = output->ipv6.tp.src;
1875 tcp_key->tcp_dst = output->ipv6.tp.dst;
1877 } else if (swkey->ip.proto == IPPROTO_UDP) {
1878 struct ovs_key_udp *udp_key;
1880 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1882 goto nla_put_failure;
1883 udp_key = nla_data(nla);
1884 if (swkey->eth.type == htons(ETH_P_IP)) {
1885 udp_key->udp_src = output->ipv4.tp.src;
1886 udp_key->udp_dst = output->ipv4.tp.dst;
1887 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1888 udp_key->udp_src = output->ipv6.tp.src;
1889 udp_key->udp_dst = output->ipv6.tp.dst;
1891 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1892 struct ovs_key_sctp *sctp_key;
1894 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1896 goto nla_put_failure;
1897 sctp_key = nla_data(nla);
1898 if (swkey->eth.type == htons(ETH_P_IP)) {
1899 sctp_key->sctp_src = swkey->ipv4.tp.src;
1900 sctp_key->sctp_dst = swkey->ipv4.tp.dst;
1901 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1902 sctp_key->sctp_src = swkey->ipv6.tp.src;
1903 sctp_key->sctp_dst = swkey->ipv6.tp.dst;
1905 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1906 swkey->ip.proto == IPPROTO_ICMP) {
1907 struct ovs_key_icmp *icmp_key;
1909 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1911 goto nla_put_failure;
1912 icmp_key = nla_data(nla);
1913 icmp_key->icmp_type = ntohs(output->ipv4.tp.src);
1914 icmp_key->icmp_code = ntohs(output->ipv4.tp.dst);
1915 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1916 swkey->ip.proto == IPPROTO_ICMPV6) {
1917 struct ovs_key_icmpv6 *icmpv6_key;
1919 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1920 sizeof(*icmpv6_key));
1922 goto nla_put_failure;
1923 icmpv6_key = nla_data(nla);
1924 icmpv6_key->icmpv6_type = ntohs(output->ipv6.tp.src);
1925 icmpv6_key->icmpv6_code = ntohs(output->ipv6.tp.dst);
1927 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1928 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1929 struct ovs_key_nd *nd_key;
1931 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1933 goto nla_put_failure;
1934 nd_key = nla_data(nla);
1935 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1936 sizeof(nd_key->nd_target));
1937 memcpy(nd_key->nd_sll, output->ipv6.nd.sll, ETH_ALEN);
1938 memcpy(nd_key->nd_tll, output->ipv6.nd.tll, ETH_ALEN);
1945 nla_nest_end(skb, encap);
1953 /* Initializes the flow module.
1954 * Returns zero if successful or a negative error code. */
1955 int ovs_flow_init(void)
1957 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
1959 if (flow_cache == NULL)
1965 /* Uninitializes the flow module. */
1966 void ovs_flow_exit(void)
1968 kmem_cache_destroy(flow_cache);
1971 struct sw_flow_mask *ovs_sw_flow_mask_alloc(void)
1973 struct sw_flow_mask *mask;
1975 mask = kmalloc(sizeof(*mask), GFP_KERNEL);
1977 mask->ref_count = 0;
1982 void ovs_sw_flow_mask_add_ref(struct sw_flow_mask *mask)
1987 static void rcu_free_sw_flow_mask_cb(struct rcu_head *rcu)
1989 struct sw_flow_mask *mask = container_of(rcu, struct sw_flow_mask, rcu);
1994 void ovs_sw_flow_mask_del_ref(struct sw_flow_mask *mask, bool deferred)
1999 BUG_ON(!mask->ref_count);
2002 if (!mask->ref_count) {
2003 list_del_rcu(&mask->list);
2005 call_rcu(&mask->rcu, rcu_free_sw_flow_mask_cb);
2011 static bool ovs_sw_flow_mask_equal(const struct sw_flow_mask *a,
2012 const struct sw_flow_mask *b)
2014 u8 *a_ = (u8 *)&a->key + a->range.start;
2015 u8 *b_ = (u8 *)&b->key + b->range.start;
2017 return (a->range.end == b->range.end)
2018 && (a->range.start == b->range.start)
2019 && (memcmp(a_, b_, ovs_sw_flow_mask_actual_size(a)) == 0);
2022 struct sw_flow_mask *ovs_sw_flow_mask_find(const struct flow_table *tbl,
2023 const struct sw_flow_mask *mask)
2025 struct list_head *ml;
2027 list_for_each(ml, tbl->mask_list) {
2028 struct sw_flow_mask *m;
2029 m = container_of(ml, struct sw_flow_mask, list);
2030 if (ovs_sw_flow_mask_equal(mask, m))
2038 * add a new mask into the mask list.
2039 * The caller needs to make sure that 'mask' is not the same
2040 * as any masks that are already on the list.
2042 void ovs_sw_flow_mask_insert(struct flow_table *tbl, struct sw_flow_mask *mask)
2044 list_add_rcu(&mask->list, tbl->mask_list);
2048 * Set 'range' fields in the mask to the value of 'val'.
2050 static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask,
2051 struct sw_flow_key_range *range, u8 val)
2053 u8 *m = (u8 *)&mask->key + range->start;
2055 mask->range = *range;
2056 memset(m, val, ovs_sw_flow_mask_size_roundup(mask));