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