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