flow: Adds support for arbitrary ethernet masking
[sliver-openvswitch.git] / lib / meta-flow.c
1 /*
2  * Copyright (c) 2011, 2012 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include "meta-flow.h"
20
21 #include <assert.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <netinet/icmp6.h>
25 #include <netinet/ip6.h>
26
27 #include "classifier.h"
28 #include "dynamic-string.h"
29 #include "ofp-errors.h"
30 #include "ofp-util.h"
31 #include "packets.h"
32 #include "random.h"
33 #include "shash.h"
34 #include "socket-util.h"
35 #include "unaligned.h"
36 #include "vlog.h"
37
38 VLOG_DEFINE_THIS_MODULE(meta_flow);
39
40 #define MF_FIELD_SIZES(MEMBER)                  \
41     sizeof ((union mf_value *)0)->MEMBER,       \
42     8 * sizeof ((union mf_value *)0)->MEMBER
43
44 static const struct mf_field mf_fields[MFF_N_IDS] = {
45     /* ## -------- ## */
46     /* ## metadata ## */
47     /* ## -------- ## */
48
49     {
50         MFF_TUN_ID, "tun_id", NULL,
51         MF_FIELD_SIZES(be64),
52         MFM_FULLY, 0,
53         MFS_HEXADECIMAL,
54         MFP_NONE,
55         true,
56         NXM_NX_TUN_ID, "NXM_NX_TUN_ID",
57         0, NULL,
58     }, {
59         MFF_IN_PORT, "in_port", NULL,
60         MF_FIELD_SIZES(be16),
61         MFM_NONE, FWW_IN_PORT,
62         MFS_OFP_PORT,
63         MFP_NONE,
64         false,
65         NXM_OF_IN_PORT, "NXM_OF_IN_PORT",
66         OXM_OF_IN_PORT, "OXM_OF_IN_PORT",
67     },
68
69 #define REGISTER(IDX)                           \
70     {                                           \
71         MFF_REG##IDX, "reg" #IDX, NULL,         \
72         MF_FIELD_SIZES(be32),                   \
73         MFM_FULLY, 0,                           \
74         MFS_HEXADECIMAL,                        \
75         MFP_NONE,                               \
76         true,                                   \
77         NXM_NX_REG(IDX),                        \
78         "NXM_NX_REG" #IDX,                      \
79         0, NULL,                                \
80     }
81 #if FLOW_N_REGS > 0
82     REGISTER(0),
83 #endif
84 #if FLOW_N_REGS > 1
85     REGISTER(1),
86 #endif
87 #if FLOW_N_REGS > 2
88     REGISTER(2),
89 #endif
90 #if FLOW_N_REGS > 3
91     REGISTER(3),
92 #endif
93 #if FLOW_N_REGS > 4
94     REGISTER(4),
95 #endif
96 #if FLOW_N_REGS > 5
97     REGISTER(5),
98 #endif
99 #if FLOW_N_REGS > 6
100     REGISTER(6),
101 #endif
102 #if FLOW_N_REGS > 7
103     REGISTER(7),
104 #endif
105 #if FLOW_N_REGS > 8
106 #error
107 #endif
108
109     /* ## -- ## */
110     /* ## L2 ## */
111     /* ## -- ## */
112
113     {
114         MFF_ETH_SRC, "eth_src", "dl_src",
115         MF_FIELD_SIZES(mac),
116         MFM_FULLY, 0,
117         MFS_ETHERNET,
118         MFP_NONE,
119         true,
120         NXM_OF_ETH_SRC, "NXM_OF_ETH_SRC",
121         OXM_OF_ETH_SRC, "OXM_OF_ETH_SRC",
122     }, {
123         MFF_ETH_DST, "eth_dst", "dl_dst",
124         MF_FIELD_SIZES(mac),
125         MFM_FULLY, 0,
126         MFS_ETHERNET,
127         MFP_NONE,
128         true,
129         NXM_OF_ETH_DST, "NXM_OF_ETH_DST",
130         OXM_OF_ETH_DST, "OXM_OF_ETH_DST",
131     }, {
132         MFF_ETH_TYPE, "eth_type", "dl_type",
133         MF_FIELD_SIZES(be16),
134         MFM_NONE, FWW_DL_TYPE,
135         MFS_HEXADECIMAL,
136         MFP_NONE,
137         false,
138         NXM_OF_ETH_TYPE, "NXM_OF_ETH_TYPE",
139         OXM_OF_ETH_TYPE, "OXM_OF_ETH_TYPE",
140     },
141
142     {
143         MFF_VLAN_TCI, "vlan_tci", NULL,
144         MF_FIELD_SIZES(be16),
145         MFM_FULLY, 0,
146         MFS_HEXADECIMAL,
147         MFP_NONE,
148         true,
149         NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI",
150         0, NULL,
151     }, {
152         MFF_VLAN_VID, "dl_vlan", NULL,
153         sizeof(ovs_be16), 12,
154         MFM_NONE, 0,
155         MFS_DECIMAL,
156         MFP_NONE,
157         true,
158         0, NULL,
159         OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID",
160     }, {
161         MFF_VLAN_PCP, "dl_vlan_pcp", NULL,
162         1, 3,
163         MFM_NONE, 0,
164         MFS_DECIMAL,
165         MFP_NONE,
166         true,
167         0, NULL,
168         OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP",
169     },
170
171     /* ## -- ## */
172     /* ## L3 ## */
173     /* ## -- ## */
174
175     {
176         MFF_IPV4_SRC, "ip_src", "nw_src",
177         MF_FIELD_SIZES(be32),
178         MFM_CIDR, 0,
179         MFS_IPV4,
180         MFP_IPV4,
181         true,
182         NXM_OF_IP_SRC, "NXM_OF_IP_SRC",
183         OXM_OF_IPV4_SRC, "OXM_OF_IPV4_SRC",
184     }, {
185         MFF_IPV4_DST, "ip_dst", "nw_dst",
186         MF_FIELD_SIZES(be32),
187         MFM_CIDR, 0,
188         MFS_IPV4,
189         MFP_IPV4,
190         true,
191         NXM_OF_IP_DST, "NXM_OF_IP_DST",
192         OXM_OF_IPV4_DST, "OXM_OF_IPV4_DST",
193     },
194
195     {
196         MFF_IPV6_SRC, "ipv6_src", NULL,
197         MF_FIELD_SIZES(ipv6),
198         MFM_CIDR, 0,
199         MFS_IPV6,
200         MFP_IPV6,
201         true,
202         NXM_NX_IPV6_SRC, "NXM_NX_IPV6_SRC",
203         OXM_OF_IPV6_SRC, "OXM_OF_IPV6_SRC",
204     }, {
205         MFF_IPV6_DST, "ipv6_dst", NULL,
206         MF_FIELD_SIZES(ipv6),
207         MFM_CIDR, 0,
208         MFS_IPV6,
209         MFP_IPV6,
210         true,
211         NXM_NX_IPV6_DST, "NXM_NX_IPV6_DST",
212         OXM_OF_IPV6_DST, "OXM_OF_IPV6_DST",
213     },
214     {
215         MFF_IPV6_LABEL, "ipv6_label", NULL,
216         4, 20,
217         MFM_NONE, FWW_IPV6_LABEL,
218         MFS_HEXADECIMAL,
219         MFP_IPV6,
220         false,
221         NXM_NX_IPV6_LABEL, "NXM_NX_IPV6_LABEL",
222         OXM_OF_IPV6_FLABEL, "OXM_OF_IPV6_FLABEL",
223     },
224
225     {
226         MFF_IP_PROTO, "nw_proto", NULL,
227         MF_FIELD_SIZES(u8),
228         MFM_NONE, FWW_NW_PROTO,
229         MFS_DECIMAL,
230         MFP_IP_ANY,
231         false,
232         NXM_OF_IP_PROTO, "NXM_OF_IP_PROTO",
233         OXM_OF_IP_PROTO, "OXM_OF_IP_PROTO",
234     }, {
235         MFF_IP_DSCP, "nw_tos", NULL,
236         MF_FIELD_SIZES(u8),
237         MFM_NONE, FWW_NW_DSCP,
238         MFS_DECIMAL,
239         MFP_IP_ANY,
240         true,
241         NXM_OF_IP_TOS, "NXM_OF_IP_TOS",
242         OXM_OF_IP_DSCP, "OXM_OF_IP_DSCP",
243     }, {
244         MFF_IP_ECN, "nw_ecn", NULL,
245         1, 2,
246         MFM_NONE, FWW_NW_ECN,
247         MFS_DECIMAL,
248         MFP_IP_ANY,
249         true,
250         NXM_NX_IP_ECN, "NXM_NX_IP_ECN",
251         OXM_OF_IP_ECN, "OXM_OF_IP_ECN",
252     }, {
253         MFF_IP_TTL, "nw_ttl", NULL,
254         MF_FIELD_SIZES(u8),
255         MFM_NONE, FWW_NW_TTL,
256         MFS_DECIMAL,
257         MFP_IP_ANY,
258         true,
259         NXM_NX_IP_TTL, "NXM_NX_IP_TTL",
260         0, NULL,
261     }, {
262         MFF_IP_FRAG, "ip_frag", NULL,
263         1, 2,
264         MFM_FULLY, 0,
265         MFS_FRAG,
266         MFP_IP_ANY,
267         false,
268         NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG",
269         0, NULL,
270     },
271
272     {
273         MFF_ARP_OP, "arp_op", NULL,
274         MF_FIELD_SIZES(be16),
275         MFM_NONE, FWW_NW_PROTO,
276         MFS_DECIMAL,
277         MFP_ARP,
278         false,
279         NXM_OF_ARP_OP, "NXM_OF_ARP_OP",
280         OXM_OF_ARP_OP, "OXM_OF_ARP_OP",
281     }, {
282         MFF_ARP_SPA, "arp_spa", NULL,
283         MF_FIELD_SIZES(be32),
284         MFM_CIDR, 0,
285         MFS_IPV4,
286         MFP_ARP,
287         false,
288         NXM_OF_ARP_SPA, "NXM_OF_ARP_SPA",
289         OXM_OF_ARP_SPA, "OXM_OF_ARP_SPA",
290     }, {
291         MFF_ARP_TPA, "arp_tpa", NULL,
292         MF_FIELD_SIZES(be32),
293         MFM_CIDR, 0,
294         MFS_IPV4,
295         MFP_ARP,
296         false,
297         NXM_OF_ARP_TPA, "NXM_OF_ARP_TPA",
298         OXM_OF_ARP_TPA, "OXM_OF_ARP_TPA",
299     }, {
300         MFF_ARP_SHA, "arp_sha", NULL,
301         MF_FIELD_SIZES(mac),
302         MFM_NONE, FWW_ARP_SHA,
303         MFS_ETHERNET,
304         MFP_ARP,
305         false,
306         NXM_NX_ARP_SHA, "NXM_NX_ARP_SHA",
307         OXM_OF_ARP_SHA, "OXM_OF_ARP_SHA",
308     }, {
309         MFF_ARP_THA, "arp_tha", NULL,
310         MF_FIELD_SIZES(mac),
311         MFM_NONE, FWW_ARP_THA,
312         MFS_ETHERNET,
313         MFP_ARP,
314         false,
315         NXM_NX_ARP_THA, "NXM_NX_ARP_THA",
316         OXM_OF_ARP_THA, "OXM_OF_ARP_THA",
317     },
318
319     /* ## -- ## */
320     /* ## L4 ## */
321     /* ## -- ## */
322
323     {
324         MFF_TCP_SRC, "tcp_src", "tp_src",
325         MF_FIELD_SIZES(be16),
326         MFM_FULLY, 0,
327         MFS_DECIMAL,
328         MFP_TCP,
329         true,
330         NXM_OF_TCP_SRC, "NXM_OF_TCP_SRC",
331         OXM_OF_TCP_SRC, "OXM_OF_TCP_SRC",
332     }, {
333         MFF_TCP_DST, "tcp_dst", "tp_dst",
334         MF_FIELD_SIZES(be16),
335         MFM_FULLY, 0,
336         MFS_DECIMAL,
337         MFP_TCP,
338         true,
339         NXM_OF_TCP_DST, "NXM_OF_TCP_DST",
340         OXM_OF_TCP_DST, "OXM_OF_TCP_DST",
341     },
342
343     {
344         MFF_UDP_SRC, "udp_src", NULL,
345         MF_FIELD_SIZES(be16),
346         MFM_FULLY, 0,
347         MFS_DECIMAL,
348         MFP_UDP,
349         true,
350         NXM_OF_UDP_SRC, "NXM_OF_UDP_SRC",
351         OXM_OF_UDP_SRC, "OXM_OF_UDP_SRC",
352     }, {
353         MFF_UDP_DST, "udp_dst", NULL,
354         MF_FIELD_SIZES(be16),
355         MFM_FULLY, 0,
356         MFS_DECIMAL,
357         MFP_UDP,
358         true,
359         NXM_OF_UDP_DST, "NXM_OF_UDP_DST",
360         OXM_OF_UDP_DST, "OXM_OF_UDP_DST",
361     },
362
363     {
364         MFF_ICMPV4_TYPE, "icmp_type", NULL,
365         MF_FIELD_SIZES(u8),
366         MFM_NONE, 0,
367         MFS_DECIMAL,
368         MFP_ICMPV4,
369         false,
370         NXM_OF_ICMP_TYPE, "NXM_OF_ICMP_TYPE",
371         OXM_OF_ICMPV4_TYPE, "OXM_OF_ICMPV4_TYPE",
372     }, {
373         MFF_ICMPV4_CODE, "icmp_code", NULL,
374         MF_FIELD_SIZES(u8),
375         MFM_NONE, 0,
376         MFS_DECIMAL,
377         MFP_ICMPV4,
378         false,
379         NXM_OF_ICMP_CODE, "NXM_OF_ICMP_CODE",
380         OXM_OF_ICMPV4_CODE, "OXM_OF_ICMPV4_CODE",
381     },
382
383     {
384         MFF_ICMPV6_TYPE, "icmpv6_type", NULL,
385         MF_FIELD_SIZES(u8),
386         MFM_NONE, 0,
387         MFS_DECIMAL,
388         MFP_ICMPV6,
389         false,
390         NXM_NX_ICMPV6_TYPE, "NXM_NX_ICMPV6_TYPE",
391         OXM_OF_ICMPV6_TYPE, "OXM_OF_ICMPV6_TYPE",
392     }, {
393         MFF_ICMPV6_CODE, "icmpv6_code", NULL,
394         MF_FIELD_SIZES(u8),
395         MFM_NONE, 0,
396         MFS_DECIMAL,
397         MFP_ICMPV6,
398         false,
399         NXM_NX_ICMPV6_CODE, "NXM_NX_ICMPV6_CODE",
400         OXM_OF_ICMPV6_CODE, "OXM_OF_ICMPV6_CODE",
401     },
402
403     /* ## ---- ## */
404     /* ## L"5" ## */
405     /* ## ---- ## */
406
407     {
408         MFF_ND_TARGET, "nd_target", NULL,
409         MF_FIELD_SIZES(ipv6),
410         MFM_CIDR, 0,
411         MFS_IPV6,
412         MFP_ND,
413         false,
414         NXM_NX_ND_TARGET, "NXM_NX_ND_TARGET",
415         OXM_OF_IPV6_ND_TARGET, "OXM_OF_IPV6_ND_TARGET",
416     }, {
417         MFF_ND_SLL, "nd_sll", NULL,
418         MF_FIELD_SIZES(mac),
419         MFM_NONE, FWW_ARP_SHA,
420         MFS_ETHERNET,
421         MFP_ND_SOLICIT,
422         false,
423         NXM_NX_ND_SLL, "NXM_NX_ND_SLL",
424         OXM_OF_IPV6_ND_SLL, "OXM_OF_IPV6_ND_SLL",
425     }, {
426         MFF_ND_TLL, "nd_tll", NULL,
427         MF_FIELD_SIZES(mac),
428         MFM_NONE, FWW_ARP_THA,
429         MFS_ETHERNET,
430         MFP_ND_ADVERT,
431         false,
432         NXM_NX_ND_TLL, "NXM_NX_ND_TLL",
433         OXM_OF_IPV6_ND_TLL, "OXM_OF_IPV6_ND_TLL",
434     }
435 };
436
437 struct nxm_field {
438     struct hmap_node hmap_node;
439     uint32_t nxm_header;
440     const struct mf_field *mf;
441 };
442
443 static struct hmap all_nxm_fields = HMAP_INITIALIZER(&all_nxm_fields);
444
445 /* Rate limit for parse errors.  These always indicate a bug in an OpenFlow
446  * controller and so there's not much point in showing a lot of them. */
447 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
448
449 /* Returns the field with the given 'id'. */
450 const struct mf_field *
451 mf_from_id(enum mf_field_id id)
452 {
453     assert((unsigned int) id < MFF_N_IDS);
454     return &mf_fields[id];
455 }
456
457 /* Returns the field with the given 'name', or a null pointer if no field has
458  * that name. */
459 const struct mf_field *
460 mf_from_name(const char *name)
461 {
462     static struct shash mf_by_name = SHASH_INITIALIZER(&mf_by_name);
463
464     if (shash_is_empty(&mf_by_name)) {
465         const struct mf_field *mf;
466
467         for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
468             shash_add_once(&mf_by_name, mf->name, mf);
469             if (mf->extra_name) {
470                 shash_add_once(&mf_by_name, mf->extra_name, mf);
471             }
472         }
473     }
474
475     return shash_find_data(&mf_by_name, name);
476 }
477
478 static void
479 add_nxm_field(uint32_t nxm_header, const struct mf_field *mf)
480 {
481     struct nxm_field *f;
482
483     f = xmalloc(sizeof *f);
484     hmap_insert(&all_nxm_fields, &f->hmap_node, hash_int(nxm_header, 0));
485     f->nxm_header = nxm_header;
486     f->mf = mf;
487 }
488
489 static void
490 nxm_init(void)
491 {
492     const struct mf_field *mf;
493
494     for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
495         if (mf->nxm_header) {
496             add_nxm_field(mf->nxm_header, mf);
497             if (mf->maskable != MFM_NONE) {
498                 add_nxm_field(NXM_MAKE_WILD_HEADER(mf->nxm_header), mf);
499             }
500         }
501     }
502
503 #ifndef NDEBUG
504     /* Verify that the header values are unique. */
505     for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
506         if (mf->nxm_header) {
507             assert(mf_from_nxm_header(mf->nxm_header) == mf);
508             if (mf->maskable != MFM_NONE) {
509                 assert(mf_from_nxm_header(NXM_MAKE_WILD_HEADER(mf->nxm_header))
510                        == mf);
511             }
512         }
513     }
514 #endif
515 }
516
517 const struct mf_field *
518 mf_from_nxm_header(uint32_t header)
519 {
520     const struct nxm_field *f;
521
522     if (hmap_is_empty(&all_nxm_fields)) {
523         nxm_init();
524     }
525
526     HMAP_FOR_EACH_IN_BUCKET (f, hmap_node, hash_int(header, 0),
527                              &all_nxm_fields) {
528         if (f->nxm_header == header) {
529             return f->mf;
530         }
531     }
532
533     return NULL;
534 }
535
536 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
537  * specifies at least one bit in the field.
538  *
539  * The caller is responsible for ensuring that 'wc' corresponds to a flow that
540  * meets 'mf''s prerequisites. */
541 bool
542 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
543 {
544     switch (mf->id) {
545     case MFF_IN_PORT:
546     case MFF_ETH_TYPE:
547     case MFF_IP_PROTO:
548     case MFF_IP_DSCP:
549     case MFF_IP_ECN:
550     case MFF_IP_TTL:
551     case MFF_IPV6_LABEL:
552     case MFF_ARP_OP:
553     case MFF_ARP_SHA:
554     case MFF_ARP_THA:
555     case MFF_ND_SLL:
556     case MFF_ND_TLL:
557         assert(mf->fww_bit != 0);
558         return (wc->wildcards & mf->fww_bit) != 0;
559
560     case MFF_TUN_ID:
561         return !wc->tun_id_mask;
562
563 #if FLOW_N_REGS > 0
564     case MFF_REG0:
565 #endif
566 #if FLOW_N_REGS > 1
567     case MFF_REG1:
568 #endif
569 #if FLOW_N_REGS > 2
570     case MFF_REG2:
571 #endif
572 #if FLOW_N_REGS > 3
573     case MFF_REG3:
574 #endif
575 #if FLOW_N_REGS > 4
576     case MFF_REG4:
577 #endif
578 #if FLOW_N_REGS > 5
579     case MFF_REG5:
580 #endif
581 #if FLOW_N_REGS > 6
582     case MFF_REG6:
583 #endif
584 #if FLOW_N_REGS > 7
585     case MFF_REG7:
586 #endif
587 #if FLOW_N_REGS > 8
588 #error
589 #endif
590         return !wc->reg_masks[mf->id - MFF_REG0];
591
592     case MFF_ETH_SRC:
593         return eth_addr_is_zero(wc->dl_src_mask);
594     case MFF_ETH_DST:
595         return eth_addr_is_zero(wc->dl_dst_mask);
596
597     case MFF_VLAN_TCI:
598         return !wc->vlan_tci_mask;
599     case MFF_VLAN_VID:
600         return !(wc->vlan_tci_mask & htons(VLAN_VID_MASK));
601     case MFF_VLAN_PCP:
602         return !(wc->vlan_tci_mask & htons(VLAN_PCP_MASK));
603
604     case MFF_IPV4_SRC:
605         return !wc->nw_src_mask;
606     case MFF_IPV4_DST:
607         return !wc->nw_dst_mask;
608
609     case MFF_IPV6_SRC:
610         return ipv6_mask_is_any(&wc->ipv6_src_mask);
611     case MFF_IPV6_DST:
612         return ipv6_mask_is_any(&wc->ipv6_dst_mask);
613
614     case MFF_ND_TARGET:
615         return ipv6_mask_is_any(&wc->nd_target_mask);
616
617     case MFF_IP_FRAG:
618         return !(wc->nw_frag_mask & FLOW_NW_FRAG_MASK);
619
620     case MFF_ARP_SPA:
621         return !wc->nw_src_mask;
622     case MFF_ARP_TPA:
623         return !wc->nw_dst_mask;
624
625     case MFF_TCP_SRC:
626     case MFF_UDP_SRC:
627     case MFF_ICMPV4_TYPE:
628     case MFF_ICMPV6_TYPE:
629         return !wc->tp_src_mask;
630     case MFF_TCP_DST:
631     case MFF_UDP_DST:
632     case MFF_ICMPV4_CODE:
633     case MFF_ICMPV6_CODE:
634         return !wc->tp_dst_mask;
635
636     case MFF_N_IDS:
637     default:
638         NOT_REACHED();
639     }
640 }
641
642 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
643  * Each bit in 'mask' will be set to 1 if the bit is significant for matching
644  * purposes, or to 0 if it is wildcarded.
645  *
646  * The caller is responsible for ensuring that 'wc' corresponds to a flow that
647  * meets 'mf''s prerequisites. */
648 void
649 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
650             union mf_value *mask)
651 {
652     switch (mf->id) {
653     case MFF_IN_PORT:
654     case MFF_ETH_TYPE:
655     case MFF_IP_PROTO:
656     case MFF_IP_DSCP:
657     case MFF_IP_ECN:
658     case MFF_IP_TTL:
659     case MFF_IPV6_LABEL:
660     case MFF_ARP_OP:
661     case MFF_ARP_SHA:
662     case MFF_ARP_THA:
663     case MFF_ND_SLL:
664     case MFF_ND_TLL:
665         assert(mf->fww_bit != 0);
666         memset(mask, wc->wildcards & mf->fww_bit ? 0x00 : 0xff, mf->n_bytes);
667         break;
668
669     case MFF_TUN_ID:
670         mask->be64 = wc->tun_id_mask;
671         break;
672
673 #if FLOW_N_REGS > 0
674     case MFF_REG0:
675 #endif
676 #if FLOW_N_REGS > 1
677     case MFF_REG1:
678 #endif
679 #if FLOW_N_REGS > 2
680     case MFF_REG2:
681 #endif
682 #if FLOW_N_REGS > 3
683     case MFF_REG3:
684 #endif
685 #if FLOW_N_REGS > 4
686     case MFF_REG4:
687 #endif
688 #if FLOW_N_REGS > 5
689     case MFF_REG5:
690 #endif
691 #if FLOW_N_REGS > 6
692     case MFF_REG6:
693 #endif
694 #if FLOW_N_REGS > 7
695     case MFF_REG7:
696 #endif
697 #if FLOW_N_REGS > 8
698 #error
699 #endif
700         mask->be32 = htonl(wc->reg_masks[mf->id - MFF_REG0]);
701         break;
702
703     case MFF_ETH_DST:
704         memcpy(mask->mac, wc->dl_dst_mask, ETH_ADDR_LEN);
705         break;
706
707     case MFF_ETH_SRC:
708         memcpy(mask->mac, wc->dl_src_mask, ETH_ADDR_LEN);
709         break;
710
711     case MFF_VLAN_TCI:
712         mask->be16 = wc->vlan_tci_mask;
713         break;
714     case MFF_VLAN_VID:
715         mask->be16 = wc->vlan_tci_mask & htons(VLAN_VID_MASK);
716         break;
717     case MFF_VLAN_PCP:
718         mask->u8 = vlan_tci_to_pcp(wc->vlan_tci_mask);
719         break;
720
721     case MFF_IPV4_SRC:
722         mask->be32 = wc->nw_src_mask;
723         break;
724     case MFF_IPV4_DST:
725         mask->be32 = wc->nw_dst_mask;
726         break;
727
728     case MFF_IPV6_SRC:
729         mask->ipv6 = wc->ipv6_src_mask;
730         break;
731     case MFF_IPV6_DST:
732         mask->ipv6 = wc->ipv6_dst_mask;
733         break;
734
735     case MFF_ND_TARGET:
736         mask->ipv6 = wc->nd_target_mask;
737         break;
738
739     case MFF_IP_FRAG:
740         mask->u8 = wc->nw_frag_mask & FLOW_NW_FRAG_MASK;
741         break;
742
743     case MFF_ARP_SPA:
744         mask->be32 = wc->nw_src_mask;
745         break;
746     case MFF_ARP_TPA:
747         mask->be32 = wc->nw_dst_mask;
748         break;
749
750     case MFF_TCP_SRC:
751     case MFF_UDP_SRC:
752         mask->be16 = wc->tp_src_mask;
753         break;
754     case MFF_TCP_DST:
755     case MFF_UDP_DST:
756         mask->be16 = wc->tp_dst_mask;
757         break;
758
759     case MFF_ICMPV4_TYPE:
760     case MFF_ICMPV6_TYPE:
761         mask->u8 = ntohs(wc->tp_src_mask);
762         break;
763     case MFF_ICMPV4_CODE:
764     case MFF_ICMPV6_CODE:
765         mask->u8 = ntohs(wc->tp_dst_mask);
766         break;
767
768     case MFF_N_IDS:
769     default:
770         NOT_REACHED();
771     }
772 }
773
774 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'.  Returns true
775  * if the mask is valid, false otherwise. */
776 bool
777 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
778 {
779     switch (mf->maskable) {
780     case MFM_NONE:
781         return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
782                 is_all_ones((const uint8_t *) mask, mf->n_bytes));
783
784     case MFM_FULLY:
785         return true;
786
787     case MFM_CIDR:
788         return (mf->n_bytes == 4
789                 ? ip_is_cidr(mask->be32)
790                 : ipv6_is_cidr(&mask->ipv6));
791     }
792
793     NOT_REACHED();
794 }
795
796 static bool
797 is_ip_any(const struct flow *flow)
798 {
799     return (flow->dl_type == htons(ETH_TYPE_IP) ||
800             flow->dl_type == htons(ETH_TYPE_IPV6));
801 }
802
803 static bool
804 is_icmpv4(const struct flow *flow)
805 {
806     return (flow->dl_type == htons(ETH_TYPE_IP)
807             && flow->nw_proto == IPPROTO_ICMP);
808 }
809
810 static bool
811 is_icmpv6(const struct flow *flow)
812 {
813     return (flow->dl_type == htons(ETH_TYPE_IPV6)
814             && flow->nw_proto == IPPROTO_ICMPV6);
815 }
816
817 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
818 bool
819 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
820 {
821     switch (mf->prereqs) {
822     case MFP_NONE:
823         return true;
824
825     case MFP_ARP:
826         return flow->dl_type == htons(ETH_TYPE_ARP);
827     case MFP_IPV4:
828         return flow->dl_type == htons(ETH_TYPE_IP);
829     case MFP_IPV6:
830         return flow->dl_type == htons(ETH_TYPE_IPV6);
831     case MFP_IP_ANY:
832         return is_ip_any(flow);
833
834     case MFP_TCP:
835         return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
836     case MFP_UDP:
837         return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
838     case MFP_ICMPV4:
839         return is_icmpv4(flow);
840     case MFP_ICMPV6:
841         return is_icmpv6(flow);
842
843     case MFP_ND:
844         return (is_icmpv6(flow)
845                 && flow->tp_dst == htons(0)
846                 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
847                     flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
848     case MFP_ND_SOLICIT:
849         return (is_icmpv6(flow)
850                 && flow->tp_dst == htons(0)
851                 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
852     case MFP_ND_ADVERT:
853         return (is_icmpv6(flow)
854                 && flow->tp_dst == htons(0)
855                 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
856     }
857
858     NOT_REACHED();
859 }
860
861 /* Returns true if 'value' may be a valid value *as part of a masked match*,
862  * false otherwise.
863  *
864  * A value is not rejected just because it is not valid for the field in
865  * question, but only if it doesn't make sense to test the bits in question at
866  * all.  For example, the MFF_VLAN_TCI field will never have a nonzero value
867  * without the VLAN_CFI bit being set, but we can't reject those values because
868  * it is still legitimate to test just for those bits (see the documentation
869  * for NXM_OF_VLAN_TCI in nicira-ext.h).  On the other hand, there is never a
870  * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
871 bool
872 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
873 {
874     switch (mf->id) {
875     case MFF_TUN_ID:
876     case MFF_IN_PORT:
877 #if FLOW_N_REGS > 0
878     case MFF_REG0:
879 #endif
880 #if FLOW_N_REGS > 1
881     case MFF_REG1:
882 #endif
883 #if FLOW_N_REGS > 2
884     case MFF_REG2:
885 #endif
886 #if FLOW_N_REGS > 3
887     case MFF_REG3:
888 #endif
889 #if FLOW_N_REGS > 4
890     case MFF_REG4:
891 #endif
892 #if FLOW_N_REGS > 5
893     case MFF_REG5:
894 #endif
895 #if FLOW_N_REGS > 6
896     case MFF_REG6:
897 #endif
898 #if FLOW_N_REGS > 7
899     case MFF_REG7:
900 #endif
901 #if FLOW_N_REGS > 8
902 #error
903 #endif
904     case MFF_ETH_SRC:
905     case MFF_ETH_DST:
906     case MFF_ETH_TYPE:
907     case MFF_VLAN_TCI:
908     case MFF_IPV4_SRC:
909     case MFF_IPV4_DST:
910     case MFF_IPV6_SRC:
911     case MFF_IPV6_DST:
912     case MFF_IP_PROTO:
913     case MFF_IP_TTL:
914     case MFF_ARP_SPA:
915     case MFF_ARP_TPA:
916     case MFF_ARP_SHA:
917     case MFF_ARP_THA:
918     case MFF_TCP_SRC:
919     case MFF_TCP_DST:
920     case MFF_UDP_SRC:
921     case MFF_UDP_DST:
922     case MFF_ICMPV4_TYPE:
923     case MFF_ICMPV4_CODE:
924     case MFF_ICMPV6_TYPE:
925     case MFF_ICMPV6_CODE:
926     case MFF_ND_TARGET:
927     case MFF_ND_SLL:
928     case MFF_ND_TLL:
929         return true;
930
931     case MFF_IP_DSCP:
932         return !(value->u8 & ~IP_DSCP_MASK);
933     case MFF_IP_ECN:
934         return !(value->u8 & ~IP_ECN_MASK);
935     case MFF_IP_FRAG:
936         return !(value->u8 & ~FLOW_NW_FRAG_MASK);
937
938     case MFF_ARP_OP:
939         return !(value->be16 & htons(0xff00));
940
941     case MFF_VLAN_VID:
942         return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
943
944     case MFF_VLAN_PCP:
945         return !(value->u8 & ~7);
946
947     case MFF_IPV6_LABEL:
948         return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
949
950     case MFF_N_IDS:
951     default:
952         NOT_REACHED();
953     }
954 }
955
956 /* Copies the value of field 'mf' from 'flow' into 'value'.  The caller is
957  * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
958 void
959 mf_get_value(const struct mf_field *mf, const struct flow *flow,
960              union mf_value *value)
961 {
962     switch (mf->id) {
963     case MFF_TUN_ID:
964         value->be64 = flow->tun_id;
965         break;
966
967     case MFF_IN_PORT:
968         value->be16 = htons(flow->in_port);
969         break;
970
971 #if FLOW_N_REGS > 0
972     case MFF_REG0:
973 #endif
974 #if FLOW_N_REGS > 1
975     case MFF_REG1:
976 #endif
977 #if FLOW_N_REGS > 2
978     case MFF_REG2:
979 #endif
980 #if FLOW_N_REGS > 3
981     case MFF_REG3:
982 #endif
983 #if FLOW_N_REGS > 4
984     case MFF_REG4:
985 #endif
986 #if FLOW_N_REGS > 5
987     case MFF_REG5:
988 #endif
989 #if FLOW_N_REGS > 6
990     case MFF_REG6:
991 #endif
992 #if FLOW_N_REGS > 7
993     case MFF_REG7:
994 #endif
995 #if FLOW_N_REGS > 8
996 #error
997 #endif
998         value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
999         break;
1000
1001     case MFF_ETH_SRC:
1002         memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
1003         break;
1004
1005     case MFF_ETH_DST:
1006         memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
1007         break;
1008
1009     case MFF_ETH_TYPE:
1010         value->be16 = flow->dl_type;
1011         break;
1012
1013     case MFF_VLAN_TCI:
1014         value->be16 = flow->vlan_tci;
1015         break;
1016
1017     case MFF_VLAN_VID:
1018         value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
1019         break;
1020
1021     case MFF_VLAN_PCP:
1022         value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
1023         break;
1024
1025     case MFF_IPV4_SRC:
1026         value->be32 = flow->nw_src;
1027         break;
1028
1029     case MFF_IPV4_DST:
1030         value->be32 = flow->nw_dst;
1031         break;
1032
1033     case MFF_IPV6_SRC:
1034         value->ipv6 = flow->ipv6_src;
1035         break;
1036
1037     case MFF_IPV6_DST:
1038         value->ipv6 = flow->ipv6_dst;
1039         break;
1040
1041     case MFF_IPV6_LABEL:
1042         value->be32 = flow->ipv6_label;
1043         break;
1044
1045     case MFF_IP_PROTO:
1046         value->u8 = flow->nw_proto;
1047         break;
1048
1049     case MFF_IP_DSCP:
1050         value->u8 = flow->nw_tos & IP_DSCP_MASK;
1051         break;
1052
1053     case MFF_IP_ECN:
1054         value->u8 = flow->nw_tos & IP_ECN_MASK;
1055         break;
1056
1057     case MFF_IP_TTL:
1058         value->u8 = flow->nw_ttl;
1059         break;
1060
1061     case MFF_IP_FRAG:
1062         value->u8 = flow->nw_frag;
1063         break;
1064
1065     case MFF_ARP_OP:
1066         value->be16 = htons(flow->nw_proto);
1067         break;
1068
1069     case MFF_ARP_SPA:
1070         value->be32 = flow->nw_src;
1071         break;
1072
1073     case MFF_ARP_TPA:
1074         value->be32 = flow->nw_dst;
1075         break;
1076
1077     case MFF_ARP_SHA:
1078     case MFF_ND_SLL:
1079         memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
1080         break;
1081
1082     case MFF_ARP_THA:
1083     case MFF_ND_TLL:
1084         memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
1085         break;
1086
1087     case MFF_TCP_SRC:
1088         value->be16 = flow->tp_src;
1089         break;
1090
1091     case MFF_TCP_DST:
1092         value->be16 = flow->tp_dst;
1093         break;
1094
1095     case MFF_UDP_SRC:
1096         value->be16 = flow->tp_src;
1097         break;
1098
1099     case MFF_UDP_DST:
1100         value->be16 = flow->tp_dst;
1101         break;
1102
1103     case MFF_ICMPV4_TYPE:
1104     case MFF_ICMPV6_TYPE:
1105         value->u8 = ntohs(flow->tp_src);
1106         break;
1107
1108     case MFF_ICMPV4_CODE:
1109     case MFF_ICMPV6_CODE:
1110         value->u8 = ntohs(flow->tp_dst);
1111         break;
1112
1113     case MFF_ND_TARGET:
1114         value->ipv6 = flow->nd_target;
1115         break;
1116
1117     case MFF_N_IDS:
1118     default:
1119         NOT_REACHED();
1120     }
1121 }
1122
1123 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
1124  * 'value'.  The caller is responsible for ensuring that 'rule' meets 'mf''s
1125  * prerequisites. */
1126 void
1127 mf_set_value(const struct mf_field *mf,
1128              const union mf_value *value, struct cls_rule *rule)
1129 {
1130     switch (mf->id) {
1131     case MFF_TUN_ID:
1132         cls_rule_set_tun_id(rule, value->be64);
1133         break;
1134
1135     case MFF_IN_PORT:
1136         cls_rule_set_in_port(rule, ntohs(value->be16));
1137         break;
1138
1139 #if FLOW_N_REGS > 0
1140     case MFF_REG0:
1141 #endif
1142 #if FLOW_N_REGS > 1
1143     case MFF_REG1:
1144 #endif
1145 #if FLOW_N_REGS > 2
1146     case MFF_REG2:
1147 #endif
1148 #if FLOW_N_REGS > 3
1149     case MFF_REG3:
1150 #endif
1151 #if FLOW_N_REGS > 4
1152     case MFF_REG4:
1153 #endif
1154 #if FLOW_N_REGS > 5
1155     case MFF_REG5:
1156 #endif
1157 #if FLOW_N_REGS > 6
1158     case MFF_REG6:
1159 #endif
1160 #if FLOW_N_REGS > 7
1161     case MFF_REG7:
1162 #endif
1163 #if FLOW_N_REGS > 8
1164 #error
1165 #endif
1166 #if FLOW_N_REGS > 0
1167         cls_rule_set_reg(rule, mf->id - MFF_REG0, ntohl(value->be32));
1168         break;
1169 #endif
1170
1171     case MFF_ETH_SRC:
1172         cls_rule_set_dl_src(rule, value->mac);
1173         break;
1174
1175     case MFF_ETH_DST:
1176         cls_rule_set_dl_dst(rule, value->mac);
1177         break;
1178
1179     case MFF_ETH_TYPE:
1180         cls_rule_set_dl_type(rule, value->be16);
1181         break;
1182
1183     case MFF_VLAN_TCI:
1184         cls_rule_set_dl_tci(rule, value->be16);
1185         break;
1186
1187     case MFF_VLAN_VID:
1188         cls_rule_set_dl_vlan(rule, value->be16);
1189         break;
1190
1191     case MFF_VLAN_PCP:
1192         cls_rule_set_dl_vlan_pcp(rule, value->u8);
1193         break;
1194
1195     case MFF_IPV4_SRC:
1196         cls_rule_set_nw_src(rule, value->be32);
1197         break;
1198
1199     case MFF_IPV4_DST:
1200         cls_rule_set_nw_dst(rule, value->be32);
1201         break;
1202
1203     case MFF_IPV6_SRC:
1204         cls_rule_set_ipv6_src(rule, &value->ipv6);
1205         break;
1206
1207     case MFF_IPV6_DST:
1208         cls_rule_set_ipv6_dst(rule, &value->ipv6);
1209         break;
1210
1211     case MFF_IPV6_LABEL:
1212         cls_rule_set_ipv6_label(rule, value->be32);
1213         break;
1214
1215     case MFF_IP_PROTO:
1216         cls_rule_set_nw_proto(rule, value->u8);
1217         break;
1218
1219     case MFF_IP_DSCP:
1220         cls_rule_set_nw_dscp(rule, value->u8);
1221         break;
1222
1223     case MFF_IP_ECN:
1224         cls_rule_set_nw_ecn(rule, value->u8);
1225         break;
1226
1227     case MFF_IP_TTL:
1228         cls_rule_set_nw_ttl(rule, value->u8);
1229         break;
1230
1231     case MFF_IP_FRAG:
1232         cls_rule_set_nw_frag(rule, value->u8);
1233         break;
1234
1235     case MFF_ARP_OP:
1236         cls_rule_set_nw_proto(rule, ntohs(value->be16));
1237         break;
1238
1239     case MFF_ARP_SPA:
1240         cls_rule_set_nw_src(rule, value->be32);
1241         break;
1242
1243     case MFF_ARP_TPA:
1244         cls_rule_set_nw_dst(rule, value->be32);
1245         break;
1246
1247     case MFF_ARP_SHA:
1248     case MFF_ND_SLL:
1249         cls_rule_set_arp_sha(rule, value->mac);
1250         break;
1251
1252     case MFF_ARP_THA:
1253     case MFF_ND_TLL:
1254         cls_rule_set_arp_tha(rule, value->mac);
1255         break;
1256
1257     case MFF_TCP_SRC:
1258         cls_rule_set_tp_src(rule, value->be16);
1259         break;
1260
1261     case MFF_TCP_DST:
1262         cls_rule_set_tp_dst(rule, value->be16);
1263         break;
1264
1265     case MFF_UDP_SRC:
1266         cls_rule_set_tp_src(rule, value->be16);
1267         break;
1268
1269     case MFF_UDP_DST:
1270         cls_rule_set_tp_dst(rule, value->be16);
1271         break;
1272
1273     case MFF_ICMPV4_TYPE:
1274     case MFF_ICMPV6_TYPE:
1275         cls_rule_set_icmp_type(rule, value->u8);
1276         break;
1277
1278     case MFF_ICMPV4_CODE:
1279     case MFF_ICMPV6_CODE:
1280         cls_rule_set_icmp_code(rule, value->u8);
1281         break;
1282
1283     case MFF_ND_TARGET:
1284         cls_rule_set_nd_target(rule, &value->ipv6);
1285         break;
1286
1287     case MFF_N_IDS:
1288     default:
1289         NOT_REACHED();
1290     }
1291 }
1292
1293 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
1294  * 'value'.  The caller is responsible for ensuring that 'rule' meets 'mf''s
1295  * prerequisites. */
1296 void
1297 mf_set_flow_value(const struct mf_field *mf,
1298                   const union mf_value *value, struct flow *flow)
1299 {
1300     switch (mf->id) {
1301     case MFF_TUN_ID:
1302         flow->tun_id = value->be64;
1303         break;
1304
1305     case MFF_IN_PORT:
1306         flow->in_port = ntohs(value->be16);
1307         break;
1308
1309 #if FLOW_N_REGS > 0
1310     case MFF_REG0:
1311 #endif
1312 #if FLOW_N_REGS > 1
1313     case MFF_REG1:
1314 #endif
1315 #if FLOW_N_REGS > 2
1316     case MFF_REG2:
1317 #endif
1318 #if FLOW_N_REGS > 3
1319     case MFF_REG3:
1320 #endif
1321 #if FLOW_N_REGS > 4
1322     case MFF_REG4:
1323 #endif
1324 #if FLOW_N_REGS > 5
1325     case MFF_REG5:
1326 #endif
1327 #if FLOW_N_REGS > 6
1328     case MFF_REG6:
1329 #endif
1330 #if FLOW_N_REGS > 7
1331     case MFF_REG7:
1332 #endif
1333 #if FLOW_N_REGS > 8
1334 #error
1335 #endif
1336 #if FLOW_N_REGS > 0
1337         flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1338         break;
1339 #endif
1340
1341     case MFF_ETH_SRC:
1342         memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1343         break;
1344
1345     case MFF_ETH_DST:
1346         memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
1347         break;
1348
1349     case MFF_ETH_TYPE:
1350         flow->dl_type = value->be16;
1351         break;
1352
1353     case MFF_VLAN_TCI:
1354         flow->vlan_tci = value->be16;
1355         break;
1356
1357     case MFF_VLAN_VID:
1358         flow_set_vlan_vid(flow, value->be16);
1359         break;
1360
1361     case MFF_VLAN_PCP:
1362         flow_set_vlan_pcp(flow, value->u8);
1363         break;
1364
1365     case MFF_IPV4_SRC:
1366         flow->nw_src = value->be32;
1367         break;
1368
1369     case MFF_IPV4_DST:
1370         flow->nw_dst = value->be32;
1371         break;
1372
1373     case MFF_IPV6_SRC:
1374         flow->ipv6_src = value->ipv6;
1375         break;
1376
1377     case MFF_IPV6_DST:
1378         flow->ipv6_dst = value->ipv6;
1379         break;
1380
1381     case MFF_IPV6_LABEL:
1382         flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1383         break;
1384
1385     case MFF_IP_PROTO:
1386         flow->nw_proto = value->u8;
1387         break;
1388
1389     case MFF_IP_DSCP:
1390         flow->nw_tos &= ~IP_DSCP_MASK;
1391         flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1392         break;
1393
1394     case MFF_IP_ECN:
1395         flow->nw_tos &= ~IP_ECN_MASK;
1396         flow->nw_tos |= value->u8 & IP_ECN_MASK;
1397         break;
1398
1399     case MFF_IP_TTL:
1400         flow->nw_ttl = value->u8;
1401         break;
1402
1403     case MFF_IP_FRAG:
1404         flow->nw_frag &= value->u8;
1405         break;
1406
1407     case MFF_ARP_OP:
1408         flow->nw_proto = ntohs(value->be16);
1409         break;
1410
1411     case MFF_ARP_SPA:
1412         flow->nw_src = value->be32;
1413         break;
1414
1415     case MFF_ARP_TPA:
1416         flow->nw_dst = value->be32;
1417         break;
1418
1419     case MFF_ARP_SHA:
1420     case MFF_ND_SLL:
1421         memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1422         break;
1423
1424     case MFF_ARP_THA:
1425     case MFF_ND_TLL:
1426         memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1427         break;
1428
1429     case MFF_TCP_SRC:
1430     case MFF_UDP_SRC:
1431         flow->tp_src = value->be16;
1432         break;
1433
1434     case MFF_TCP_DST:
1435     case MFF_UDP_DST:
1436         flow->tp_dst = value->be16;
1437         break;
1438
1439     case MFF_ICMPV4_TYPE:
1440     case MFF_ICMPV6_TYPE:
1441         flow->tp_src = htons(value->u8);
1442         break;
1443
1444     case MFF_ICMPV4_CODE:
1445     case MFF_ICMPV6_CODE:
1446         flow->tp_dst = htons(value->u8);
1447         break;
1448
1449     case MFF_ND_TARGET:
1450         flow->nd_target = value->ipv6;
1451         break;
1452
1453     case MFF_N_IDS:
1454     default:
1455         NOT_REACHED();
1456     }
1457 }
1458
1459 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1460  *
1461  * The caller is responsible for ensuring that 'flow' meets 'mf''s
1462  * prerequisites. */
1463 bool
1464 mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1465 {
1466     union mf_value value;
1467
1468     mf_get_value(mf, flow, &value);
1469     return is_all_zeros((const uint8_t *) &value, mf->n_bytes);
1470 }
1471
1472 /* Makes 'rule' wildcard field 'mf'.
1473  *
1474  * The caller is responsible for ensuring that 'rule' meets 'mf''s
1475  * prerequisites. */
1476 void
1477 mf_set_wild(const struct mf_field *mf, struct cls_rule *rule)
1478 {
1479     switch (mf->id) {
1480     case MFF_TUN_ID:
1481         cls_rule_set_tun_id_masked(rule, htonll(0), htonll(0));
1482         break;
1483
1484     case MFF_IN_PORT:
1485         rule->wc.wildcards |= FWW_IN_PORT;
1486         rule->flow.in_port = 0;
1487         break;
1488
1489 #if FLOW_N_REGS > 0
1490     case MFF_REG0:
1491         cls_rule_set_reg_masked(rule, 0, 0, 0);
1492         break;
1493 #endif
1494 #if FLOW_N_REGS > 1
1495     case MFF_REG1:
1496         cls_rule_set_reg_masked(rule, 1, 0, 0);
1497         break;
1498 #endif
1499 #if FLOW_N_REGS > 2
1500     case MFF_REG2:
1501         cls_rule_set_reg_masked(rule, 2, 0, 0);
1502         break;
1503 #endif
1504 #if FLOW_N_REGS > 3
1505     case MFF_REG3:
1506         cls_rule_set_reg_masked(rule, 3, 0, 0);
1507         break;
1508 #endif
1509 #if FLOW_N_REGS > 4
1510     case MFF_REG4:
1511         cls_rule_set_reg_masked(rule, 4, 0, 0);
1512         break;
1513 #endif
1514 #if FLOW_N_REGS > 5
1515     case MFF_REG5:
1516         cls_rule_set_reg_masked(rule, 5, 0, 0);
1517         break;
1518 #endif
1519 #if FLOW_N_REGS > 6
1520     case MFF_REG6:
1521         cls_rule_set_reg_masked(rule, 6, 0, 0);
1522         break;
1523 #endif
1524 #if FLOW_N_REGS > 7
1525     case MFF_REG7:
1526         cls_rule_set_reg_masked(rule, 7, 0, 0);
1527         break;
1528 #endif
1529 #if FLOW_N_REGS > 8
1530 #error
1531 #endif
1532
1533     case MFF_ETH_SRC:
1534         memset(rule->flow.dl_src, 0, ETH_ADDR_LEN);
1535         memset(rule->wc.dl_src_mask, 0, ETH_ADDR_LEN);
1536         break;
1537
1538     case MFF_ETH_DST:
1539         memset(rule->flow.dl_dst, 0, ETH_ADDR_LEN);
1540         memset(rule->wc.dl_dst_mask, 0, ETH_ADDR_LEN);
1541         break;
1542
1543     case MFF_ETH_TYPE:
1544         rule->wc.wildcards |= FWW_DL_TYPE;
1545         rule->flow.dl_type = htons(0);
1546         break;
1547
1548     case MFF_VLAN_TCI:
1549         cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
1550         break;
1551
1552     case MFF_VLAN_VID:
1553         cls_rule_set_any_vid(rule);
1554         break;
1555
1556     case MFF_VLAN_PCP:
1557         cls_rule_set_any_pcp(rule);
1558         break;
1559
1560     case MFF_IPV4_SRC:
1561     case MFF_ARP_SPA:
1562         cls_rule_set_nw_src_masked(rule, htonl(0), htonl(0));
1563         break;
1564
1565     case MFF_IPV4_DST:
1566     case MFF_ARP_TPA:
1567         cls_rule_set_nw_dst_masked(rule, htonl(0), htonl(0));
1568         break;
1569
1570     case MFF_IPV6_SRC:
1571         memset(&rule->wc.ipv6_src_mask, 0, sizeof rule->wc.ipv6_src_mask);
1572         memset(&rule->flow.ipv6_src, 0, sizeof rule->flow.ipv6_src);
1573         break;
1574
1575     case MFF_IPV6_DST:
1576         memset(&rule->wc.ipv6_dst_mask, 0, sizeof rule->wc.ipv6_dst_mask);
1577         memset(&rule->flow.ipv6_dst, 0, sizeof rule->flow.ipv6_dst);
1578         break;
1579
1580     case MFF_IPV6_LABEL:
1581         rule->wc.wildcards |= FWW_IPV6_LABEL;
1582         rule->flow.ipv6_label = 0;
1583         break;
1584
1585     case MFF_IP_PROTO:
1586         rule->wc.wildcards |= FWW_NW_PROTO;
1587         rule->flow.nw_proto = 0;
1588         break;
1589
1590     case MFF_IP_DSCP:
1591         rule->wc.wildcards |= FWW_NW_DSCP;
1592         rule->flow.nw_tos &= ~IP_DSCP_MASK;
1593         break;
1594
1595     case MFF_IP_ECN:
1596         rule->wc.wildcards |= FWW_NW_ECN;
1597         rule->flow.nw_tos &= ~IP_ECN_MASK;
1598         break;
1599
1600     case MFF_IP_TTL:
1601         rule->wc.wildcards |= FWW_NW_TTL;
1602         rule->flow.nw_ttl = 0;
1603         break;
1604
1605     case MFF_IP_FRAG:
1606         rule->wc.nw_frag_mask |= FLOW_NW_FRAG_MASK;
1607         rule->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1608         break;
1609
1610     case MFF_ARP_OP:
1611         rule->wc.wildcards |= FWW_NW_PROTO;
1612         rule->flow.nw_proto = 0;
1613         break;
1614
1615     case MFF_ARP_SHA:
1616     case MFF_ND_SLL:
1617         rule->wc.wildcards |= FWW_ARP_SHA;
1618         memset(rule->flow.arp_sha, 0, sizeof rule->flow.arp_sha);
1619         break;
1620
1621     case MFF_ARP_THA:
1622     case MFF_ND_TLL:
1623         rule->wc.wildcards |= FWW_ARP_THA;
1624         memset(rule->flow.arp_tha, 0, sizeof rule->flow.arp_tha);
1625         break;
1626
1627     case MFF_TCP_SRC:
1628     case MFF_UDP_SRC:
1629     case MFF_ICMPV4_TYPE:
1630     case MFF_ICMPV6_TYPE:
1631         rule->wc.tp_src_mask = htons(0);
1632         rule->flow.tp_src = htons(0);
1633         break;
1634
1635     case MFF_TCP_DST:
1636     case MFF_UDP_DST:
1637     case MFF_ICMPV4_CODE:
1638     case MFF_ICMPV6_CODE:
1639         rule->wc.tp_dst_mask = htons(0);
1640         rule->flow.tp_dst = htons(0);
1641         break;
1642
1643     case MFF_ND_TARGET:
1644         memset(&rule->wc.nd_target_mask, 0, sizeof rule->wc.nd_target_mask);
1645         memset(&rule->flow.nd_target, 0, sizeof rule->flow.nd_target);
1646         break;
1647
1648     case MFF_N_IDS:
1649     default:
1650         NOT_REACHED();
1651     }
1652 }
1653
1654 /* Makes 'rule' match field 'mf' with the specified 'value' and 'mask'.
1655  * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1656  * with a 1-bit indicating that the corresponding value bit must match and a
1657  * 0-bit indicating a don't-care.
1658  *
1659  * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1660  * mf_set_value(mf, value, rule).  If 'mask' points to all-0-bits, then this
1661  * call is equivalent to mf_set_wild(mf, rule).
1662  *
1663  * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()).  The caller
1664  * is responsible for ensuring that 'rule' meets 'mf''s prerequisites. */
1665 void
1666 mf_set(const struct mf_field *mf,
1667        const union mf_value *value, const union mf_value *mask,
1668        struct cls_rule *rule)
1669 {
1670     if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1671         mf_set_value(mf, value, rule);
1672         return;
1673     } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1674         mf_set_wild(mf, rule);
1675         return;
1676     }
1677
1678     switch (mf->id) {
1679     case MFF_IN_PORT:
1680     case MFF_ETH_TYPE:
1681     case MFF_VLAN_VID:
1682     case MFF_VLAN_PCP:
1683     case MFF_IPV6_LABEL:
1684     case MFF_IP_PROTO:
1685     case MFF_IP_TTL:
1686     case MFF_IP_DSCP:
1687     case MFF_IP_ECN:
1688     case MFF_ARP_OP:
1689     case MFF_ARP_SHA:
1690     case MFF_ARP_THA:
1691     case MFF_ICMPV4_TYPE:
1692     case MFF_ICMPV4_CODE:
1693     case MFF_ICMPV6_TYPE:
1694     case MFF_ICMPV6_CODE:
1695     case MFF_ND_SLL:
1696     case MFF_ND_TLL:
1697         NOT_REACHED();
1698
1699     case MFF_TUN_ID:
1700         cls_rule_set_tun_id_masked(rule, value->be64, mask->be64);
1701         break;
1702
1703 #if FLOW_N_REGS > 0
1704     case MFF_REG0:
1705 #endif
1706 #if FLOW_N_REGS > 1
1707     case MFF_REG1:
1708 #endif
1709 #if FLOW_N_REGS > 2
1710     case MFF_REG2:
1711 #endif
1712 #if FLOW_N_REGS > 3
1713     case MFF_REG3:
1714 #endif
1715 #if FLOW_N_REGS > 4
1716     case MFF_REG4:
1717 #endif
1718 #if FLOW_N_REGS > 5
1719     case MFF_REG5:
1720 #endif
1721 #if FLOW_N_REGS > 6
1722     case MFF_REG6:
1723 #endif
1724 #if FLOW_N_REGS > 7
1725     case MFF_REG7:
1726 #endif
1727 #if FLOW_N_REGS > 8
1728 #error
1729 #endif
1730         cls_rule_set_reg_masked(rule, mf->id - MFF_REG0,
1731                                 ntohl(value->be32), ntohl(mask->be32));
1732         break;
1733
1734     case MFF_ETH_DST:
1735         cls_rule_set_dl_dst_masked(rule, value->mac, mask->mac);
1736         break;
1737
1738     case MFF_ETH_SRC:
1739         cls_rule_set_dl_src_masked(rule, value->mac, mask->mac);
1740         break;
1741
1742     case MFF_VLAN_TCI:
1743         cls_rule_set_dl_tci_masked(rule, value->be16, mask->be16);
1744         break;
1745
1746     case MFF_IPV4_SRC:
1747         cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1748         break;
1749
1750     case MFF_IPV4_DST:
1751         cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1752         break;
1753
1754     case MFF_IPV6_SRC:
1755         cls_rule_set_ipv6_src_masked(rule, &value->ipv6, &mask->ipv6);
1756         break;
1757
1758     case MFF_IPV6_DST:
1759         cls_rule_set_ipv6_dst_masked(rule, &value->ipv6, &mask->ipv6);
1760         break;
1761
1762     case MFF_ND_TARGET:
1763         cls_rule_set_nd_target_masked(rule, &value->ipv6, &mask->ipv6);
1764         break;
1765
1766     case MFF_IP_FRAG:
1767         cls_rule_set_nw_frag_masked(rule, value->u8, mask->u8);
1768         break;
1769
1770     case MFF_ARP_SPA:
1771         cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1772         break;
1773
1774     case MFF_ARP_TPA:
1775         cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1776         break;
1777
1778     case MFF_TCP_SRC:
1779     case MFF_UDP_SRC:
1780         cls_rule_set_tp_src_masked(rule, value->be16, mask->be16);
1781         break;
1782
1783     case MFF_TCP_DST:
1784     case MFF_UDP_DST:
1785         cls_rule_set_tp_dst_masked(rule, value->be16, mask->be16);
1786         break;
1787
1788     case MFF_N_IDS:
1789     default:
1790         NOT_REACHED();
1791     }
1792 }
1793
1794 static enum ofperr
1795 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1796            const char *type)
1797 {
1798     if (!sf->field) {
1799         VLOG_WARN_RL(&rl, "unknown %s field", type);
1800     } else if (!sf->n_bits) {
1801         VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1802     } else if (sf->ofs >= sf->field->n_bits) {
1803         VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1804                      sf->ofs, sf->field->n_bits, type, sf->field->name);
1805     } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1806         VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1807                      "of %s field %s", sf->ofs, sf->n_bits,
1808                      sf->field->n_bits, type, sf->field->name);
1809     } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1810         VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1811                      type, sf->field->name);
1812     } else {
1813         return 0;
1814     }
1815
1816     return OFPERR_OFPBAC_BAD_ARGUMENT;
1817 }
1818
1819 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'.  Returns
1820  * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1821  * ofp_mkerr()).  */
1822 enum ofperr
1823 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1824 {
1825     return mf_check__(sf, flow, "source");
1826 }
1827
1828 /* Checks whether 'sf' is valid for writing a subfield into 'flow'.  Returns 0
1829  * if so, otherwise an OpenFlow error code (e.g. as returned by
1830  * ofp_mkerr()). */
1831 enum ofperr
1832 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1833 {
1834     int error = mf_check__(sf, flow, "destination");
1835     if (!error && !sf->field->writable) {
1836         VLOG_WARN_RL(&rl, "destination field %s is not writable",
1837                      sf->field->name);
1838         return OFPERR_OFPBAC_BAD_ARGUMENT;
1839     }
1840     return error;
1841 }
1842
1843 /* Copies the value and wildcard bit pattern for 'mf' from 'rule' into the
1844  * 'value' and 'mask', respectively. */
1845 void
1846 mf_get(const struct mf_field *mf, const struct cls_rule *rule,
1847        union mf_value *value, union mf_value *mask)
1848 {
1849     mf_get_value(mf, &rule->flow, value);
1850     mf_get_mask(mf, &rule->wc, mask);
1851 }
1852
1853 /* Assigns a random value for field 'mf' to 'value'. */
1854 void
1855 mf_random_value(const struct mf_field *mf, union mf_value *value)
1856 {
1857     random_bytes(value, mf->n_bytes);
1858
1859     switch (mf->id) {
1860     case MFF_TUN_ID:
1861     case MFF_IN_PORT:
1862 #if FLOW_N_REGS > 0
1863     case MFF_REG0:
1864 #endif
1865 #if FLOW_N_REGS > 1
1866     case MFF_REG1:
1867 #endif
1868 #if FLOW_N_REGS > 2
1869     case MFF_REG2:
1870 #endif
1871 #if FLOW_N_REGS > 3
1872     case MFF_REG3:
1873 #endif
1874 #if FLOW_N_REGS > 4
1875     case MFF_REG4:
1876 #endif
1877 #if FLOW_N_REGS > 5
1878     case MFF_REG5:
1879 #endif
1880 #if FLOW_N_REGS > 6
1881     case MFF_REG6:
1882 #endif
1883 #if FLOW_N_REGS > 7
1884     case MFF_REG7:
1885 #endif
1886 #if FLOW_N_REGS > 8
1887 #error
1888 #endif
1889     case MFF_ETH_SRC:
1890     case MFF_ETH_DST:
1891     case MFF_ETH_TYPE:
1892     case MFF_VLAN_TCI:
1893     case MFF_IPV4_SRC:
1894     case MFF_IPV4_DST:
1895     case MFF_IPV6_SRC:
1896     case MFF_IPV6_DST:
1897     case MFF_IP_PROTO:
1898     case MFF_IP_TTL:
1899     case MFF_ARP_SPA:
1900     case MFF_ARP_TPA:
1901     case MFF_ARP_SHA:
1902     case MFF_ARP_THA:
1903     case MFF_TCP_SRC:
1904     case MFF_TCP_DST:
1905     case MFF_UDP_SRC:
1906     case MFF_UDP_DST:
1907     case MFF_ICMPV4_TYPE:
1908     case MFF_ICMPV4_CODE:
1909     case MFF_ICMPV6_TYPE:
1910     case MFF_ICMPV6_CODE:
1911     case MFF_ND_TARGET:
1912     case MFF_ND_SLL:
1913     case MFF_ND_TLL:
1914         break;
1915
1916     case MFF_IPV6_LABEL:
1917         value->be32 &= ~htonl(IPV6_LABEL_MASK);
1918         break;
1919
1920     case MFF_IP_DSCP:
1921         value->u8 &= IP_DSCP_MASK;
1922         break;
1923
1924     case MFF_IP_ECN:
1925         value->u8 &= IP_ECN_MASK;
1926         break;
1927
1928     case MFF_IP_FRAG:
1929         value->u8 &= FLOW_NW_FRAG_MASK;
1930         break;
1931
1932     case MFF_ARP_OP:
1933         value->be16 &= htons(0xff);
1934         break;
1935
1936     case MFF_VLAN_VID:
1937         value->be16 &= htons(VLAN_VID_MASK);
1938         break;
1939
1940     case MFF_VLAN_PCP:
1941         value->u8 &= 0x07;
1942         break;
1943
1944     case MFF_N_IDS:
1945     default:
1946         NOT_REACHED();
1947     }
1948 }
1949
1950 static char *
1951 mf_from_integer_string(const struct mf_field *mf, const char *s,
1952                        uint8_t *valuep, uint8_t *maskp)
1953 {
1954     unsigned long long int integer, mask;
1955     char *tail;
1956     int i;
1957
1958     errno = 0;
1959     integer = strtoull(s, &tail, 0);
1960     if (errno || (*tail != '\0' && *tail != '/')) {
1961         goto syntax_error;
1962     }
1963
1964     if (*tail == '/') {
1965         mask = strtoull(tail + 1, &tail, 0);
1966         if (errno || *tail != '\0') {
1967             goto syntax_error;
1968         }
1969     } else {
1970         mask = ULLONG_MAX;
1971     }
1972
1973     for (i = mf->n_bytes - 1; i >= 0; i--) {
1974         valuep[i] = integer;
1975         maskp[i] = mask;
1976         integer >>= 8;
1977         mask >>= 8;
1978     }
1979     if (integer) {
1980         return xasprintf("%s: value too large for %u-byte field %s",
1981                          s, mf->n_bytes, mf->name);
1982     }
1983     return NULL;
1984
1985 syntax_error:
1986     return xasprintf("%s: bad syntax for %s", s, mf->name);
1987 }
1988
1989 static char *
1990 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1991                         uint8_t mac[ETH_ADDR_LEN],
1992                         uint8_t mask[ETH_ADDR_LEN])
1993 {
1994     assert(mf->n_bytes == ETH_ADDR_LEN);
1995
1996     switch (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
1997                    ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))){
1998     case ETH_ADDR_SCAN_COUNT * 2:
1999         return NULL;
2000
2001     case ETH_ADDR_SCAN_COUNT:
2002         memset(mask, 0xff, ETH_ADDR_LEN);
2003         return NULL;
2004
2005     default:
2006         return xasprintf("%s: invalid Ethernet address", s);
2007     }
2008 }
2009
2010 static char *
2011 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
2012                     ovs_be32 *ip, ovs_be32 *mask)
2013 {
2014     int prefix;
2015
2016     assert(mf->n_bytes == sizeof *ip);
2017
2018     if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
2019                IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
2020         /* OK. */
2021     } else if (sscanf(s, IP_SCAN_FMT"/%d",
2022                       IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
2023         if (prefix <= 0 || prefix > 32) {
2024             return xasprintf("%s: network prefix bits not between 1 and "
2025                              "32", s);
2026         } else if (prefix == 32) {
2027             *mask = htonl(UINT32_MAX);
2028         } else {
2029             *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
2030         }
2031     } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
2032         *mask = htonl(UINT32_MAX);
2033     } else {
2034         return xasprintf("%s: invalid IP address", s);
2035     }
2036     return NULL;
2037 }
2038
2039 static char *
2040 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
2041                     struct in6_addr *value, struct in6_addr *mask)
2042 {
2043     char *str = xstrdup(s);
2044     char *save_ptr = NULL;
2045     const char *name, *netmask;
2046     int retval;
2047
2048     assert(mf->n_bytes == sizeof *value);
2049
2050     name = strtok_r(str, "/", &save_ptr);
2051     retval = name ? lookup_ipv6(name, value) : EINVAL;
2052     if (retval) {
2053         char *err;
2054
2055         err = xasprintf("%s: could not convert to IPv6 address", str);
2056         free(str);
2057
2058         return err;
2059     }
2060
2061     netmask = strtok_r(NULL, "/", &save_ptr);
2062     if (netmask) {
2063         int prefix = atoi(netmask);
2064         if (prefix <= 0 || prefix > 128) {
2065             free(str);
2066             return xasprintf("%s: prefix bits not between 1 and 128", s);
2067         } else {
2068             *mask = ipv6_create_mask(prefix);
2069         }
2070     } else {
2071         *mask = in6addr_exact;
2072     }
2073     free(str);
2074
2075     return NULL;
2076 }
2077
2078 static char *
2079 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
2080                         ovs_be16 *valuep, ovs_be16 *maskp)
2081 {
2082     uint16_t port;
2083
2084     assert(mf->n_bytes == sizeof(ovs_be16));
2085     if (ofputil_port_from_string(s, &port)) {
2086         *valuep = htons(port);
2087         *maskp = htons(UINT16_MAX);
2088         return NULL;
2089     } else {
2090         return mf_from_integer_string(mf, s,
2091                                       (uint8_t *) valuep, (uint8_t *) maskp);
2092     }
2093 }
2094
2095 struct frag_handling {
2096     const char *name;
2097     uint8_t mask;
2098     uint8_t value;
2099 };
2100
2101 static const struct frag_handling all_frags[] = {
2102 #define A FLOW_NW_FRAG_ANY
2103 #define L FLOW_NW_FRAG_LATER
2104     /* name               mask  value */
2105
2106     { "no",               A|L,  0     },
2107     { "first",            A|L,  A     },
2108     { "later",            A|L,  A|L   },
2109
2110     { "no",               A,    0     },
2111     { "yes",              A,    A     },
2112
2113     { "not_later",        L,    0     },
2114     { "later",            L,    L     },
2115 #undef A
2116 #undef L
2117 };
2118
2119 static char *
2120 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2121 {
2122     const struct frag_handling *h;
2123
2124     for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2125         if (!strcasecmp(s, h->name)) {
2126             /* We force the upper bits of the mask on to make mf_parse_value()
2127              * happy (otherwise it will never think it's an exact match.) */
2128             *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
2129             *valuep = h->value;
2130             return NULL;
2131         }
2132     }
2133
2134     return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2135                      "\"yes\", \"first\", \"later\", \"not_first\"", s);
2136 }
2137
2138 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'.  Returns
2139  * NULL if successful, otherwise a malloc()'d string describing the error. */
2140 char *
2141 mf_parse(const struct mf_field *mf, const char *s,
2142          union mf_value *value, union mf_value *mask)
2143 {
2144     if (!strcasecmp(s, "any") || !strcmp(s, "*")) {
2145         memset(value, 0, mf->n_bytes);
2146         memset(mask, 0, mf->n_bytes);
2147         return NULL;
2148     }
2149
2150     switch (mf->string) {
2151     case MFS_DECIMAL:
2152     case MFS_HEXADECIMAL:
2153         return mf_from_integer_string(mf, s,
2154                                       (uint8_t *) value, (uint8_t *) mask);
2155
2156     case MFS_ETHERNET:
2157         return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
2158
2159     case MFS_IPV4:
2160         return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2161
2162     case MFS_IPV6:
2163         return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2164
2165     case MFS_OFP_PORT:
2166         return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2167
2168     case MFS_FRAG:
2169         return mf_from_frag_string(s, &value->u8, &mask->u8);
2170     }
2171     NOT_REACHED();
2172 }
2173
2174 /* Parses 's', a string value for field 'mf', into 'value'.  Returns NULL if
2175  * successful, otherwise a malloc()'d string describing the error. */
2176 char *
2177 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2178 {
2179     union mf_value mask;
2180     char *error;
2181
2182     error = mf_parse(mf, s, value, &mask);
2183     if (error) {
2184         return error;
2185     }
2186
2187     if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2188         return xasprintf("%s: wildcards not allowed here", s);
2189     }
2190     return NULL;
2191 }
2192
2193 static void
2194 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2195                          const uint8_t *maskp, struct ds *s)
2196 {
2197     unsigned long long int integer;
2198     int i;
2199
2200     assert(mf->n_bytes <= 8);
2201
2202     integer = 0;
2203     for (i = 0; i < mf->n_bytes; i++) {
2204         integer = (integer << 8) | valuep[i];
2205     }
2206     if (mf->string == MFS_HEXADECIMAL) {
2207         ds_put_format(s, "%#llx", integer);
2208     } else {
2209         ds_put_format(s, "%lld", integer);
2210     }
2211
2212     if (maskp) {
2213         unsigned long long int mask;
2214
2215         mask = 0;
2216         for (i = 0; i < mf->n_bytes; i++) {
2217             mask = (mask << 8) | maskp[i];
2218         }
2219
2220         /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2221          * not sure that that a bit-mask written in decimal is ever easier to
2222          * understand than the same bit-mask written in hexadecimal. */
2223         ds_put_format(s, "/%#llx", mask);
2224     }
2225 }
2226
2227 static void
2228 mf_format_frag_string(const uint8_t *valuep, const uint8_t *maskp,
2229                       struct ds *s)
2230 {
2231     const struct frag_handling *h;
2232     uint8_t value = *valuep;
2233     uint8_t mask = *maskp;
2234
2235     value &= mask;
2236     mask &= FLOW_NW_FRAG_MASK;
2237
2238     for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2239         if (value == h->value && mask == h->mask) {
2240             ds_put_cstr(s, h->name);
2241             return;
2242         }
2243     }
2244     ds_put_cstr(s, "<error>");
2245 }
2246
2247 /* Appends to 's' a string representation of field 'mf' whose value is in
2248  * 'value' and 'mask'.  'mask' may be NULL to indicate an exact match. */
2249 void
2250 mf_format(const struct mf_field *mf,
2251           const union mf_value *value, const union mf_value *mask,
2252           struct ds *s)
2253 {
2254     if (mask) {
2255         if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
2256             ds_put_cstr(s, "ANY");
2257             return;
2258         } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
2259             mask = NULL;
2260         }
2261     }
2262
2263     switch (mf->string) {
2264     case MFS_OFP_PORT:
2265         if (!mask) {
2266             ofputil_format_port(ntohs(value->be16), s);
2267             break;
2268         }
2269         /* fall through */
2270     case MFS_DECIMAL:
2271     case MFS_HEXADECIMAL:
2272         mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2273         break;
2274
2275     case MFS_ETHERNET:
2276         eth_format_masked(value->mac, mask->mac, s);
2277         break;
2278
2279     case MFS_IPV4:
2280         ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
2281                          s);
2282         break;
2283
2284     case MFS_IPV6:
2285         print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2286         break;
2287
2288     case MFS_FRAG:
2289         mf_format_frag_string(&value->u8, &mask->u8, s);
2290         break;
2291
2292     default:
2293         NOT_REACHED();
2294     }
2295 }
2296 \f
2297 /* Makes subfield 'sf' within 'rule' exactly match the 'sf->n_bits'
2298  * least-significant bits in 'x'.
2299  *
2300  * See mf_set_subfield() for an example.
2301  *
2302  * The difference between this function and mf_set_subfield() is that the
2303  * latter function can only handle subfields up to 64 bits wide, whereas this
2304  * one handles the general case.  On the other hand, mf_set_subfield() is
2305  * arguably easier to use. */
2306 void
2307 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2308                   struct cls_rule *rule)
2309 {
2310     const struct mf_field *field = sf->field;
2311     union mf_value value, mask;
2312
2313     mf_get(field, rule, &value, &mask);
2314     bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2315     bitwise_one (                 &mask,  field->n_bytes, sf->ofs, sf->n_bits);
2316     mf_set(field, &value, &mask, rule);
2317 }
2318
2319 /* Makes subfield 'sf' within 'rule' exactly match the 'sf->n_bits'
2320  * least-significant bits of 'x'.
2321  *
2322  * Example: suppose that 'sf->field' is originally the following 2-byte field
2323  * in 'rule':
2324  *
2325  *     value == 0xe00a == 2#1110000000001010
2326  *      mask == 0xfc3f == 2#1111110000111111
2327  *
2328  * The call mf_set_subfield(sf, 0x55, 8, 7, rule), where sf->ofs == 8 and
2329  * sf->n_bits == 7 would have the following effect (note that 0x55 is
2330  * 2#1010101):
2331  *
2332  *     value == 0xd50a == 2#1101010100001010
2333  *      mask == 0xff3f == 2#1111111100111111
2334  *                           ^^^^^^^ affected bits
2335  *
2336  * The caller is responsible for ensuring that the result will be a valid
2337  * wildcard pattern for 'sf->field'.  The caller is responsible for ensuring
2338  * that 'rule' meets 'sf->field''s prerequisites. */
2339 void
2340 mf_set_subfield(const struct mf_subfield *sf, uint64_t x,
2341                 struct cls_rule *rule)
2342 {
2343     const struct mf_field *field = sf->field;
2344     unsigned int n_bits = sf->n_bits;
2345     unsigned int ofs = sf->ofs;
2346
2347     if (ofs == 0 && field->n_bytes * 8 == n_bits) {
2348         union mf_value value;
2349         int i;
2350
2351         for (i = field->n_bytes - 1; i >= 0; i--) {
2352             ((uint8_t *) &value)[i] = x;
2353             x >>= 8;
2354         }
2355         mf_set_value(field, &value, rule);
2356     } else {
2357         union mf_value value, mask;
2358         uint8_t *vp = (uint8_t *) &value;
2359         uint8_t *mp = (uint8_t *) &mask;
2360
2361         mf_get(field, rule, &value, &mask);
2362         bitwise_put(x,          vp, field->n_bytes, ofs, n_bits);
2363         bitwise_put(UINT64_MAX, mp, field->n_bytes, ofs, n_bits);
2364         mf_set(field, &value, &mask, rule);
2365     }
2366 }
2367
2368 /* Similar to mf_set_subfield() but modifies only a flow, not a cls_rule. */
2369 void
2370 mf_set_subfield_value(const struct mf_subfield *sf, uint64_t x,
2371                       struct flow *flow)
2372 {
2373     const struct mf_field *field = sf->field;
2374     unsigned int n_bits = sf->n_bits;
2375     unsigned int ofs = sf->ofs;
2376     union mf_value value;
2377
2378     if (ofs == 0 && field->n_bytes * 8 == n_bits) {
2379         int i;
2380
2381         for (i = field->n_bytes - 1; i >= 0; i--) {
2382             ((uint8_t *) &value)[i] = x;
2383             x >>= 8;
2384         }
2385         mf_set_flow_value(field, &value, flow);
2386     } else {
2387         mf_get_value(field, flow, &value);
2388         bitwise_put(x, &value, field->n_bytes, ofs, n_bits);
2389         mf_set_flow_value(field, &value, flow);
2390     }
2391 }
2392
2393 /* Initializes 'x' to the value of 'sf' within 'flow'.  'sf' must be valid for
2394  * reading 'flow', e.g. as checked by mf_check_src(). */
2395 void
2396 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2397                  union mf_subvalue *x)
2398 {
2399     union mf_value value;
2400
2401     mf_get_value(sf->field, flow, &value);
2402
2403     memset(x, 0, sizeof *x);
2404     bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2405                  x, sizeof *x, 0,
2406                  sf->n_bits);
2407 }
2408
2409 /* Returns the value of 'sf' within 'flow'.  'sf' must be valid for reading
2410  * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2411  * less. */
2412 uint64_t
2413 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2414 {
2415     union mf_value value;
2416
2417     mf_get_value(sf->field, flow, &value);
2418     return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2419 }
2420
2421 /* Formats 'sf' into 's' in a format normally acceptable to
2422  * mf_parse_subfield().  (It won't be acceptable if sf->field is NULL or if
2423  * sf->field has no NXM name.) */
2424 void
2425 mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
2426 {
2427     if (!sf->field) {
2428         ds_put_cstr(s, "<unknown>");
2429     } else if (sf->field->nxm_name) {
2430         ds_put_cstr(s, sf->field->nxm_name);
2431     } else if (sf->field->nxm_header) {
2432         uint32_t header = sf->field->nxm_header;
2433         ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
2434     } else {
2435         ds_put_cstr(s, sf->field->name);
2436     }
2437
2438     if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
2439         ds_put_cstr(s, "[]");
2440     } else if (sf->n_bits == 1) {
2441         ds_put_format(s, "[%d]", sf->ofs);
2442     } else {
2443         ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
2444     }
2445 }
2446
2447 static const struct mf_field *
2448 mf_parse_subfield_name(const char *name, int name_len, bool *wild)
2449 {
2450     int i;
2451
2452     *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
2453     if (*wild) {
2454         name_len -= 2;
2455     }
2456
2457     for (i = 0; i < MFF_N_IDS; i++) {
2458         const struct mf_field *mf = mf_from_id(i);
2459
2460         if (mf->nxm_name
2461             && !strncmp(mf->nxm_name, name, name_len)
2462             && mf->nxm_name[name_len] == '\0') {
2463             return mf;
2464         }
2465     }
2466
2467     return NULL;
2468 }
2469
2470 /* Parses a subfield from the beginning of '*sp' into 'sf'.  If successful,
2471  * returns NULL and advances '*sp' to the first byte following the parsed
2472  * string.  On failure, returns a malloc()'d error message, does not modify
2473  * '*sp', and does not properly initialize 'sf'.
2474  *
2475  * The syntax parsed from '*sp' takes the form "header[start..end]" where
2476  * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2477  * bit indexes.  "..end" may be omitted to indicate a single bit.  "start..end"
2478  * may both be omitted (the [] are still required) to indicate an entire
2479  * field. */
2480 char *
2481 mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
2482 {
2483     const struct mf_field *field;
2484     const char *name;
2485     int start, end;
2486     const char *s;
2487     int name_len;
2488     bool wild;
2489
2490     s = *sp;
2491     name = s;
2492     name_len = strcspn(s, "[");
2493     if (s[name_len] != '[') {
2494         return xasprintf("%s: missing [ looking for field name", *sp);
2495     }
2496
2497     field = mf_parse_subfield_name(name, name_len, &wild);
2498     if (!field) {
2499         return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
2500     }
2501
2502     s += name_len;
2503     if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
2504         /* Nothing to do. */
2505     } else if (sscanf(s, "[%d]", &start) == 1) {
2506         end = start;
2507     } else if (!strncmp(s, "[]", 2)) {
2508         start = 0;
2509         end = field->n_bits - 1;
2510     } else {
2511         return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2512                          "[<start>..<end>]", *sp);
2513     }
2514     s = strchr(s, ']') + 1;
2515
2516     if (start > end) {
2517         return xasprintf("%s: starting bit %d is after ending bit %d",
2518                          *sp, start, end);
2519     } else if (start >= field->n_bits) {
2520         return xasprintf("%s: starting bit %d is not valid because field is "
2521                          "only %d bits wide", *sp, start, field->n_bits);
2522     } else if (end >= field->n_bits){
2523         return xasprintf("%s: ending bit %d is not valid because field is "
2524                          "only %d bits wide", *sp, end, field->n_bits);
2525     }
2526
2527     sf->field = field;
2528     sf->ofs = start;
2529     sf->n_bits = end - start + 1;
2530
2531     *sp = s;
2532     return NULL;
2533 }
2534
2535 /* Parses a subfield from the beginning of 's' into 'sf'.  Returns the first
2536  * byte in 's' following the parsed string.
2537  *
2538  * Exits with an error message if 's' has incorrect syntax.
2539  *
2540  * The syntax parsed from 's' takes the form "header[start..end]" where
2541  * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2542  * bit indexes.  "..end" may be omitted to indicate a single bit.  "start..end"
2543  * may both be omitted (the [] are still required) to indicate an entire
2544  * field.  */
2545 const char *
2546 mf_parse_subfield(struct mf_subfield *sf, const char *s)
2547 {
2548     char *msg = mf_parse_subfield__(sf, &s);
2549     if (msg) {
2550         ovs_fatal(0, "%s", msg);
2551     }
2552     return s;
2553 }