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