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/tcp.h>
38 #include <linux/udp.h>
39 #include <linux/icmp.h>
40 #include <linux/icmpv6.h>
41 #include <linux/rculist.h>
44 #include <net/ndisc.h>
48 static struct kmem_cache *flow_cache;
50 static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask,
51 struct sw_flow_key_range *range, u8 val);
53 static void update_range__(struct sw_flow_match *match,
54 size_t offset, size_t size, bool is_mask)
56 struct sw_flow_key_range *range = NULL;
57 size_t start = offset;
58 size_t end = offset + size;
61 range = &match->range;
63 range = &match->mask->range;
68 if (range->start == range->end) {
74 if (range->start > start)
81 #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
83 update_range__(match, offsetof(struct sw_flow_key, field), \
84 sizeof((match)->key->field), is_mask); \
85 if (is_mask && match->mask != NULL) { \
86 (match)->mask->key.field = value; \
88 (match)->key->field = value; \
92 #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
94 update_range__(match, offsetof(struct sw_flow_key, field), \
96 if (is_mask && match->mask != NULL) { \
97 memcpy(&(match)->mask->key.field, value_p, len); \
99 memcpy(&(match)->key->field, value_p, len); \
103 void ovs_match_init(struct sw_flow_match *match,
104 struct sw_flow_key *key,
105 struct sw_flow_mask *mask)
107 memset(match, 0, sizeof(*match));
111 memset(key, 0, sizeof(*key));
114 memset(&mask->key, 0, sizeof(mask->key));
115 mask->range.start = mask->range.end = 0;
119 static bool ovs_match_validate(const struct sw_flow_match *match,
120 u64 key_attrs, u64 mask_attrs)
122 u64 key_expected = 1ULL << OVS_KEY_ATTR_ETHERNET;
123 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
125 /* The following mask attributes allowed only if they
126 * pass the validation tests. */
127 mask_allowed &= ~((1ULL << OVS_KEY_ATTR_IPV4)
128 | (1ULL << OVS_KEY_ATTR_IPV6)
129 | (1ULL << OVS_KEY_ATTR_TCP)
130 | (1ULL << OVS_KEY_ATTR_UDP)
131 | (1ULL << OVS_KEY_ATTR_ICMP)
132 | (1ULL << OVS_KEY_ATTR_ICMPV6)
133 | (1ULL << OVS_KEY_ATTR_ARP)
134 | (1ULL << OVS_KEY_ATTR_ND));
136 if (match->key->eth.type == htons(ETH_P_802_2) &&
137 match->mask && (match->mask->key.eth.type == htons(0xffff)))
138 mask_allowed |= (1ULL << OVS_KEY_ATTR_ETHERTYPE);
140 /* Check key attributes. */
141 if (match->key->eth.type == htons(ETH_P_ARP)
142 || match->key->eth.type == htons(ETH_P_RARP)) {
143 key_expected |= 1ULL << OVS_KEY_ATTR_ARP;
144 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
145 mask_allowed |= 1ULL << OVS_KEY_ATTR_ARP;
148 if (match->key->eth.type == htons(ETH_P_IP)) {
149 key_expected |= 1ULL << OVS_KEY_ATTR_IPV4;
150 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
151 mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV4;
153 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
154 if (match->key->ip.proto == IPPROTO_UDP) {
155 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
156 if (match->mask && (match->mask->key.ip.proto == 0xff))
157 mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
160 if (match->key->ip.proto == IPPROTO_TCP) {
161 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
162 if (match->mask && (match->mask->key.ip.proto == 0xff))
163 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
166 if (match->key->ip.proto == IPPROTO_ICMP) {
167 key_expected |= 1ULL << OVS_KEY_ATTR_ICMP;
168 if (match->mask && (match->mask->key.ip.proto == 0xff))
169 mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMP;
174 if (match->key->eth.type == htons(ETH_P_IPV6)) {
175 key_expected |= 1ULL << OVS_KEY_ATTR_IPV6;
176 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
177 mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV6;
179 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
180 if (match->key->ip.proto == IPPROTO_UDP) {
181 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
182 if (match->mask && (match->mask->key.ip.proto == 0xff))
183 mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
186 if (match->key->ip.proto == IPPROTO_TCP) {
187 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
188 if (match->mask && (match->mask->key.ip.proto == 0xff))
189 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
192 if (match->key->ip.proto == IPPROTO_ICMPV6) {
193 key_expected |= 1ULL << OVS_KEY_ATTR_ICMPV6;
194 if (match->mask && (match->mask->key.ip.proto == 0xff))
195 mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMPV6;
197 if (match->key->ipv6.tp.src ==
198 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
199 match->key->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
200 key_expected |= 1ULL << OVS_KEY_ATTR_ND;
201 if (match->mask && (match->mask->key.ipv6.tp.src == htons(0xffff)))
202 mask_allowed |= 1ULL << OVS_KEY_ATTR_ND;
208 if ((key_attrs & key_expected) != key_expected) {
209 /* Key attributes check failed. */
210 OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n",
211 key_attrs, key_expected);
215 if ((mask_attrs & mask_allowed) != mask_attrs) {
216 /* Mask attributes check failed. */
217 OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n",
218 mask_attrs, mask_allowed);
225 static int check_header(struct sk_buff *skb, int len)
227 if (unlikely(skb->len < len))
229 if (unlikely(!pskb_may_pull(skb, len)))
234 static bool arphdr_ok(struct sk_buff *skb)
236 return pskb_may_pull(skb, skb_network_offset(skb) +
237 sizeof(struct arp_eth_header));
240 static int check_iphdr(struct sk_buff *skb)
242 unsigned int nh_ofs = skb_network_offset(skb);
246 err = check_header(skb, nh_ofs + sizeof(struct iphdr));
250 ip_len = ip_hdrlen(skb);
251 if (unlikely(ip_len < sizeof(struct iphdr) ||
252 skb->len < nh_ofs + ip_len))
255 skb_set_transport_header(skb, nh_ofs + ip_len);
259 static bool tcphdr_ok(struct sk_buff *skb)
261 int th_ofs = skb_transport_offset(skb);
264 if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
267 tcp_len = tcp_hdrlen(skb);
268 if (unlikely(tcp_len < sizeof(struct tcphdr) ||
269 skb->len < th_ofs + tcp_len))
275 static bool udphdr_ok(struct sk_buff *skb)
277 return pskb_may_pull(skb, skb_transport_offset(skb) +
278 sizeof(struct udphdr));
281 static bool icmphdr_ok(struct sk_buff *skb)
283 return pskb_may_pull(skb, skb_transport_offset(skb) +
284 sizeof(struct icmphdr));
287 u64 ovs_flow_used_time(unsigned long flow_jiffies)
289 struct timespec cur_ts;
292 ktime_get_ts(&cur_ts);
293 idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
294 cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
295 cur_ts.tv_nsec / NSEC_PER_MSEC;
297 return cur_ms - idle_ms;
300 static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
302 unsigned int nh_ofs = skb_network_offset(skb);
310 err = check_header(skb, nh_ofs + sizeof(*nh));
315 nexthdr = nh->nexthdr;
316 payload_ofs = (u8 *)(nh + 1) - skb->data;
318 key->ip.proto = NEXTHDR_NONE;
319 key->ip.tos = ipv6_get_dsfield(nh);
320 key->ip.ttl = nh->hop_limit;
321 key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
322 key->ipv6.addr.src = nh->saddr;
323 key->ipv6.addr.dst = nh->daddr;
325 payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
326 if (unlikely(payload_ofs < 0))
330 if (frag_off & htons(~0x7))
331 key->ip.frag = OVS_FRAG_TYPE_LATER;
333 key->ip.frag = OVS_FRAG_TYPE_FIRST;
336 nh_len = payload_ofs - nh_ofs;
337 skb_set_transport_header(skb, nh_ofs + nh_len);
338 key->ip.proto = nexthdr;
342 static bool icmp6hdr_ok(struct sk_buff *skb)
344 return pskb_may_pull(skb, skb_transport_offset(skb) +
345 sizeof(struct icmp6hdr));
348 static void flow_key_mask(struct sw_flow_key *dst,
349 const struct sw_flow_key *src,
350 const struct sw_flow_mask *mask)
352 u8 *m = (u8 *)&mask->key + mask->range.start;
353 u8 *s = (u8 *)src + mask->range.start;
354 u8 *d = (u8 *)dst + mask->range.start;
357 memset(dst, 0, sizeof(*dst));
358 for (i = 0; i < ovs_sw_flow_mask_size_roundup(mask); i++) {
364 #define TCP_FLAGS_OFFSET 13
365 #define TCP_FLAG_MASK 0x3f
367 void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb)
371 if ((flow->key.eth.type == htons(ETH_P_IP) ||
372 flow->key.eth.type == htons(ETH_P_IPV6)) &&
373 flow->key.ip.proto == IPPROTO_TCP &&
374 likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) {
375 u8 *tcp = (u8 *)tcp_hdr(skb);
376 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
379 spin_lock(&flow->lock);
380 flow->used = jiffies;
381 flow->packet_count++;
382 flow->byte_count += skb->len;
383 flow->tcp_flags |= tcp_flags;
384 spin_unlock(&flow->lock);
387 struct sw_flow_actions *ovs_flow_actions_alloc(int size)
389 struct sw_flow_actions *sfa;
391 if (size > MAX_ACTIONS_BUFSIZE)
392 return ERR_PTR(-EINVAL);
394 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
396 return ERR_PTR(-ENOMEM);
398 sfa->actions_len = 0;
402 struct sw_flow *ovs_flow_alloc(void)
404 struct sw_flow *flow;
406 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
408 return ERR_PTR(-ENOMEM);
410 spin_lock_init(&flow->lock);
411 flow->sf_acts = NULL;
417 static struct hlist_head *find_bucket(struct flow_table *table, u32 hash)
419 hash = jhash_1word(hash, table->hash_seed);
420 return flex_array_get(table->buckets,
421 (hash & (table->n_buckets - 1)));
424 static struct flex_array *alloc_buckets(unsigned int n_buckets)
426 struct flex_array *buckets;
429 buckets = flex_array_alloc(sizeof(struct hlist_head *),
430 n_buckets, GFP_KERNEL);
434 err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
436 flex_array_free(buckets);
440 for (i = 0; i < n_buckets; i++)
441 INIT_HLIST_HEAD((struct hlist_head *)
442 flex_array_get(buckets, i));
447 static void free_buckets(struct flex_array *buckets)
449 flex_array_free(buckets);
452 static struct flow_table *__flow_tbl_alloc(int new_size)
454 struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL);
459 table->buckets = alloc_buckets(new_size);
461 if (!table->buckets) {
465 table->n_buckets = new_size;
468 table->keep_flows = false;
469 get_random_bytes(&table->hash_seed, sizeof(u32));
470 table->mask_list = NULL;
475 static void __flow_tbl_destroy(struct flow_table *table)
479 if (table->keep_flows)
482 for (i = 0; i < table->n_buckets; i++) {
483 struct sw_flow *flow;
484 struct hlist_head *head = flex_array_get(table->buckets, i);
485 struct hlist_node *n;
486 int ver = table->node_ver;
488 hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
489 hlist_del_rcu(&flow->hash_node[ver]);
490 ovs_flow_free(flow, false);
494 BUG_ON(!list_empty(table->mask_list));
495 kfree(table->mask_list);
498 free_buckets(table->buckets);
502 struct flow_table *ovs_flow_tbl_alloc(int new_size)
504 struct flow_table *table = __flow_tbl_alloc(new_size);
509 table->mask_list = kmalloc(sizeof(struct list_head), GFP_KERNEL);
510 if (!table->mask_list) {
511 table->keep_flows = true;
512 __flow_tbl_destroy(table);
515 INIT_LIST_HEAD(table->mask_list);
520 static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
522 struct flow_table *table = container_of(rcu, struct flow_table, rcu);
524 __flow_tbl_destroy(table);
527 void ovs_flow_tbl_destroy(struct flow_table *table, bool deferred)
533 call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb);
535 __flow_tbl_destroy(table);
538 struct sw_flow *ovs_flow_dump_next(struct flow_table *table, u32 *bucket, u32 *last)
540 struct sw_flow *flow;
541 struct hlist_head *head;
545 ver = table->node_ver;
546 while (*bucket < table->n_buckets) {
548 head = flex_array_get(table->buckets, *bucket);
549 hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
564 static void __tbl_insert(struct flow_table *table, struct sw_flow *flow)
566 struct hlist_head *head;
568 head = find_bucket(table, flow->hash);
569 hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
574 static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new)
579 old_ver = old->node_ver;
580 new->node_ver = !old_ver;
582 /* Insert in new table. */
583 for (i = 0; i < old->n_buckets; i++) {
584 struct sw_flow *flow;
585 struct hlist_head *head;
587 head = flex_array_get(old->buckets, i);
589 hlist_for_each_entry(flow, head, hash_node[old_ver])
590 __tbl_insert(new, flow);
593 new->mask_list = old->mask_list;
594 old->keep_flows = true;
597 static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets)
599 struct flow_table *new_table;
601 new_table = __flow_tbl_alloc(n_buckets);
603 return ERR_PTR(-ENOMEM);
605 flow_table_copy_flows(table, new_table);
610 struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table)
612 return __flow_tbl_rehash(table, table->n_buckets);
615 struct flow_table *ovs_flow_tbl_expand(struct flow_table *table)
617 return __flow_tbl_rehash(table, table->n_buckets * 2);
620 static void __flow_free(struct sw_flow *flow)
622 kfree((struct sf_flow_acts __force *)flow->sf_acts);
623 kmem_cache_free(flow_cache, flow);
626 static void rcu_free_flow_callback(struct rcu_head *rcu)
628 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
633 void ovs_flow_free(struct sw_flow *flow, bool deferred)
638 ovs_sw_flow_mask_del_ref((struct sw_flow_mask __force *)flow->mask,
642 call_rcu(&flow->rcu, rcu_free_flow_callback);
647 /* RCU callback used by ovs_flow_deferred_free_acts. */
648 static void rcu_free_acts_callback(struct rcu_head *rcu)
650 struct sw_flow_actions *sf_acts = container_of(rcu,
651 struct sw_flow_actions, rcu);
655 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
656 * The caller must hold rcu_read_lock for this to be sensible. */
657 void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
659 call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
662 static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
665 __be16 eth_type; /* ETH_P_8021Q */
668 struct qtag_prefix *qp;
670 if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
673 if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
677 qp = (struct qtag_prefix *) skb->data;
678 key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
679 __skb_pull(skb, sizeof(struct qtag_prefix));
684 static __be16 parse_ethertype(struct sk_buff *skb)
686 struct llc_snap_hdr {
687 u8 dsap; /* Always 0xAA */
688 u8 ssap; /* Always 0xAA */
693 struct llc_snap_hdr *llc;
696 proto = *(__be16 *) skb->data;
697 __skb_pull(skb, sizeof(__be16));
699 if (ntohs(proto) >= ETH_P_802_3_MIN)
702 if (skb->len < sizeof(struct llc_snap_hdr))
703 return htons(ETH_P_802_2);
705 if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
708 llc = (struct llc_snap_hdr *) skb->data;
709 if (llc->dsap != LLC_SAP_SNAP ||
710 llc->ssap != LLC_SAP_SNAP ||
711 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
712 return htons(ETH_P_802_2);
714 __skb_pull(skb, sizeof(struct llc_snap_hdr));
716 if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
717 return llc->ethertype;
719 return htons(ETH_P_802_2);
722 static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
725 struct icmp6hdr *icmp = icmp6_hdr(skb);
727 /* The ICMPv6 type and code fields use the 16-bit transport port
728 * fields, so we need to store them in 16-bit network byte order.
730 key->ipv6.tp.src = htons(icmp->icmp6_type);
731 key->ipv6.tp.dst = htons(icmp->icmp6_code);
733 if (icmp->icmp6_code == 0 &&
734 (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
735 icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
736 int icmp_len = skb->len - skb_transport_offset(skb);
740 /* In order to process neighbor discovery options, we need the
743 if (unlikely(icmp_len < sizeof(*nd)))
746 if (unlikely(skb_linearize(skb)))
749 nd = (struct nd_msg *)skb_transport_header(skb);
750 key->ipv6.nd.target = nd->target;
752 icmp_len -= sizeof(*nd);
754 while (icmp_len >= 8) {
755 struct nd_opt_hdr *nd_opt =
756 (struct nd_opt_hdr *)(nd->opt + offset);
757 int opt_len = nd_opt->nd_opt_len * 8;
759 if (unlikely(!opt_len || opt_len > icmp_len))
762 /* Store the link layer address if the appropriate
763 * option is provided. It is considered an error if
764 * the same link layer option is specified twice.
766 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
768 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
770 memcpy(key->ipv6.nd.sll,
771 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
772 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
774 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
776 memcpy(key->ipv6.nd.tll,
777 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
788 memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
789 memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
790 memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
796 * ovs_flow_extract - extracts a flow key from an Ethernet frame.
797 * @skb: sk_buff that contains the frame, with skb->data pointing to the
799 * @in_port: port number on which @skb was received.
800 * @key: output flow key
801 * @key_lenp: length of output flow key
803 * The caller must ensure that skb->len >= ETH_HLEN.
805 * Returns 0 if successful, otherwise a negative errno value.
807 * Initializes @skb header pointers as follows:
809 * - skb->mac_header: the Ethernet header.
811 * - skb->network_header: just past the Ethernet header, or just past the
812 * VLAN header, to the first byte of the Ethernet payload.
814 * - skb->transport_header: If key->dl_type is ETH_P_IP or ETH_P_IPV6
815 * on output, then just past the IP header, if one is present and
816 * of a correct length, otherwise the same as skb->network_header.
817 * For other key->dl_type values it is left untouched.
819 int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
824 memset(key, 0, sizeof(*key));
826 key->phy.priority = skb->priority;
827 if (OVS_CB(skb)->tun_key)
828 memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key));
829 key->phy.in_port = in_port;
830 key->phy.skb_mark = skb_get_mark(skb);
832 skb_reset_mac_header(skb);
834 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
835 * header in the linear data area.
838 memcpy(key->eth.src, eth->h_source, ETH_ALEN);
839 memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
841 __skb_pull(skb, 2 * ETH_ALEN);
843 if (vlan_tx_tag_present(skb))
844 key->eth.tci = htons(vlan_get_tci(skb));
845 else if (eth->h_proto == htons(ETH_P_8021Q))
846 if (unlikely(parse_vlan(skb, key)))
849 key->eth.type = parse_ethertype(skb);
850 if (unlikely(key->eth.type == htons(0)))
853 skb_reset_network_header(skb);
854 __skb_push(skb, skb->data - skb_mac_header(skb));
857 if (key->eth.type == htons(ETH_P_IP)) {
861 error = check_iphdr(skb);
862 if (unlikely(error)) {
863 if (error == -EINVAL) {
864 skb->transport_header = skb->network_header;
871 key->ipv4.addr.src = nh->saddr;
872 key->ipv4.addr.dst = nh->daddr;
874 key->ip.proto = nh->protocol;
875 key->ip.tos = nh->tos;
876 key->ip.ttl = nh->ttl;
878 offset = nh->frag_off & htons(IP_OFFSET);
880 key->ip.frag = OVS_FRAG_TYPE_LATER;
883 if (nh->frag_off & htons(IP_MF) ||
884 skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
885 key->ip.frag = OVS_FRAG_TYPE_FIRST;
887 /* Transport layer. */
888 if (key->ip.proto == IPPROTO_TCP) {
889 if (tcphdr_ok(skb)) {
890 struct tcphdr *tcp = tcp_hdr(skb);
891 key->ipv4.tp.src = tcp->source;
892 key->ipv4.tp.dst = tcp->dest;
894 } else if (key->ip.proto == IPPROTO_UDP) {
895 if (udphdr_ok(skb)) {
896 struct udphdr *udp = udp_hdr(skb);
897 key->ipv4.tp.src = udp->source;
898 key->ipv4.tp.dst = udp->dest;
900 } else if (key->ip.proto == IPPROTO_ICMP) {
901 if (icmphdr_ok(skb)) {
902 struct icmphdr *icmp = icmp_hdr(skb);
903 /* The ICMP type and code fields use the 16-bit
904 * transport port fields, so we need to store
905 * them in 16-bit network byte order. */
906 key->ipv4.tp.src = htons(icmp->type);
907 key->ipv4.tp.dst = htons(icmp->code);
911 } else if ((key->eth.type == htons(ETH_P_ARP) ||
912 key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) {
913 struct arp_eth_header *arp;
915 arp = (struct arp_eth_header *)skb_network_header(skb);
917 if (arp->ar_hrd == htons(ARPHRD_ETHER)
918 && arp->ar_pro == htons(ETH_P_IP)
919 && arp->ar_hln == ETH_ALEN
920 && arp->ar_pln == 4) {
922 /* We only match on the lower 8 bits of the opcode. */
923 if (ntohs(arp->ar_op) <= 0xff)
924 key->ip.proto = ntohs(arp->ar_op);
925 memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
926 memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
927 memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
928 memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
930 } else if (key->eth.type == htons(ETH_P_IPV6)) {
931 int nh_len; /* IPv6 Header + Extensions */
933 nh_len = parse_ipv6hdr(skb, key);
934 if (unlikely(nh_len < 0)) {
935 if (nh_len == -EINVAL) {
936 skb->transport_header = skb->network_header;
944 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
946 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
947 key->ip.frag = OVS_FRAG_TYPE_FIRST;
949 /* Transport layer. */
950 if (key->ip.proto == NEXTHDR_TCP) {
951 if (tcphdr_ok(skb)) {
952 struct tcphdr *tcp = tcp_hdr(skb);
953 key->ipv6.tp.src = tcp->source;
954 key->ipv6.tp.dst = tcp->dest;
956 } else if (key->ip.proto == NEXTHDR_UDP) {
957 if (udphdr_ok(skb)) {
958 struct udphdr *udp = udp_hdr(skb);
959 key->ipv6.tp.src = udp->source;
960 key->ipv6.tp.dst = udp->dest;
962 } else if (key->ip.proto == NEXTHDR_ICMP) {
963 if (icmp6hdr_ok(skb)) {
964 error = parse_icmpv6(skb, key, nh_len);
974 static u32 ovs_flow_hash(const struct sw_flow_key *key, int key_start, int key_len)
976 return jhash2((u32 *)((u8 *)key + key_start),
977 DIV_ROUND_UP(key_len - key_start, sizeof(u32)), 0);
980 static int flow_key_start(const struct sw_flow_key *key)
982 if (key->tun_key.ipv4_dst)
985 return offsetof(struct sw_flow_key, phy);
988 static bool __cmp_key(const struct sw_flow_key *key1,
989 const struct sw_flow_key *key2, int key_start, int key_len)
991 return !memcmp((u8 *)key1 + key_start,
992 (u8 *)key2 + key_start, (key_len - key_start));
995 static bool __flow_cmp_key(const struct sw_flow *flow,
996 const struct sw_flow_key *key, int key_start, int key_len)
998 return __cmp_key(&flow->key, key, key_start, key_len);
1001 static bool __flow_cmp_unmasked_key(const struct sw_flow *flow,
1002 const struct sw_flow_key *key, int key_start, int key_len)
1004 return __cmp_key(&flow->unmasked_key, key, key_start, key_len);
1007 bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
1008 const struct sw_flow_key *key, int key_len)
1011 key_start = flow_key_start(key);
1013 return __flow_cmp_unmasked_key(flow, key, key_start, key_len);
1017 struct sw_flow *ovs_flow_lookup_unmasked_key(struct flow_table *table,
1018 struct sw_flow_match *match)
1020 struct sw_flow_key *unmasked = match->key;
1021 int key_len = match->range.end;
1022 struct sw_flow *flow;
1024 flow = ovs_flow_lookup(table, unmasked);
1025 if (flow && (!ovs_flow_cmp_unmasked_key(flow, unmasked, key_len)))
1031 static struct sw_flow *ovs_masked_flow_lookup(struct flow_table *table,
1032 const struct sw_flow_key *flow_key,
1033 struct sw_flow_mask *mask)
1035 struct sw_flow *flow;
1036 struct hlist_head *head;
1037 int key_start = mask->range.start;
1038 int key_len = mask->range.end;
1040 struct sw_flow_key masked_key;
1042 flow_key_mask(&masked_key, flow_key, mask);
1043 hash = ovs_flow_hash(&masked_key, key_start, key_len);
1044 head = find_bucket(table, hash);
1045 hlist_for_each_entry_rcu(flow, head, hash_node[table->node_ver]) {
1046 if (__flow_cmp_key(flow, &masked_key, key_start, key_len))
1052 struct sw_flow *ovs_flow_lookup(struct flow_table *tbl,
1053 const struct sw_flow_key *key)
1055 struct sw_flow *flow = NULL;
1056 struct sw_flow_mask *mask;
1058 list_for_each_entry_rcu(mask, tbl->mask_list, list) {
1059 flow = ovs_masked_flow_lookup(tbl, key, mask);
1060 if (flow) /* Found */
1068 void ovs_flow_insert(struct flow_table *table, struct sw_flow *flow,
1069 const struct sw_flow_key *key, int key_len)
1071 flow->unmasked_key = *key;
1072 flow_key_mask(&flow->key, &flow->unmasked_key, ovsl_dereference(flow->mask));
1073 flow->hash = ovs_flow_hash(&flow->key,
1074 ovsl_dereference(flow->mask)->range.start,
1075 ovsl_dereference(flow->mask)->range.end);
1076 __tbl_insert(table, flow);
1079 void ovs_flow_remove(struct flow_table *table, struct sw_flow *flow)
1081 BUG_ON(table->count == 0);
1082 hlist_del_rcu(&flow->hash_node[table->node_ver]);
1086 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
1087 const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
1088 [OVS_KEY_ATTR_ENCAP] = -1,
1089 [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
1090 [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
1091 [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
1092 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
1093 [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
1094 [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
1095 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
1096 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
1097 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
1098 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
1099 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
1100 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
1101 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
1102 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
1103 [OVS_KEY_ATTR_TUNNEL] = -1,
1106 static bool is_all_zero(const u8 *fp, size_t size)
1113 for (i = 0; i < size; i++)
1120 static int __parse_flow_nlattrs(const struct nlattr *attr,
1121 const struct nlattr *a[],
1122 u64 *attrsp, bool nz)
1124 const struct nlattr *nla;
1129 nla_for_each_nested(nla, attr, rem) {
1130 u16 type = nla_type(nla);
1133 if (type > OVS_KEY_ATTR_MAX) {
1134 OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
1135 type, OVS_KEY_ATTR_MAX);
1138 if (attrs & (1ULL << type)) {
1139 OVS_NLERR("Duplicate key attribute (type %d).\n", type);
1143 expected_len = ovs_key_lens[type];
1144 if (nla_len(nla) != expected_len && expected_len != -1) {
1145 OVS_NLERR("Key attribute has unexpected length (type=%d"
1146 ", length=%d, expected=%d).\n", type,
1147 nla_len(nla), expected_len);
1151 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
1152 attrs |= 1ULL << type;
1157 OVS_NLERR("Message has %d unknown bytes.\n", rem);
1165 static int parse_flow_mask_nlattrs(const struct nlattr *attr,
1166 const struct nlattr *a[], u64 *attrsp)
1168 return __parse_flow_nlattrs(attr, a, attrsp, true);
1171 static int parse_flow_nlattrs(const struct nlattr *attr,
1172 const struct nlattr *a[], u64 *attrsp)
1174 return __parse_flow_nlattrs(attr, a, attrsp, false);
1177 int ipv4_tun_from_nlattr(const struct nlattr *attr,
1178 struct sw_flow_match *match, bool is_mask)
1185 nla_for_each_nested(a, attr, rem) {
1186 int type = nla_type(a);
1187 static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
1188 [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
1189 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
1190 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
1191 [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
1192 [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
1193 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
1194 [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
1197 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
1198 OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d)\n",
1199 type, OVS_TUNNEL_KEY_ATTR_MAX);
1203 if (ovs_tunnel_key_lens[type] != nla_len(a)) {
1204 OVS_NLERR("IPv4 tunnel attribute type has unexpected "
1205 " legnth (type=%d, length=%d, expected=%d.)\n",
1206 type, nla_len(a), ovs_tunnel_key_lens[type]);
1211 case OVS_TUNNEL_KEY_ATTR_ID:
1212 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
1213 nla_get_be64(a), is_mask);
1214 tun_flags |= OVS_TNL_F_KEY;
1216 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1217 SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
1218 nla_get_be32(a), is_mask);
1220 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1221 SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
1222 nla_get_be32(a), is_mask);
1224 case OVS_TUNNEL_KEY_ATTR_TOS:
1225 SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
1226 nla_get_u8(a), is_mask);
1228 case OVS_TUNNEL_KEY_ATTR_TTL:
1229 SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
1230 nla_get_u8(a), is_mask);
1233 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1234 tun_flags |= OVS_TNL_F_DONT_FRAGMENT;
1236 case OVS_TUNNEL_KEY_ATTR_CSUM:
1237 tun_flags |= OVS_TNL_F_CSUM;
1244 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
1247 OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
1251 if (!match->key->tun_key.ipv4_dst) {
1252 OVS_NLERR("IPv4 tunnel destination address is zero.\n");
1257 OVS_NLERR("IPv4 tunnel TTL is zero.\n");
1264 int ipv4_tun_to_nlattr(struct sk_buff *skb,
1265 const struct ovs_key_ipv4_tunnel *tun_key,
1266 const struct ovs_key_ipv4_tunnel *output)
1270 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
1274 if (tun_key->tun_flags & OVS_TNL_F_KEY &&
1275 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
1277 if (tun_key->ipv4_src &&
1278 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
1280 if (nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
1282 if (tun_key->ipv4_tos &&
1283 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
1285 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
1287 if ((tun_key->tun_flags & OVS_TNL_F_DONT_FRAGMENT) &&
1288 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
1290 if ((tun_key->tun_flags & OVS_TNL_F_CSUM) &&
1291 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
1294 nla_nest_end(skb, nla);
1299 static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
1300 const struct nlattr **a, bool is_mask)
1302 if (*attrs & (1ULL << OVS_KEY_ATTR_PRIORITY)) {
1303 SW_FLOW_KEY_PUT(match, phy.priority,
1304 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
1305 *attrs &= ~(1ULL << OVS_KEY_ATTR_PRIORITY);
1308 if (*attrs & (1ULL << OVS_KEY_ATTR_IN_PORT)) {
1309 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1311 if (!is_mask && in_port >= DP_MAX_PORTS)
1313 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
1314 *attrs &= ~(1ULL << OVS_KEY_ATTR_IN_PORT);
1317 if (*attrs & (1ULL << OVS_KEY_ATTR_SKB_MARK)) {
1318 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
1319 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER)
1320 if (!is_mask && mark != 0) {
1321 OVS_NLERR("skb->mark must be zero on this kernel (mark=%d).\n", mark);
1325 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
1326 *attrs &= ~(1ULL << OVS_KEY_ATTR_SKB_MARK);
1328 if (*attrs & (1ULL << OVS_KEY_ATTR_TUNNEL)) {
1329 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
1332 *attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL);
1337 static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
1338 const struct nlattr **a, bool is_mask)
1341 u64 orig_attrs = attrs;
1343 err = metadata_from_nlattrs(match, &attrs, a, is_mask);
1347 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) {
1348 const struct ovs_key_ethernet *eth_key;
1350 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1351 SW_FLOW_KEY_MEMCPY(match, eth.src,
1352 eth_key->eth_src, ETH_ALEN, is_mask);
1353 SW_FLOW_KEY_MEMCPY(match, eth.dst,
1354 eth_key->eth_dst, ETH_ALEN, is_mask);
1355 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERNET);
1358 if (attrs & (1ULL << OVS_KEY_ATTR_VLAN)) {
1361 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1363 if (!(tci & htons(VLAN_TAG_PRESENT))) {
1364 OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
1368 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
1369 attrs &= ~(1ULL << OVS_KEY_ATTR_VLAN);
1372 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) {
1375 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1376 if (!is_mask && ntohs(eth_type) < ETH_P_802_3_MIN) {
1377 OVS_NLERR("EtherType is less than mimimum (type=%x, min=%x).\n",
1378 ntohs(eth_type), ETH_P_802_3_MIN);
1382 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
1383 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1384 } else if (!is_mask) {
1385 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
1388 if (attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1389 const struct ovs_key_ipv4 *ipv4_key;
1391 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1392 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
1393 OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
1394 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
1397 SW_FLOW_KEY_PUT(match, ip.proto,
1398 ipv4_key->ipv4_proto, is_mask);
1399 SW_FLOW_KEY_PUT(match, ip.tos,
1400 ipv4_key->ipv4_tos, is_mask);
1401 SW_FLOW_KEY_PUT(match, ip.ttl,
1402 ipv4_key->ipv4_ttl, is_mask);
1403 SW_FLOW_KEY_PUT(match, ip.frag,
1404 ipv4_key->ipv4_frag, is_mask);
1405 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1406 ipv4_key->ipv4_src, is_mask);
1407 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1408 ipv4_key->ipv4_dst, is_mask);
1409 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV4);
1412 if (attrs & (1ULL << OVS_KEY_ATTR_IPV6)) {
1413 const struct ovs_key_ipv6 *ipv6_key;
1415 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1416 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
1417 OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
1418 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
1421 SW_FLOW_KEY_PUT(match, ipv6.label,
1422 ipv6_key->ipv6_label, is_mask);
1423 SW_FLOW_KEY_PUT(match, ip.proto,
1424 ipv6_key->ipv6_proto, is_mask);
1425 SW_FLOW_KEY_PUT(match, ip.tos,
1426 ipv6_key->ipv6_tclass, is_mask);
1427 SW_FLOW_KEY_PUT(match, ip.ttl,
1428 ipv6_key->ipv6_hlimit, is_mask);
1429 SW_FLOW_KEY_PUT(match, ip.frag,
1430 ipv6_key->ipv6_frag, is_mask);
1431 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
1433 sizeof(match->key->ipv6.addr.src),
1435 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
1437 sizeof(match->key->ipv6.addr.dst),
1440 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV6);
1443 if (attrs & (1ULL << OVS_KEY_ATTR_ARP)) {
1444 const struct ovs_key_arp *arp_key;
1446 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1447 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
1448 OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
1453 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1454 arp_key->arp_sip, is_mask);
1455 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1456 arp_key->arp_tip, is_mask);
1457 SW_FLOW_KEY_PUT(match, ip.proto,
1458 ntohs(arp_key->arp_op), is_mask);
1459 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
1460 arp_key->arp_sha, ETH_ALEN, is_mask);
1461 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
1462 arp_key->arp_tha, ETH_ALEN, is_mask);
1464 attrs &= ~(1ULL << OVS_KEY_ATTR_ARP);
1467 if (attrs & (1ULL << OVS_KEY_ATTR_TCP)) {
1468 const struct ovs_key_tcp *tcp_key;
1470 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
1471 if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1472 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1473 tcp_key->tcp_src, is_mask);
1474 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1475 tcp_key->tcp_dst, is_mask);
1477 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1478 tcp_key->tcp_src, is_mask);
1479 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1480 tcp_key->tcp_dst, is_mask);
1482 attrs &= ~(1ULL << OVS_KEY_ATTR_TCP);
1485 if (attrs & (1ULL << OVS_KEY_ATTR_UDP)) {
1486 const struct ovs_key_udp *udp_key;
1488 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
1489 if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1490 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1491 udp_key->udp_src, is_mask);
1492 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1493 udp_key->udp_dst, is_mask);
1495 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1496 udp_key->udp_src, is_mask);
1497 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1498 udp_key->udp_dst, is_mask);
1500 attrs &= ~(1ULL << OVS_KEY_ATTR_UDP);
1503 if (attrs & (1ULL << OVS_KEY_ATTR_ICMP)) {
1504 const struct ovs_key_icmp *icmp_key;
1506 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
1507 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1508 htons(icmp_key->icmp_type), is_mask);
1509 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1510 htons(icmp_key->icmp_code), is_mask);
1511 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMP);
1514 if (attrs & (1ULL << OVS_KEY_ATTR_ICMPV6)) {
1515 const struct ovs_key_icmpv6 *icmpv6_key;
1517 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
1518 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1519 htons(icmpv6_key->icmpv6_type), is_mask);
1520 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1521 htons(icmpv6_key->icmpv6_code), is_mask);
1522 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMPV6);
1525 if (attrs & (1ULL << OVS_KEY_ATTR_ND)) {
1526 const struct ovs_key_nd *nd_key;
1528 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1529 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1531 sizeof(match->key->ipv6.nd.target),
1533 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1534 nd_key->nd_sll, ETH_ALEN, is_mask);
1535 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1536 nd_key->nd_tll, ETH_ALEN, is_mask);
1537 attrs &= ~(1ULL << OVS_KEY_ATTR_ND);
1547 * ovs_match_from_nlattrs - parses Netlink attributes into a flow key and
1548 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1549 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1550 * does not include any don't care bit.
1551 * @match: receives the extracted flow match information.
1552 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1553 * sequence. The fields should of the packet that triggered the creation
1555 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1556 * attribute specifies the mask field of the wildcarded flow.
1558 int ovs_match_from_nlattrs(struct sw_flow_match *match,
1559 const struct nlattr *key,
1560 const struct nlattr *mask)
1562 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1563 const struct nlattr *encap;
1566 bool encap_valid = false;
1569 err = parse_flow_nlattrs(key, a, &key_attrs);
1573 if (key_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) {
1574 encap = a[OVS_KEY_ATTR_ENCAP];
1575 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
1576 if (nla_len(encap)) {
1577 __be16 eth_type = 0; /* ETH_P_8021Q */
1579 if (a[OVS_KEY_ATTR_ETHERTYPE])
1580 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1582 if ((eth_type == htons(ETH_P_8021Q)) && (a[OVS_KEY_ATTR_VLAN])) {
1584 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1585 err = parse_flow_nlattrs(encap, a, &key_attrs);
1587 OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
1596 err = ovs_key_from_nlattrs(match, key_attrs, a, false);
1601 err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
1605 if ((mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) && encap_valid) {
1606 __be16 eth_type = 0;
1608 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
1609 if (a[OVS_KEY_ATTR_ETHERTYPE])
1610 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1611 if (eth_type == htons(0xffff)) {
1612 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1613 encap = a[OVS_KEY_ATTR_ENCAP];
1614 err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
1616 OVS_NLERR("VLAN frames must have an exact match"
1617 " on the TPID (mask=%x).\n",
1626 err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
1630 /* Populate exact match flow's key mask. */
1632 ovs_sw_flow_mask_set(match->mask, &match->range, 0xff);
1635 if (!ovs_match_validate(match, key_attrs, mask_attrs))
1642 * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
1643 * @flow: Receives extracted in_port, priority, tun_key and skb_mark.
1644 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1647 * This parses a series of Netlink attributes that form a flow key, which must
1648 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1649 * get the metadata, that is, the parts of the flow key that cannot be
1650 * extracted from the packet itself.
1653 int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow,
1654 const struct nlattr *attr)
1656 struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key;
1657 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1660 struct sw_flow_match match;
1662 flow->key.phy.in_port = DP_MAX_PORTS;
1663 flow->key.phy.priority = 0;
1664 flow->key.phy.skb_mark = 0;
1665 memset(tun_key, 0, sizeof(flow->key.tun_key));
1667 err = parse_flow_nlattrs(attr, a, &attrs);
1671 memset(&match, 0, sizeof(match));
1672 match.key = &flow->key;
1674 err = metadata_from_nlattrs(&match, &attrs, a, false);
1681 int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey,
1682 const struct sw_flow_key *output, struct sk_buff *skb)
1684 struct ovs_key_ethernet *eth_key;
1685 struct nlattr *nla, *encap;
1687 if (swkey->phy.priority &&
1688 nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1689 goto nla_put_failure;
1691 if (swkey->tun_key.ipv4_dst &&
1692 ipv4_tun_to_nlattr(skb, &swkey->tun_key, &output->tun_key))
1693 goto nla_put_failure;
1695 if (swkey->phy.in_port != DP_MAX_PORTS) {
1696 /* Exact match upper 16 bits. */
1698 upper_u16 = (swkey == output) ? 0 : 0xffff;
1700 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1701 (upper_u16 << 16) | output->phy.in_port))
1702 goto nla_put_failure;
1705 if (swkey->phy.skb_mark &&
1706 nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1707 goto nla_put_failure;
1709 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1711 goto nla_put_failure;
1713 eth_key = nla_data(nla);
1714 memcpy(eth_key->eth_src, output->eth.src, ETH_ALEN);
1715 memcpy(eth_key->eth_dst, output->eth.dst, ETH_ALEN);
1717 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1719 eth_type = (swkey == output) ? htons(ETH_P_8021Q) : htons(0xffff) ;
1720 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1721 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1722 goto nla_put_failure;
1723 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1724 if (!swkey->eth.tci)
1729 if ((swkey == output) && (swkey->eth.type == htons(ETH_P_802_2)))
1732 if (output->eth.type != 0)
1733 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1734 goto nla_put_failure;
1736 if (swkey->eth.type == htons(ETH_P_IP)) {
1737 struct ovs_key_ipv4 *ipv4_key;
1739 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1741 goto nla_put_failure;
1742 ipv4_key = nla_data(nla);
1743 ipv4_key->ipv4_src = output->ipv4.addr.src;
1744 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1745 ipv4_key->ipv4_proto = output->ip.proto;
1746 ipv4_key->ipv4_tos = output->ip.tos;
1747 ipv4_key->ipv4_ttl = output->ip.ttl;
1748 ipv4_key->ipv4_frag = output->ip.frag;
1749 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1750 struct ovs_key_ipv6 *ipv6_key;
1752 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1754 goto nla_put_failure;
1755 ipv6_key = nla_data(nla);
1756 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1757 sizeof(ipv6_key->ipv6_src));
1758 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1759 sizeof(ipv6_key->ipv6_dst));
1760 ipv6_key->ipv6_label = output->ipv6.label;
1761 ipv6_key->ipv6_proto = output->ip.proto;
1762 ipv6_key->ipv6_tclass = output->ip.tos;
1763 ipv6_key->ipv6_hlimit = output->ip.ttl;
1764 ipv6_key->ipv6_frag = output->ip.frag;
1765 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1766 swkey->eth.type == htons(ETH_P_RARP)) {
1767 struct ovs_key_arp *arp_key;
1769 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1771 goto nla_put_failure;
1772 arp_key = nla_data(nla);
1773 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1774 arp_key->arp_sip = output->ipv4.addr.src;
1775 arp_key->arp_tip = output->ipv4.addr.dst;
1776 arp_key->arp_op = htons(output->ip.proto);
1777 memcpy(arp_key->arp_sha, output->ipv4.arp.sha, ETH_ALEN);
1778 memcpy(arp_key->arp_tha, output->ipv4.arp.tha, ETH_ALEN);
1781 if ((swkey->eth.type == htons(ETH_P_IP) ||
1782 swkey->eth.type == htons(ETH_P_IPV6)) &&
1783 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1785 if (swkey->ip.proto == IPPROTO_TCP) {
1786 struct ovs_key_tcp *tcp_key;
1788 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1790 goto nla_put_failure;
1791 tcp_key = nla_data(nla);
1792 if (swkey->eth.type == htons(ETH_P_IP)) {
1793 tcp_key->tcp_src = output->ipv4.tp.src;
1794 tcp_key->tcp_dst = output->ipv4.tp.dst;
1795 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1796 tcp_key->tcp_src = output->ipv6.tp.src;
1797 tcp_key->tcp_dst = output->ipv6.tp.dst;
1799 } else if (swkey->ip.proto == IPPROTO_UDP) {
1800 struct ovs_key_udp *udp_key;
1802 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1804 goto nla_put_failure;
1805 udp_key = nla_data(nla);
1806 if (swkey->eth.type == htons(ETH_P_IP)) {
1807 udp_key->udp_src = output->ipv4.tp.src;
1808 udp_key->udp_dst = output->ipv4.tp.dst;
1809 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1810 udp_key->udp_src = output->ipv6.tp.src;
1811 udp_key->udp_dst = output->ipv6.tp.dst;
1813 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1814 swkey->ip.proto == IPPROTO_ICMP) {
1815 struct ovs_key_icmp *icmp_key;
1817 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1819 goto nla_put_failure;
1820 icmp_key = nla_data(nla);
1821 icmp_key->icmp_type = ntohs(output->ipv4.tp.src);
1822 icmp_key->icmp_code = ntohs(output->ipv4.tp.dst);
1823 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1824 swkey->ip.proto == IPPROTO_ICMPV6) {
1825 struct ovs_key_icmpv6 *icmpv6_key;
1827 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1828 sizeof(*icmpv6_key));
1830 goto nla_put_failure;
1831 icmpv6_key = nla_data(nla);
1832 icmpv6_key->icmpv6_type = ntohs(output->ipv6.tp.src);
1833 icmpv6_key->icmpv6_code = ntohs(output->ipv6.tp.dst);
1835 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1836 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1837 struct ovs_key_nd *nd_key;
1839 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1841 goto nla_put_failure;
1842 nd_key = nla_data(nla);
1843 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1844 sizeof(nd_key->nd_target));
1845 memcpy(nd_key->nd_sll, output->ipv6.nd.sll, ETH_ALEN);
1846 memcpy(nd_key->nd_tll, output->ipv6.nd.tll, ETH_ALEN);
1853 nla_nest_end(skb, encap);
1861 /* Initializes the flow module.
1862 * Returns zero if successful or a negative error code. */
1863 int ovs_flow_init(void)
1865 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
1867 if (flow_cache == NULL)
1873 /* Uninitializes the flow module. */
1874 void ovs_flow_exit(void)
1876 kmem_cache_destroy(flow_cache);
1879 struct sw_flow_mask *ovs_sw_flow_mask_alloc(void)
1881 struct sw_flow_mask *mask;
1883 mask = kmalloc(sizeof(*mask), GFP_KERNEL);
1885 mask->ref_count = 0;
1890 void ovs_sw_flow_mask_add_ref(struct sw_flow_mask *mask)
1895 static void rcu_free_sw_flow_mask_cb(struct rcu_head *rcu)
1897 struct sw_flow_mask *mask = container_of(rcu, struct sw_flow_mask, rcu);
1902 void ovs_sw_flow_mask_del_ref(struct sw_flow_mask *mask, bool deferred)
1907 BUG_ON(!mask->ref_count);
1910 if (!mask->ref_count) {
1911 list_del_rcu(&mask->list);
1913 call_rcu(&mask->rcu, rcu_free_sw_flow_mask_cb);
1919 static bool ovs_sw_flow_mask_equal(const struct sw_flow_mask *a,
1920 const struct sw_flow_mask *b)
1922 u8 *a_ = (u8 *)&a->key + a->range.start;
1923 u8 *b_ = (u8 *)&b->key + b->range.start;
1925 return (a->range.end == b->range.end)
1926 && (a->range.start == b->range.start)
1927 && (memcmp(a_, b_, ovs_sw_flow_mask_actual_size(a)) == 0);
1930 struct sw_flow_mask *ovs_sw_flow_mask_find(const struct flow_table *tbl,
1931 const struct sw_flow_mask *mask)
1933 struct list_head *ml;
1935 list_for_each(ml, tbl->mask_list) {
1936 struct sw_flow_mask *m;
1937 m = container_of(ml, struct sw_flow_mask, list);
1938 if (ovs_sw_flow_mask_equal(mask, m))
1946 * add a new mask into the mask list.
1947 * The caller needs to make sure that 'mask' is not the same
1948 * as any masks that are already on the list.
1950 void ovs_sw_flow_mask_insert(struct flow_table *tbl, struct sw_flow_mask *mask)
1952 list_add_rcu(&mask->list, tbl->mask_list);
1956 * Set 'range' fields in the mask to the value of 'val'.
1958 static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask,
1959 struct sw_flow_key_range *range, u8 val)
1961 u8 *m = (u8 *)&mask->key + range->start;
1963 mask->range = *range;
1964 memset(m, val, ovs_sw_flow_mask_size_roundup(mask));