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 = rounddown(offset, sizeof(long));
59 size_t end = roundup(offset + size, sizeof(long));
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 static u16 range_n_bytes(const struct sw_flow_key_range *range)
108 return range->end - range->start;
111 void ovs_match_init(struct sw_flow_match *match,
112 struct sw_flow_key *key,
113 struct sw_flow_mask *mask)
115 memset(match, 0, sizeof(*match));
119 memset(key, 0, sizeof(*key));
122 memset(&mask->key, 0, sizeof(mask->key));
123 mask->range.start = mask->range.end = 0;
127 static bool ovs_match_validate(const struct sw_flow_match *match,
128 u64 key_attrs, u64 mask_attrs)
130 u64 key_expected = 1ULL << OVS_KEY_ATTR_ETHERNET;
131 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
133 /* The following mask attributes allowed only if they
134 * pass the validation tests. */
135 mask_allowed &= ~((1ULL << OVS_KEY_ATTR_IPV4)
136 | (1ULL << OVS_KEY_ATTR_IPV6)
137 | (1ULL << OVS_KEY_ATTR_TCP)
138 | (1ULL << OVS_KEY_ATTR_UDP)
139 | (1ULL << OVS_KEY_ATTR_SCTP)
140 | (1ULL << OVS_KEY_ATTR_ICMP)
141 | (1ULL << OVS_KEY_ATTR_ICMPV6)
142 | (1ULL << OVS_KEY_ATTR_ARP)
143 | (1ULL << OVS_KEY_ATTR_ND));
145 /* Always allowed mask fields. */
146 mask_allowed |= ((1ULL << OVS_KEY_ATTR_TUNNEL)
147 | (1ULL << OVS_KEY_ATTR_IN_PORT)
148 | (1ULL << OVS_KEY_ATTR_ETHERTYPE));
150 /* Check key attributes. */
151 if (match->key->eth.type == htons(ETH_P_ARP)
152 || match->key->eth.type == htons(ETH_P_RARP)) {
153 key_expected |= 1ULL << OVS_KEY_ATTR_ARP;
154 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
155 mask_allowed |= 1ULL << OVS_KEY_ATTR_ARP;
158 if (match->key->eth.type == htons(ETH_P_IP)) {
159 key_expected |= 1ULL << OVS_KEY_ATTR_IPV4;
160 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
161 mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV4;
163 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
164 if (match->key->ip.proto == IPPROTO_UDP) {
165 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
166 if (match->mask && (match->mask->key.ip.proto == 0xff))
167 mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
170 if (match->key->ip.proto == IPPROTO_SCTP) {
171 key_expected |= 1ULL << OVS_KEY_ATTR_SCTP;
172 if (match->mask && (match->mask->key.ip.proto == 0xff))
173 mask_allowed |= 1ULL << OVS_KEY_ATTR_SCTP;
176 if (match->key->ip.proto == IPPROTO_TCP) {
177 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
178 if (match->mask && (match->mask->key.ip.proto == 0xff))
179 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
182 if (match->key->ip.proto == IPPROTO_ICMP) {
183 key_expected |= 1ULL << OVS_KEY_ATTR_ICMP;
184 if (match->mask && (match->mask->key.ip.proto == 0xff))
185 mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMP;
190 if (match->key->eth.type == htons(ETH_P_IPV6)) {
191 key_expected |= 1ULL << OVS_KEY_ATTR_IPV6;
192 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
193 mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV6;
195 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
196 if (match->key->ip.proto == IPPROTO_UDP) {
197 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
198 if (match->mask && (match->mask->key.ip.proto == 0xff))
199 mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
202 if (match->key->ip.proto == IPPROTO_SCTP) {
203 key_expected |= 1ULL << OVS_KEY_ATTR_SCTP;
204 if (match->mask && (match->mask->key.ip.proto == 0xff))
205 mask_allowed |= 1ULL << OVS_KEY_ATTR_SCTP;
208 if (match->key->ip.proto == IPPROTO_TCP) {
209 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
210 if (match->mask && (match->mask->key.ip.proto == 0xff))
211 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
214 if (match->key->ip.proto == IPPROTO_ICMPV6) {
215 key_expected |= 1ULL << OVS_KEY_ATTR_ICMPV6;
216 if (match->mask && (match->mask->key.ip.proto == 0xff))
217 mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMPV6;
219 if (match->key->ipv6.tp.src ==
220 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
221 match->key->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
222 key_expected |= 1ULL << OVS_KEY_ATTR_ND;
223 if (match->mask && (match->mask->key.ipv6.tp.src == htons(0xffff)))
224 mask_allowed |= 1ULL << OVS_KEY_ATTR_ND;
230 if ((key_attrs & key_expected) != key_expected) {
231 /* Key attributes check failed. */
232 OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n",
233 key_attrs, key_expected);
237 if ((mask_attrs & mask_allowed) != mask_attrs) {
238 /* Mask attributes check failed. */
239 OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n",
240 mask_attrs, mask_allowed);
247 static int check_header(struct sk_buff *skb, int len)
249 if (unlikely(skb->len < len))
251 if (unlikely(!pskb_may_pull(skb, len)))
256 static bool arphdr_ok(struct sk_buff *skb)
258 return pskb_may_pull(skb, skb_network_offset(skb) +
259 sizeof(struct arp_eth_header));
262 static int check_iphdr(struct sk_buff *skb)
264 unsigned int nh_ofs = skb_network_offset(skb);
268 err = check_header(skb, nh_ofs + sizeof(struct iphdr));
272 ip_len = ip_hdrlen(skb);
273 if (unlikely(ip_len < sizeof(struct iphdr) ||
274 skb->len < nh_ofs + ip_len))
277 skb_set_transport_header(skb, nh_ofs + ip_len);
281 static bool tcphdr_ok(struct sk_buff *skb)
283 int th_ofs = skb_transport_offset(skb);
286 if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
289 tcp_len = tcp_hdrlen(skb);
290 if (unlikely(tcp_len < sizeof(struct tcphdr) ||
291 skb->len < th_ofs + tcp_len))
297 static bool udphdr_ok(struct sk_buff *skb)
299 return pskb_may_pull(skb, skb_transport_offset(skb) +
300 sizeof(struct udphdr));
303 static bool sctphdr_ok(struct sk_buff *skb)
305 return pskb_may_pull(skb, skb_transport_offset(skb) +
306 sizeof(struct sctphdr));
309 static bool icmphdr_ok(struct sk_buff *skb)
311 return pskb_may_pull(skb, skb_transport_offset(skb) +
312 sizeof(struct icmphdr));
315 u64 ovs_flow_used_time(unsigned long flow_jiffies)
317 struct timespec cur_ts;
320 ktime_get_ts(&cur_ts);
321 idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
322 cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
323 cur_ts.tv_nsec / NSEC_PER_MSEC;
325 return cur_ms - idle_ms;
328 static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
330 unsigned int nh_ofs = skb_network_offset(skb);
338 err = check_header(skb, nh_ofs + sizeof(*nh));
343 nexthdr = nh->nexthdr;
344 payload_ofs = (u8 *)(nh + 1) - skb->data;
346 key->ip.proto = NEXTHDR_NONE;
347 key->ip.tos = ipv6_get_dsfield(nh);
348 key->ip.ttl = nh->hop_limit;
349 key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
350 key->ipv6.addr.src = nh->saddr;
351 key->ipv6.addr.dst = nh->daddr;
353 payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
354 if (unlikely(payload_ofs < 0))
358 if (frag_off & htons(~0x7))
359 key->ip.frag = OVS_FRAG_TYPE_LATER;
361 key->ip.frag = OVS_FRAG_TYPE_FIRST;
364 nh_len = payload_ofs - nh_ofs;
365 skb_set_transport_header(skb, nh_ofs + nh_len);
366 key->ip.proto = nexthdr;
370 static bool icmp6hdr_ok(struct sk_buff *skb)
372 return pskb_may_pull(skb, skb_transport_offset(skb) +
373 sizeof(struct icmp6hdr));
376 void ovs_flow_key_mask(struct sw_flow_key *dst, const struct sw_flow_key *src,
377 const struct sw_flow_mask *mask)
379 const long *m = (long *)((u8 *)&mask->key + mask->range.start);
380 const long *s = (long *)((u8 *)src + mask->range.start);
381 long *d = (long *)((u8 *)dst + mask->range.start);
384 /* The memory outside of the 'mask->range' are not set since
385 * further operations on 'dst' only uses contents within
388 for (i = 0; i < range_n_bytes(&mask->range); i += sizeof(long))
392 #define TCP_FLAGS_OFFSET 13
393 #define TCP_FLAG_MASK 0x3f
395 void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb)
399 if ((flow->key.eth.type == htons(ETH_P_IP) ||
400 flow->key.eth.type == htons(ETH_P_IPV6)) &&
401 flow->key.ip.proto == IPPROTO_TCP &&
402 likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) {
403 u8 *tcp = (u8 *)tcp_hdr(skb);
404 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
407 spin_lock(&flow->lock);
408 flow->used = jiffies;
409 flow->packet_count++;
410 flow->byte_count += skb->len;
411 flow->tcp_flags |= tcp_flags;
412 spin_unlock(&flow->lock);
415 struct sw_flow_actions *ovs_flow_actions_alloc(int size)
417 struct sw_flow_actions *sfa;
419 if (size > MAX_ACTIONS_BUFSIZE)
420 return ERR_PTR(-EINVAL);
422 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
424 return ERR_PTR(-ENOMEM);
426 sfa->actions_len = 0;
430 struct sw_flow *ovs_flow_alloc(void)
432 struct sw_flow *flow;
434 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
436 return ERR_PTR(-ENOMEM);
438 spin_lock_init(&flow->lock);
439 flow->sf_acts = NULL;
445 static struct hlist_head *find_bucket(struct flow_table *table, u32 hash)
447 hash = jhash_1word(hash, table->hash_seed);
448 return flex_array_get(table->buckets,
449 (hash & (table->n_buckets - 1)));
452 static struct flex_array *alloc_buckets(unsigned int n_buckets)
454 struct flex_array *buckets;
457 buckets = flex_array_alloc(sizeof(struct hlist_head),
458 n_buckets, GFP_KERNEL);
462 err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
464 flex_array_free(buckets);
468 for (i = 0; i < n_buckets; i++)
469 INIT_HLIST_HEAD((struct hlist_head *)
470 flex_array_get(buckets, i));
475 static void free_buckets(struct flex_array *buckets)
477 flex_array_free(buckets);
480 static struct flow_table *__flow_tbl_alloc(int new_size)
482 struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL);
487 table->buckets = alloc_buckets(new_size);
489 if (!table->buckets) {
493 table->n_buckets = new_size;
496 table->keep_flows = false;
497 get_random_bytes(&table->hash_seed, sizeof(u32));
498 table->mask_list = NULL;
503 static void __flow_tbl_destroy(struct flow_table *table)
507 if (table->keep_flows)
510 for (i = 0; i < table->n_buckets; i++) {
511 struct sw_flow *flow;
512 struct hlist_head *head = flex_array_get(table->buckets, i);
513 struct hlist_node *n;
514 int ver = table->node_ver;
516 hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
517 hlist_del(&flow->hash_node[ver]);
518 ovs_flow_free(flow, false);
522 BUG_ON(!list_empty(table->mask_list));
523 kfree(table->mask_list);
526 free_buckets(table->buckets);
530 struct flow_table *ovs_flow_tbl_alloc(int new_size)
532 struct flow_table *table = __flow_tbl_alloc(new_size);
537 table->mask_list = kmalloc(sizeof(struct list_head), GFP_KERNEL);
538 if (!table->mask_list) {
539 table->keep_flows = true;
540 __flow_tbl_destroy(table);
543 INIT_LIST_HEAD(table->mask_list);
548 static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
550 struct flow_table *table = container_of(rcu, struct flow_table, rcu);
552 __flow_tbl_destroy(table);
555 void ovs_flow_tbl_destroy(struct flow_table *table, bool deferred)
561 call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb);
563 __flow_tbl_destroy(table);
566 struct sw_flow *ovs_flow_dump_next(struct flow_table *table, u32 *bucket, u32 *last)
568 struct sw_flow *flow;
569 struct hlist_head *head;
573 ver = table->node_ver;
574 while (*bucket < table->n_buckets) {
576 head = flex_array_get(table->buckets, *bucket);
577 hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
592 static void __tbl_insert(struct flow_table *table, struct sw_flow *flow)
594 struct hlist_head *head;
596 head = find_bucket(table, flow->hash);
597 hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
602 static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new)
607 old_ver = old->node_ver;
608 new->node_ver = !old_ver;
610 /* Insert in new table. */
611 for (i = 0; i < old->n_buckets; i++) {
612 struct sw_flow *flow;
613 struct hlist_head *head;
615 head = flex_array_get(old->buckets, i);
617 hlist_for_each_entry(flow, head, hash_node[old_ver])
618 __tbl_insert(new, flow);
621 new->mask_list = old->mask_list;
622 old->keep_flows = true;
625 static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets)
627 struct flow_table *new_table;
629 new_table = __flow_tbl_alloc(n_buckets);
631 return ERR_PTR(-ENOMEM);
633 flow_table_copy_flows(table, new_table);
638 struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table)
640 return __flow_tbl_rehash(table, table->n_buckets);
643 struct flow_table *ovs_flow_tbl_expand(struct flow_table *table)
645 return __flow_tbl_rehash(table, table->n_buckets * 2);
648 static void __flow_free(struct sw_flow *flow)
650 kfree((struct sf_flow_acts __force *)flow->sf_acts);
651 kmem_cache_free(flow_cache, flow);
654 static void rcu_free_flow_callback(struct rcu_head *rcu)
656 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
661 void ovs_flow_free(struct sw_flow *flow, bool deferred)
666 ovs_sw_flow_mask_del_ref(flow->mask, deferred);
669 call_rcu(&flow->rcu, rcu_free_flow_callback);
674 /* RCU callback used by ovs_flow_deferred_free_acts. */
675 static void rcu_free_acts_callback(struct rcu_head *rcu)
677 struct sw_flow_actions *sf_acts = container_of(rcu,
678 struct sw_flow_actions, rcu);
682 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
683 * The caller must hold rcu_read_lock for this to be sensible. */
684 void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
686 call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
689 static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
692 __be16 eth_type; /* ETH_P_8021Q */
695 struct qtag_prefix *qp;
697 if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
700 if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
704 qp = (struct qtag_prefix *) skb->data;
705 key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
706 __skb_pull(skb, sizeof(struct qtag_prefix));
711 static __be16 parse_ethertype(struct sk_buff *skb)
713 struct llc_snap_hdr {
714 u8 dsap; /* Always 0xAA */
715 u8 ssap; /* Always 0xAA */
720 struct llc_snap_hdr *llc;
723 proto = *(__be16 *) skb->data;
724 __skb_pull(skb, sizeof(__be16));
726 if (ntohs(proto) >= ETH_P_802_3_MIN)
729 if (skb->len < sizeof(struct llc_snap_hdr))
730 return htons(ETH_P_802_2);
732 if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
735 llc = (struct llc_snap_hdr *) skb->data;
736 if (llc->dsap != LLC_SAP_SNAP ||
737 llc->ssap != LLC_SAP_SNAP ||
738 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
739 return htons(ETH_P_802_2);
741 __skb_pull(skb, sizeof(struct llc_snap_hdr));
743 if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
744 return llc->ethertype;
746 return htons(ETH_P_802_2);
749 static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
752 struct icmp6hdr *icmp = icmp6_hdr(skb);
754 /* The ICMPv6 type and code fields use the 16-bit transport port
755 * fields, so we need to store them in 16-bit network byte order.
757 key->ipv6.tp.src = htons(icmp->icmp6_type);
758 key->ipv6.tp.dst = htons(icmp->icmp6_code);
760 if (icmp->icmp6_code == 0 &&
761 (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
762 icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
763 int icmp_len = skb->len - skb_transport_offset(skb);
767 /* In order to process neighbor discovery options, we need the
770 if (unlikely(icmp_len < sizeof(*nd)))
773 if (unlikely(skb_linearize(skb)))
776 nd = (struct nd_msg *)skb_transport_header(skb);
777 key->ipv6.nd.target = nd->target;
779 icmp_len -= sizeof(*nd);
781 while (icmp_len >= 8) {
782 struct nd_opt_hdr *nd_opt =
783 (struct nd_opt_hdr *)(nd->opt + offset);
784 int opt_len = nd_opt->nd_opt_len * 8;
786 if (unlikely(!opt_len || opt_len > icmp_len))
789 /* Store the link layer address if the appropriate
790 * option is provided. It is considered an error if
791 * the same link layer option is specified twice.
793 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
795 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
797 memcpy(key->ipv6.nd.sll,
798 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
799 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
801 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
803 memcpy(key->ipv6.nd.tll,
804 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
815 memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
816 memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
817 memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
823 * ovs_flow_extract - extracts a flow key from an Ethernet frame.
824 * @skb: sk_buff that contains the frame, with skb->data pointing to the
826 * @in_port: port number on which @skb was received.
827 * @key: output flow key
829 * The caller must ensure that skb->len >= ETH_HLEN.
831 * Returns 0 if successful, otherwise a negative errno value.
833 * Initializes @skb header pointers as follows:
835 * - skb->mac_header: the Ethernet header.
837 * - skb->network_header: just past the Ethernet header, or just past the
838 * VLAN header, to the first byte of the Ethernet payload.
840 * - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
841 * on output, then just past the IP header, if one is present and
842 * of a correct length, otherwise the same as skb->network_header.
843 * For other key->eth.type values it is left untouched.
845 int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
850 memset(key, 0, sizeof(*key));
852 key->phy.priority = skb->priority;
853 if (OVS_CB(skb)->tun_key)
854 memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key));
855 key->phy.in_port = in_port;
856 key->phy.skb_mark = skb->mark;
858 skb_reset_mac_header(skb);
860 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
861 * header in the linear data area.
864 memcpy(key->eth.src, eth->h_source, ETH_ALEN);
865 memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
867 __skb_pull(skb, 2 * ETH_ALEN);
868 /* We are going to push all headers that we pull, so no need to
869 * update skb->csum here. */
871 if (vlan_tx_tag_present(skb))
872 key->eth.tci = htons(vlan_get_tci(skb));
873 else if (eth->h_proto == htons(ETH_P_8021Q))
874 if (unlikely(parse_vlan(skb, key)))
877 key->eth.type = parse_ethertype(skb);
878 if (unlikely(key->eth.type == htons(0)))
881 skb_reset_network_header(skb);
882 __skb_push(skb, skb->data - skb_mac_header(skb));
885 if (key->eth.type == htons(ETH_P_IP)) {
889 error = check_iphdr(skb);
890 if (unlikely(error)) {
891 if (error == -EINVAL) {
892 skb->transport_header = skb->network_header;
899 key->ipv4.addr.src = nh->saddr;
900 key->ipv4.addr.dst = nh->daddr;
902 key->ip.proto = nh->protocol;
903 key->ip.tos = nh->tos;
904 key->ip.ttl = nh->ttl;
906 offset = nh->frag_off & htons(IP_OFFSET);
908 key->ip.frag = OVS_FRAG_TYPE_LATER;
911 if (nh->frag_off & htons(IP_MF) ||
912 skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
913 key->ip.frag = OVS_FRAG_TYPE_FIRST;
915 /* Transport layer. */
916 if (key->ip.proto == IPPROTO_TCP) {
917 if (tcphdr_ok(skb)) {
918 struct tcphdr *tcp = tcp_hdr(skb);
919 key->ipv4.tp.src = tcp->source;
920 key->ipv4.tp.dst = tcp->dest;
922 } else if (key->ip.proto == IPPROTO_UDP) {
923 if (udphdr_ok(skb)) {
924 struct udphdr *udp = udp_hdr(skb);
925 key->ipv4.tp.src = udp->source;
926 key->ipv4.tp.dst = udp->dest;
928 } else if (key->ip.proto == IPPROTO_SCTP) {
929 if (sctphdr_ok(skb)) {
930 struct sctphdr *sctp = sctp_hdr(skb);
931 key->ipv4.tp.src = sctp->source;
932 key->ipv4.tp.dst = sctp->dest;
934 } else if (key->ip.proto == IPPROTO_ICMP) {
935 if (icmphdr_ok(skb)) {
936 struct icmphdr *icmp = icmp_hdr(skb);
937 /* The ICMP type and code fields use the 16-bit
938 * transport port fields, so we need to store
939 * them in 16-bit network byte order. */
940 key->ipv4.tp.src = htons(icmp->type);
941 key->ipv4.tp.dst = htons(icmp->code);
945 } else if ((key->eth.type == htons(ETH_P_ARP) ||
946 key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) {
947 struct arp_eth_header *arp;
949 arp = (struct arp_eth_header *)skb_network_header(skb);
951 if (arp->ar_hrd == htons(ARPHRD_ETHER)
952 && arp->ar_pro == htons(ETH_P_IP)
953 && arp->ar_hln == ETH_ALEN
954 && arp->ar_pln == 4) {
956 /* We only match on the lower 8 bits of the opcode. */
957 if (ntohs(arp->ar_op) <= 0xff)
958 key->ip.proto = ntohs(arp->ar_op);
959 memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
960 memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
961 memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
962 memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
964 } else if (key->eth.type == htons(ETH_P_IPV6)) {
965 int nh_len; /* IPv6 Header + Extensions */
967 nh_len = parse_ipv6hdr(skb, key);
968 if (unlikely(nh_len < 0)) {
969 if (nh_len == -EINVAL) {
970 skb->transport_header = skb->network_header;
978 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
980 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
981 key->ip.frag = OVS_FRAG_TYPE_FIRST;
983 /* Transport layer. */
984 if (key->ip.proto == NEXTHDR_TCP) {
985 if (tcphdr_ok(skb)) {
986 struct tcphdr *tcp = tcp_hdr(skb);
987 key->ipv6.tp.src = tcp->source;
988 key->ipv6.tp.dst = tcp->dest;
990 } else if (key->ip.proto == NEXTHDR_UDP) {
991 if (udphdr_ok(skb)) {
992 struct udphdr *udp = udp_hdr(skb);
993 key->ipv6.tp.src = udp->source;
994 key->ipv6.tp.dst = udp->dest;
996 } else if (key->ip.proto == NEXTHDR_SCTP) {
997 if (sctphdr_ok(skb)) {
998 struct sctphdr *sctp = sctp_hdr(skb);
999 key->ipv6.tp.src = sctp->source;
1000 key->ipv6.tp.dst = sctp->dest;
1002 } else if (key->ip.proto == NEXTHDR_ICMP) {
1003 if (icmp6hdr_ok(skb)) {
1004 error = parse_icmpv6(skb, key, nh_len);
1014 static u32 ovs_flow_hash(const struct sw_flow_key *key, int key_start,
1017 u32 *hash_key = (u32 *)((u8 *)key + key_start);
1018 int hash_u32s = (key_end - key_start) >> 2;
1020 /* Make sure number of hash bytes are multiple of u32. */
1021 BUILD_BUG_ON(sizeof(long) % sizeof(u32));
1023 return jhash2(hash_key, hash_u32s, 0);
1026 static int flow_key_start(const struct sw_flow_key *key)
1028 if (key->tun_key.ipv4_dst)
1031 return rounddown(offsetof(struct sw_flow_key, phy),
1035 static bool __cmp_key(const struct sw_flow_key *key1,
1036 const struct sw_flow_key *key2, int key_start, int key_end)
1038 const long *cp1 = (long *)((u8 *)key1 + key_start);
1039 const long *cp2 = (long *)((u8 *)key2 + key_start);
1043 for (i = key_start; i < key_end; i += sizeof(long))
1044 diffs |= *cp1++ ^ *cp2++;
1049 static bool __flow_cmp_masked_key(const struct sw_flow *flow,
1050 const struct sw_flow_key *key, int key_start, int key_end)
1052 return __cmp_key(&flow->key, key, key_start, key_end);
1055 static bool __flow_cmp_unmasked_key(const struct sw_flow *flow,
1056 const struct sw_flow_key *key, int key_start, int key_end)
1058 return __cmp_key(&flow->unmasked_key, key, key_start, key_end);
1061 bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
1062 const struct sw_flow_key *key, int key_end)
1065 key_start = flow_key_start(key);
1067 return __flow_cmp_unmasked_key(flow, key, key_start, key_end);
1071 struct sw_flow *ovs_flow_lookup_unmasked_key(struct flow_table *table,
1072 struct sw_flow_match *match)
1074 struct sw_flow_key *unmasked = match->key;
1075 int key_end = match->range.end;
1076 struct sw_flow *flow;
1078 flow = ovs_flow_lookup(table, unmasked);
1079 if (flow && (!ovs_flow_cmp_unmasked_key(flow, unmasked, key_end)))
1085 static struct sw_flow *ovs_masked_flow_lookup(struct flow_table *table,
1086 const struct sw_flow_key *unmasked,
1087 struct sw_flow_mask *mask)
1089 struct sw_flow *flow;
1090 struct hlist_head *head;
1091 int key_start = mask->range.start;
1092 int key_end = mask->range.end;
1094 struct sw_flow_key masked_key;
1096 ovs_flow_key_mask(&masked_key, unmasked, mask);
1097 hash = ovs_flow_hash(&masked_key, key_start, key_end);
1098 head = find_bucket(table, hash);
1099 hlist_for_each_entry_rcu(flow, head, hash_node[table->node_ver]) {
1100 if (flow->mask == mask &&
1101 __flow_cmp_masked_key(flow, &masked_key,
1102 key_start, key_end))
1108 struct sw_flow *ovs_flow_lookup(struct flow_table *tbl,
1109 const struct sw_flow_key *key)
1111 struct sw_flow *flow = NULL;
1112 struct sw_flow_mask *mask;
1114 list_for_each_entry_rcu(mask, tbl->mask_list, list) {
1115 flow = ovs_masked_flow_lookup(tbl, key, mask);
1116 if (flow) /* Found */
1124 void ovs_flow_insert(struct flow_table *table, struct sw_flow *flow)
1126 flow->hash = ovs_flow_hash(&flow->key, flow->mask->range.start,
1127 flow->mask->range.end);
1128 __tbl_insert(table, flow);
1131 void ovs_flow_remove(struct flow_table *table, struct sw_flow *flow)
1133 BUG_ON(table->count == 0);
1134 hlist_del_rcu(&flow->hash_node[table->node_ver]);
1138 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
1139 const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
1140 [OVS_KEY_ATTR_ENCAP] = -1,
1141 [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
1142 [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
1143 [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
1144 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
1145 [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
1146 [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
1147 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
1148 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
1149 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
1150 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
1151 [OVS_KEY_ATTR_SCTP] = sizeof(struct ovs_key_sctp),
1152 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
1153 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
1154 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
1155 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
1156 [OVS_KEY_ATTR_TUNNEL] = -1,
1159 static bool is_all_zero(const u8 *fp, size_t size)
1166 for (i = 0; i < size; i++)
1173 static int __parse_flow_nlattrs(const struct nlattr *attr,
1174 const struct nlattr *a[],
1175 u64 *attrsp, bool nz)
1177 const struct nlattr *nla;
1182 nla_for_each_nested(nla, attr, rem) {
1183 u16 type = nla_type(nla);
1186 if (type > OVS_KEY_ATTR_MAX) {
1187 OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
1188 type, OVS_KEY_ATTR_MAX);
1191 if (attrs & (1ULL << type)) {
1192 OVS_NLERR("Duplicate key attribute (type %d).\n", type);
1196 expected_len = ovs_key_lens[type];
1197 if (nla_len(nla) != expected_len && expected_len != -1) {
1198 OVS_NLERR("Key attribute has unexpected length (type=%d"
1199 ", length=%d, expected=%d).\n", type,
1200 nla_len(nla), expected_len);
1204 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
1205 attrs |= 1ULL << type;
1210 OVS_NLERR("Message has %d unknown bytes.\n", rem);
1218 static int parse_flow_mask_nlattrs(const struct nlattr *attr,
1219 const struct nlattr *a[], u64 *attrsp)
1221 return __parse_flow_nlattrs(attr, a, attrsp, true);
1224 static int parse_flow_nlattrs(const struct nlattr *attr,
1225 const struct nlattr *a[], u64 *attrsp)
1227 return __parse_flow_nlattrs(attr, a, attrsp, false);
1230 int ovs_ipv4_tun_from_nlattr(const struct nlattr *attr,
1231 struct sw_flow_match *match, bool is_mask)
1236 __be16 tun_flags = 0;
1238 nla_for_each_nested(a, attr, rem) {
1239 int type = nla_type(a);
1240 static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
1241 [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
1242 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
1243 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
1244 [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
1245 [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
1246 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
1247 [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
1250 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
1251 OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n",
1252 type, OVS_TUNNEL_KEY_ATTR_MAX);
1256 if (ovs_tunnel_key_lens[type] != nla_len(a)) {
1257 OVS_NLERR("IPv4 tunnel attribute type has unexpected "
1258 " length (type=%d, length=%d, expected=%d).\n",
1259 type, nla_len(a), ovs_tunnel_key_lens[type]);
1264 case OVS_TUNNEL_KEY_ATTR_ID:
1265 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
1266 nla_get_be64(a), is_mask);
1267 tun_flags |= TUNNEL_KEY;
1269 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1270 SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
1271 nla_get_be32(a), is_mask);
1273 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1274 SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
1275 nla_get_be32(a), is_mask);
1277 case OVS_TUNNEL_KEY_ATTR_TOS:
1278 SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
1279 nla_get_u8(a), is_mask);
1281 case OVS_TUNNEL_KEY_ATTR_TTL:
1282 SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
1283 nla_get_u8(a), is_mask);
1286 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1287 tun_flags |= TUNNEL_DONT_FRAGMENT;
1289 case OVS_TUNNEL_KEY_ATTR_CSUM:
1290 tun_flags |= TUNNEL_CSUM;
1297 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
1300 OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
1305 if (!match->key->tun_key.ipv4_dst) {
1306 OVS_NLERR("IPv4 tunnel destination address is zero.\n");
1311 OVS_NLERR("IPv4 tunnel TTL not specified.\n");
1319 int ovs_ipv4_tun_to_nlattr(struct sk_buff *skb,
1320 const struct ovs_key_ipv4_tunnel *tun_key,
1321 const struct ovs_key_ipv4_tunnel *output)
1325 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
1329 if (output->tun_flags & TUNNEL_KEY &&
1330 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
1332 if (output->ipv4_src &&
1333 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
1335 if (output->ipv4_dst &&
1336 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
1338 if (output->ipv4_tos &&
1339 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
1341 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
1343 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
1344 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
1346 if ((output->tun_flags & TUNNEL_CSUM) &&
1347 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
1350 nla_nest_end(skb, nla);
1355 static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
1356 const struct nlattr **a, bool is_mask)
1358 if (*attrs & (1ULL << OVS_KEY_ATTR_PRIORITY)) {
1359 SW_FLOW_KEY_PUT(match, phy.priority,
1360 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
1361 *attrs &= ~(1ULL << OVS_KEY_ATTR_PRIORITY);
1364 if (*attrs & (1ULL << OVS_KEY_ATTR_IN_PORT)) {
1365 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1368 in_port = 0xffffffff; /* Always exact match in_port. */
1369 else if (in_port >= DP_MAX_PORTS)
1372 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
1373 *attrs &= ~(1ULL << OVS_KEY_ATTR_IN_PORT);
1374 } else if (!is_mask) {
1375 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
1378 if (*attrs & (1ULL << OVS_KEY_ATTR_SKB_MARK)) {
1379 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
1381 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
1382 *attrs &= ~(1ULL << OVS_KEY_ATTR_SKB_MARK);
1384 if (*attrs & (1ULL << OVS_KEY_ATTR_TUNNEL)) {
1385 if (ovs_ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
1388 *attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL);
1393 static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
1394 const struct nlattr **a, bool is_mask)
1397 u64 orig_attrs = attrs;
1399 err = metadata_from_nlattrs(match, &attrs, a, is_mask);
1403 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) {
1404 const struct ovs_key_ethernet *eth_key;
1406 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1407 SW_FLOW_KEY_MEMCPY(match, eth.src,
1408 eth_key->eth_src, ETH_ALEN, is_mask);
1409 SW_FLOW_KEY_MEMCPY(match, eth.dst,
1410 eth_key->eth_dst, ETH_ALEN, is_mask);
1411 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERNET);
1414 if (attrs & (1ULL << OVS_KEY_ATTR_VLAN)) {
1417 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1418 if (!(tci & htons(VLAN_TAG_PRESENT))) {
1420 OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
1422 OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
1427 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
1428 attrs &= ~(1ULL << OVS_KEY_ATTR_VLAN);
1429 } else if (!is_mask)
1430 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
1432 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) {
1435 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1437 /* Always exact match EtherType. */
1438 eth_type = htons(0xffff);
1439 } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
1440 OVS_NLERR("EtherType is less than minimum (type=%x, min=%x).\n",
1441 ntohs(eth_type), ETH_P_802_3_MIN);
1445 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
1446 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1447 } else if (!is_mask) {
1448 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
1451 if (attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1452 const struct ovs_key_ipv4 *ipv4_key;
1454 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1455 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
1456 OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
1457 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
1460 SW_FLOW_KEY_PUT(match, ip.proto,
1461 ipv4_key->ipv4_proto, is_mask);
1462 SW_FLOW_KEY_PUT(match, ip.tos,
1463 ipv4_key->ipv4_tos, is_mask);
1464 SW_FLOW_KEY_PUT(match, ip.ttl,
1465 ipv4_key->ipv4_ttl, is_mask);
1466 SW_FLOW_KEY_PUT(match, ip.frag,
1467 ipv4_key->ipv4_frag, is_mask);
1468 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1469 ipv4_key->ipv4_src, is_mask);
1470 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1471 ipv4_key->ipv4_dst, is_mask);
1472 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV4);
1475 if (attrs & (1ULL << OVS_KEY_ATTR_IPV6)) {
1476 const struct ovs_key_ipv6 *ipv6_key;
1478 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1479 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
1480 OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
1481 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
1484 SW_FLOW_KEY_PUT(match, ipv6.label,
1485 ipv6_key->ipv6_label, is_mask);
1486 SW_FLOW_KEY_PUT(match, ip.proto,
1487 ipv6_key->ipv6_proto, is_mask);
1488 SW_FLOW_KEY_PUT(match, ip.tos,
1489 ipv6_key->ipv6_tclass, is_mask);
1490 SW_FLOW_KEY_PUT(match, ip.ttl,
1491 ipv6_key->ipv6_hlimit, is_mask);
1492 SW_FLOW_KEY_PUT(match, ip.frag,
1493 ipv6_key->ipv6_frag, is_mask);
1494 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
1496 sizeof(match->key->ipv6.addr.src),
1498 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
1500 sizeof(match->key->ipv6.addr.dst),
1503 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV6);
1506 if (attrs & (1ULL << OVS_KEY_ATTR_ARP)) {
1507 const struct ovs_key_arp *arp_key;
1509 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1510 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
1511 OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
1516 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1517 arp_key->arp_sip, is_mask);
1518 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1519 arp_key->arp_tip, is_mask);
1520 SW_FLOW_KEY_PUT(match, ip.proto,
1521 ntohs(arp_key->arp_op), is_mask);
1522 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
1523 arp_key->arp_sha, ETH_ALEN, is_mask);
1524 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
1525 arp_key->arp_tha, ETH_ALEN, is_mask);
1527 attrs &= ~(1ULL << OVS_KEY_ATTR_ARP);
1530 if (attrs & (1ULL << OVS_KEY_ATTR_TCP)) {
1531 const struct ovs_key_tcp *tcp_key;
1533 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
1534 if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1535 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1536 tcp_key->tcp_src, is_mask);
1537 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1538 tcp_key->tcp_dst, is_mask);
1540 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1541 tcp_key->tcp_src, is_mask);
1542 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1543 tcp_key->tcp_dst, is_mask);
1545 attrs &= ~(1ULL << OVS_KEY_ATTR_TCP);
1548 if (attrs & (1ULL << OVS_KEY_ATTR_UDP)) {
1549 const struct ovs_key_udp *udp_key;
1551 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
1552 if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1553 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1554 udp_key->udp_src, is_mask);
1555 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1556 udp_key->udp_dst, is_mask);
1558 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1559 udp_key->udp_src, is_mask);
1560 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1561 udp_key->udp_dst, is_mask);
1563 attrs &= ~(1ULL << OVS_KEY_ATTR_UDP);
1566 if (attrs & (1ULL << OVS_KEY_ATTR_SCTP)) {
1567 const struct ovs_key_sctp *sctp_key;
1569 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
1570 if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1571 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1572 sctp_key->sctp_src, is_mask);
1573 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1574 sctp_key->sctp_dst, is_mask);
1576 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1577 sctp_key->sctp_src, is_mask);
1578 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1579 sctp_key->sctp_dst, is_mask);
1581 attrs &= ~(1ULL << OVS_KEY_ATTR_SCTP);
1584 if (attrs & (1ULL << OVS_KEY_ATTR_ICMP)) {
1585 const struct ovs_key_icmp *icmp_key;
1587 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
1588 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1589 htons(icmp_key->icmp_type), is_mask);
1590 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1591 htons(icmp_key->icmp_code), is_mask);
1592 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMP);
1595 if (attrs & (1ULL << OVS_KEY_ATTR_ICMPV6)) {
1596 const struct ovs_key_icmpv6 *icmpv6_key;
1598 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
1599 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1600 htons(icmpv6_key->icmpv6_type), is_mask);
1601 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1602 htons(icmpv6_key->icmpv6_code), is_mask);
1603 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMPV6);
1606 if (attrs & (1ULL << OVS_KEY_ATTR_ND)) {
1607 const struct ovs_key_nd *nd_key;
1609 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1610 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1612 sizeof(match->key->ipv6.nd.target),
1614 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1615 nd_key->nd_sll, ETH_ALEN, is_mask);
1616 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1617 nd_key->nd_tll, ETH_ALEN, is_mask);
1618 attrs &= ~(1ULL << OVS_KEY_ATTR_ND);
1628 * ovs_match_from_nlattrs - parses Netlink attributes into a flow key and
1629 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1630 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1631 * does not include any don't care bit.
1632 * @match: receives the extracted flow match information.
1633 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1634 * sequence. The fields should of the packet that triggered the creation
1636 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1637 * attribute specifies the mask field of the wildcarded flow.
1639 int ovs_match_from_nlattrs(struct sw_flow_match *match,
1640 const struct nlattr *key,
1641 const struct nlattr *mask)
1643 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1644 const struct nlattr *encap;
1647 bool encap_valid = false;
1650 err = parse_flow_nlattrs(key, a, &key_attrs);
1654 if ((key_attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) &&
1655 (key_attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) &&
1656 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
1659 if (!((key_attrs & (1ULL << OVS_KEY_ATTR_VLAN)) &&
1660 (key_attrs & (1ULL << OVS_KEY_ATTR_ENCAP)))) {
1661 OVS_NLERR("Invalid Vlan frame.\n");
1665 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1666 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1667 encap = a[OVS_KEY_ATTR_ENCAP];
1668 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
1671 if (tci & htons(VLAN_TAG_PRESENT)) {
1672 err = parse_flow_nlattrs(encap, a, &key_attrs);
1676 /* Corner case for truncated 802.1Q header. */
1677 if (nla_len(encap)) {
1678 OVS_NLERR("Truncated 802.1Q header has non-zero encap attribute.\n");
1682 OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
1687 err = ovs_key_from_nlattrs(match, key_attrs, a, false);
1692 err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
1696 if (mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) {
1697 __be16 eth_type = 0;
1701 OVS_NLERR("Encap mask attribute is set for non-VLAN frame.\n");
1705 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
1706 if (a[OVS_KEY_ATTR_ETHERTYPE])
1707 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1709 if (eth_type == htons(0xffff)) {
1710 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1711 encap = a[OVS_KEY_ATTR_ENCAP];
1712 err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
1714 OVS_NLERR("VLAN frames must have an exact match on the TPID (mask=%x).\n",
1719 if (a[OVS_KEY_ATTR_VLAN])
1720 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1722 if (!(tci & htons(VLAN_TAG_PRESENT))) {
1723 OVS_NLERR("VLAN tag present bit must have an exact match (tci_mask=%x).\n", ntohs(tci));
1728 err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
1732 /* Populate exact match flow's key mask. */
1734 ovs_sw_flow_mask_set(match->mask, &match->range, 0xff);
1737 if (!ovs_match_validate(match, key_attrs, mask_attrs))
1744 * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
1745 * @flow: Receives extracted in_port, priority, tun_key and skb_mark.
1746 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1749 * This parses a series of Netlink attributes that form a flow key, which must
1750 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1751 * get the metadata, that is, the parts of the flow key that cannot be
1752 * extracted from the packet itself.
1755 int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow,
1756 const struct nlattr *attr)
1758 struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key;
1759 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1762 struct sw_flow_match match;
1764 flow->key.phy.in_port = DP_MAX_PORTS;
1765 flow->key.phy.priority = 0;
1766 flow->key.phy.skb_mark = 0;
1767 memset(tun_key, 0, sizeof(flow->key.tun_key));
1769 err = parse_flow_nlattrs(attr, a, &attrs);
1773 memset(&match, 0, sizeof(match));
1774 match.key = &flow->key;
1776 err = metadata_from_nlattrs(&match, &attrs, a, false);
1783 int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey,
1784 const struct sw_flow_key *output, struct sk_buff *skb)
1786 struct ovs_key_ethernet *eth_key;
1787 struct nlattr *nla, *encap;
1788 bool is_mask = (swkey != output);
1790 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1791 goto nla_put_failure;
1793 if ((swkey->tun_key.ipv4_dst || is_mask) &&
1794 ovs_ipv4_tun_to_nlattr(skb, &swkey->tun_key, &output->tun_key))
1795 goto nla_put_failure;
1797 if (swkey->phy.in_port == DP_MAX_PORTS) {
1798 if (is_mask && (output->phy.in_port == 0xffff))
1799 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1800 goto nla_put_failure;
1803 upper_u16 = !is_mask ? 0 : 0xffff;
1805 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1806 (upper_u16 << 16) | output->phy.in_port))
1807 goto nla_put_failure;
1810 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1811 goto nla_put_failure;
1813 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1815 goto nla_put_failure;
1817 eth_key = nla_data(nla);
1818 memcpy(eth_key->eth_src, output->eth.src, ETH_ALEN);
1819 memcpy(eth_key->eth_dst, output->eth.dst, ETH_ALEN);
1821 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1823 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
1824 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1825 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1826 goto nla_put_failure;
1827 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1828 if (!swkey->eth.tci)
1833 if (swkey->eth.type == htons(ETH_P_802_2)) {
1835 * Ethertype 802.2 is represented in the netlink with omitted
1836 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1837 * 0xffff in the mask attribute. Ethertype can also
1840 if (is_mask && output->eth.type)
1841 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1843 goto nla_put_failure;
1847 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1848 goto nla_put_failure;
1850 if (swkey->eth.type == htons(ETH_P_IP)) {
1851 struct ovs_key_ipv4 *ipv4_key;
1853 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1855 goto nla_put_failure;
1856 ipv4_key = nla_data(nla);
1857 ipv4_key->ipv4_src = output->ipv4.addr.src;
1858 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1859 ipv4_key->ipv4_proto = output->ip.proto;
1860 ipv4_key->ipv4_tos = output->ip.tos;
1861 ipv4_key->ipv4_ttl = output->ip.ttl;
1862 ipv4_key->ipv4_frag = output->ip.frag;
1863 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1864 struct ovs_key_ipv6 *ipv6_key;
1866 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1868 goto nla_put_failure;
1869 ipv6_key = nla_data(nla);
1870 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1871 sizeof(ipv6_key->ipv6_src));
1872 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1873 sizeof(ipv6_key->ipv6_dst));
1874 ipv6_key->ipv6_label = output->ipv6.label;
1875 ipv6_key->ipv6_proto = output->ip.proto;
1876 ipv6_key->ipv6_tclass = output->ip.tos;
1877 ipv6_key->ipv6_hlimit = output->ip.ttl;
1878 ipv6_key->ipv6_frag = output->ip.frag;
1879 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1880 swkey->eth.type == htons(ETH_P_RARP)) {
1881 struct ovs_key_arp *arp_key;
1883 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1885 goto nla_put_failure;
1886 arp_key = nla_data(nla);
1887 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1888 arp_key->arp_sip = output->ipv4.addr.src;
1889 arp_key->arp_tip = output->ipv4.addr.dst;
1890 arp_key->arp_op = htons(output->ip.proto);
1891 memcpy(arp_key->arp_sha, output->ipv4.arp.sha, ETH_ALEN);
1892 memcpy(arp_key->arp_tha, output->ipv4.arp.tha, ETH_ALEN);
1895 if ((swkey->eth.type == htons(ETH_P_IP) ||
1896 swkey->eth.type == htons(ETH_P_IPV6)) &&
1897 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1899 if (swkey->ip.proto == IPPROTO_TCP) {
1900 struct ovs_key_tcp *tcp_key;
1902 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1904 goto nla_put_failure;
1905 tcp_key = nla_data(nla);
1906 if (swkey->eth.type == htons(ETH_P_IP)) {
1907 tcp_key->tcp_src = output->ipv4.tp.src;
1908 tcp_key->tcp_dst = output->ipv4.tp.dst;
1909 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1910 tcp_key->tcp_src = output->ipv6.tp.src;
1911 tcp_key->tcp_dst = output->ipv6.tp.dst;
1913 } else if (swkey->ip.proto == IPPROTO_UDP) {
1914 struct ovs_key_udp *udp_key;
1916 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1918 goto nla_put_failure;
1919 udp_key = nla_data(nla);
1920 if (swkey->eth.type == htons(ETH_P_IP)) {
1921 udp_key->udp_src = output->ipv4.tp.src;
1922 udp_key->udp_dst = output->ipv4.tp.dst;
1923 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1924 udp_key->udp_src = output->ipv6.tp.src;
1925 udp_key->udp_dst = output->ipv6.tp.dst;
1927 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1928 struct ovs_key_sctp *sctp_key;
1930 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1932 goto nla_put_failure;
1933 sctp_key = nla_data(nla);
1934 if (swkey->eth.type == htons(ETH_P_IP)) {
1935 sctp_key->sctp_src = swkey->ipv4.tp.src;
1936 sctp_key->sctp_dst = swkey->ipv4.tp.dst;
1937 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1938 sctp_key->sctp_src = swkey->ipv6.tp.src;
1939 sctp_key->sctp_dst = swkey->ipv6.tp.dst;
1941 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1942 swkey->ip.proto == IPPROTO_ICMP) {
1943 struct ovs_key_icmp *icmp_key;
1945 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1947 goto nla_put_failure;
1948 icmp_key = nla_data(nla);
1949 icmp_key->icmp_type = ntohs(output->ipv4.tp.src);
1950 icmp_key->icmp_code = ntohs(output->ipv4.tp.dst);
1951 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1952 swkey->ip.proto == IPPROTO_ICMPV6) {
1953 struct ovs_key_icmpv6 *icmpv6_key;
1955 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1956 sizeof(*icmpv6_key));
1958 goto nla_put_failure;
1959 icmpv6_key = nla_data(nla);
1960 icmpv6_key->icmpv6_type = ntohs(output->ipv6.tp.src);
1961 icmpv6_key->icmpv6_code = ntohs(output->ipv6.tp.dst);
1963 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1964 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1965 struct ovs_key_nd *nd_key;
1967 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1969 goto nla_put_failure;
1970 nd_key = nla_data(nla);
1971 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1972 sizeof(nd_key->nd_target));
1973 memcpy(nd_key->nd_sll, output->ipv6.nd.sll, ETH_ALEN);
1974 memcpy(nd_key->nd_tll, output->ipv6.nd.tll, ETH_ALEN);
1981 nla_nest_end(skb, encap);
1989 /* Initializes the flow module.
1990 * Returns zero if successful or a negative error code. */
1991 int ovs_flow_init(void)
1993 BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
1994 BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
1996 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
1998 if (flow_cache == NULL)
2004 /* Uninitializes the flow module. */
2005 void ovs_flow_exit(void)
2007 kmem_cache_destroy(flow_cache);
2010 struct sw_flow_mask *ovs_sw_flow_mask_alloc(void)
2012 struct sw_flow_mask *mask;
2014 mask = kmalloc(sizeof(*mask), GFP_KERNEL);
2016 mask->ref_count = 0;
2021 void ovs_sw_flow_mask_add_ref(struct sw_flow_mask *mask)
2026 static void rcu_free_sw_flow_mask_cb(struct rcu_head *rcu)
2028 struct sw_flow_mask *mask = container_of(rcu, struct sw_flow_mask, rcu);
2033 void ovs_sw_flow_mask_del_ref(struct sw_flow_mask *mask, bool deferred)
2038 BUG_ON(!mask->ref_count);
2041 if (!mask->ref_count) {
2042 list_del_rcu(&mask->list);
2044 call_rcu(&mask->rcu, rcu_free_sw_flow_mask_cb);
2050 static bool ovs_sw_flow_mask_equal(const struct sw_flow_mask *a,
2051 const struct sw_flow_mask *b)
2053 u8 *a_ = (u8 *)&a->key + a->range.start;
2054 u8 *b_ = (u8 *)&b->key + b->range.start;
2056 return (a->range.end == b->range.end)
2057 && (a->range.start == b->range.start)
2058 && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
2061 struct sw_flow_mask *ovs_sw_flow_mask_find(const struct flow_table *tbl,
2062 const struct sw_flow_mask *mask)
2064 struct list_head *ml;
2066 list_for_each(ml, tbl->mask_list) {
2067 struct sw_flow_mask *m;
2068 m = container_of(ml, struct sw_flow_mask, list);
2069 if (ovs_sw_flow_mask_equal(mask, m))
2077 * add a new mask into the mask list.
2078 * The caller needs to make sure that 'mask' is not the same
2079 * as any masks that are already on the list.
2081 void ovs_sw_flow_mask_insert(struct flow_table *tbl, struct sw_flow_mask *mask)
2083 list_add_rcu(&mask->list, tbl->mask_list);
2087 * Set 'range' fields in the mask to the value of 'val'.
2089 static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask,
2090 struct sw_flow_key_range *range, u8 val)
2092 u8 *m = (u8 *)&mask->key + range->start;
2094 mask->range = *range;
2095 memset(m, val, range_n_bytes(range));