nicira-ext: Bump number of registers to five from four.
[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         NXM_NX_TUN_ID,
52     }, {
53         MFF_IN_PORT, "in_port", NULL,
54         MF_FIELD_SIZES(be16),
55         MFM_NONE, FWW_IN_PORT,
56         MFS_OFP_PORT,
57         MFP_NONE,
58         NXM_OF_IN_PORT,
59     },
60
61 #define REGISTER(IDX)                           \
62     {                                           \
63         MFF_REG##IDX, "reg" #IDX, NULL,         \
64         MF_FIELD_SIZES(be32),                   \
65         MFM_FULLY, 0,                           \
66         MFS_HEXADECIMAL,                        \
67         MFP_NONE,                               \
68         NXM_NX_REG(IDX),                        \
69     }
70 #if FLOW_N_REGS > 0
71     REGISTER(0),
72 #endif
73 #if FLOW_N_REGS > 1
74     REGISTER(1),
75 #endif
76 #if FLOW_N_REGS > 2
77     REGISTER(2),
78 #endif
79 #if FLOW_N_REGS > 3
80     REGISTER(3),
81 #endif
82 #if FLOW_N_REGS > 4
83     REGISTER(4),
84 #endif
85 #if FLOW_N_REGS > 5
86 #error
87 #endif
88
89     /* ## -- ## */
90     /* ## L2 ## */
91     /* ## -- ## */
92
93     {
94         MFF_ETH_SRC, "eth_src", "dl_src",
95         MF_FIELD_SIZES(mac),
96         MFM_NONE, FWW_DL_SRC,
97         MFS_ETHERNET,
98         MFP_NONE,
99         NXM_OF_ETH_SRC,
100     }, {
101         MFF_ETH_DST, "eth_dst", "dl_dst",
102         MF_FIELD_SIZES(mac),
103         MFM_MCAST, 0,
104         MFS_ETHERNET,
105         MFP_NONE,
106         NXM_OF_ETH_DST,
107     }, {
108         MFF_ETH_TYPE, "eth_type", "dl_type",
109         MF_FIELD_SIZES(be16),
110         MFM_NONE, FWW_DL_TYPE,
111         MFS_HEXADECIMAL,
112         MFP_NONE,
113         NXM_OF_ETH_TYPE,
114     },
115
116     {
117         MFF_VLAN_TCI, "vlan_tci", NULL,
118         MF_FIELD_SIZES(be16),
119         MFM_FULLY, 0,
120         MFS_HEXADECIMAL,
121         MFP_NONE,
122         NXM_OF_VLAN_TCI,
123     }, {
124         MFF_VLAN_VID, "dl_vlan", NULL,
125         sizeof(ovs_be16), 12,
126         MFM_NONE, 0,
127         MFS_DECIMAL,
128         MFP_NONE,
129         0,
130     }, {
131         MFF_VLAN_PCP, "dl_vlan_pcp", NULL,
132         1, 3,
133         MFM_NONE, 0,
134         MFS_DECIMAL,
135         MFP_NONE,
136         0,
137     },
138
139     /* ## -- ## */
140     /* ## L3 ## */
141     /* ## -- ## */
142
143     {
144         MFF_IPV4_SRC, "ip_src", "nw_src",
145         MF_FIELD_SIZES(be32),
146         MFM_CIDR, 0,
147         MFS_IPV4,
148         MFP_IPV4,
149         NXM_OF_IP_SRC,
150     }, {
151         MFF_IPV4_DST, "ip_dst", "nw_dst",
152         MF_FIELD_SIZES(be32),
153         MFM_CIDR, 0,
154         MFS_IPV4,
155         MFP_IPV4,
156         NXM_OF_IP_DST,
157     },
158
159     {
160         MFF_IPV6_SRC, "ipv6_src", NULL,
161         MF_FIELD_SIZES(ipv6),
162         MFM_CIDR, 0,
163         MFS_IPV6,
164         MFP_IPV6,
165         NXM_NX_IPV6_SRC,
166     }, {
167         MFF_IPV6_DST, "ipv6_dst", NULL,
168         MF_FIELD_SIZES(ipv6),
169         MFM_CIDR, 0,
170         MFS_IPV6,
171         MFP_IPV6,
172         NXM_NX_IPV6_DST,
173     },
174
175     {
176         MFF_IP_PROTO, "nw_proto", NULL,
177         MF_FIELD_SIZES(u8),
178         MFM_NONE, FWW_NW_PROTO,
179         MFS_DECIMAL,
180         MFP_IP_ANY,
181         NXM_OF_IP_PROTO,
182     }, {
183         MFF_IP_TOS, "nw_tos", NULL,
184         MF_FIELD_SIZES(u8),
185         MFM_NONE, FWW_NW_TOS,
186         MFS_DECIMAL,
187         MFP_IP_ANY,
188         NXM_OF_IP_TOS,
189     },
190
191     {
192         MFF_ARP_OP, "arp_op", NULL,
193         MF_FIELD_SIZES(be16),
194         MFM_NONE, FWW_NW_PROTO,
195         MFS_DECIMAL,
196         MFP_ARP,
197         NXM_OF_ARP_OP,
198     }, {
199         MFF_ARP_SPA, "arp_spa", NULL,
200         MF_FIELD_SIZES(be32),
201         MFM_CIDR, 0,
202         MFS_IPV4,
203         MFP_ARP,
204         NXM_OF_ARP_SPA,
205     }, {
206         MFF_ARP_TPA, "arp_tpa", NULL,
207         MF_FIELD_SIZES(be32),
208         MFM_CIDR, 0,
209         MFS_IPV4,
210         MFP_ARP,
211         NXM_OF_ARP_TPA,
212     }, {
213         MFF_ARP_SHA, "arp_sha", NULL,
214         MF_FIELD_SIZES(mac),
215         MFM_NONE, FWW_ARP_SHA,
216         MFS_ETHERNET,
217         MFP_ARP,
218         NXM_NX_ARP_SHA,
219     }, {
220         MFF_ARP_THA, "arp_tha", NULL,
221         MF_FIELD_SIZES(mac),
222         MFM_NONE, FWW_ARP_THA,
223         MFS_ETHERNET,
224         MFP_ARP,
225         NXM_NX_ARP_THA,
226     },
227
228     /* ## -- ## */
229     /* ## L4 ## */
230     /* ## -- ## */
231
232     {
233         MFF_TCP_SRC, "tcp_src", "tp_src",
234         MF_FIELD_SIZES(be16),
235         MFM_NONE, FWW_TP_SRC,
236         MFS_DECIMAL,
237         MFP_TCP,
238         NXM_OF_TCP_SRC,
239     }, {
240         MFF_TCP_DST, "tcp_dst", "tp_dst",
241         MF_FIELD_SIZES(be16),
242         MFM_NONE, FWW_TP_DST,
243         MFS_DECIMAL,
244         MFP_TCP,
245         NXM_OF_TCP_DST,
246     },
247
248     {
249         MFF_UDP_SRC, "udp_src", NULL,
250         MF_FIELD_SIZES(be16),
251         MFM_NONE, FWW_TP_SRC,
252         MFS_DECIMAL,
253         MFP_UDP,
254         NXM_OF_UDP_SRC,
255     }, {
256         MFF_UDP_DST, "udp_dst", NULL,
257         MF_FIELD_SIZES(be16),
258         MFM_NONE, FWW_TP_DST,
259         MFS_DECIMAL,
260         MFP_UDP,
261         NXM_OF_UDP_DST,
262     },
263
264     {
265         MFF_ICMP_TYPE, "icmp_type", NULL,
266         MF_FIELD_SIZES(u8),
267         MFM_NONE, FWW_TP_SRC,
268         MFS_DECIMAL,
269         MFP_ICMP_ANY,
270         NXM_OF_ICMP_TYPE,
271     }, {
272         MFF_ICMP_CODE, "icmp_code", NULL,
273         MF_FIELD_SIZES(u8),
274         MFM_NONE, FWW_TP_DST,
275         MFS_DECIMAL,
276         MFP_ICMP_ANY,
277         NXM_OF_ICMP_CODE,
278     },
279
280     /* ## ---- ## */
281     /* ## L"5" ## */
282     /* ## ---- ## */
283
284     {
285         MFF_ND_TARGET, "nd_target", NULL,
286         MF_FIELD_SIZES(ipv6),
287         MFM_NONE, FWW_ND_TARGET,
288         MFS_IPV6,
289         MFP_ND,
290         NXM_NX_ND_TARGET,
291     }, {
292         MFF_ND_SLL, "nd_sll", NULL,
293         MF_FIELD_SIZES(mac),
294         MFM_NONE, FWW_ARP_SHA,
295         MFS_ETHERNET,
296         MFP_ND_SOLICIT,
297         NXM_NX_ND_SLL,
298     }, {
299         MFF_ND_TLL, "nd_tll", NULL,
300         MF_FIELD_SIZES(mac),
301         MFM_NONE, FWW_ARP_THA,
302         MFS_ETHERNET,
303         MFP_ND_ADVERT,
304         NXM_NX_ND_TLL,
305     }
306 };
307
308 /* Returns the field with the given 'id'. */
309 const struct mf_field *
310 mf_from_id(enum mf_field_id id)
311 {
312     assert((unsigned int) id < MFF_N_IDS);
313     return &mf_fields[id];
314 }
315
316 /* Returns the field with the given 'name', or a null pointer if no field has
317  * that name. */
318 const struct mf_field *
319 mf_from_name(const char *name)
320 {
321     static struct shash mf_by_name = SHASH_INITIALIZER(&mf_by_name);
322
323     if (shash_is_empty(&mf_by_name)) {
324         const struct mf_field *mf;
325
326         for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
327             shash_add_once(&mf_by_name, mf->name, mf);
328             if (mf->extra_name) {
329                 shash_add_once(&mf_by_name, mf->extra_name, mf);
330             }
331         }
332     }
333
334     return shash_find_data(&mf_by_name, name);
335 }
336
337 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
338  * specifies at least one bit in the field.
339  *
340  * The caller is responsible for ensuring that 'wc' corresponds to a flow that
341  * meets 'mf''s prerequisites. */
342 bool
343 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
344 {
345     switch (mf->id) {
346     case MFF_IN_PORT:
347     case MFF_ETH_SRC:
348     case MFF_ETH_TYPE:
349     case MFF_IP_PROTO:
350     case MFF_IP_TOS:
351     case MFF_ARP_OP:
352     case MFF_ARP_SHA:
353     case MFF_ARP_THA:
354     case MFF_TCP_SRC:
355     case MFF_TCP_DST:
356     case MFF_UDP_SRC:
357     case MFF_UDP_DST:
358     case MFF_ICMP_TYPE:
359     case MFF_ICMP_CODE:
360     case MFF_ND_TARGET:
361     case MFF_ND_SLL:
362     case MFF_ND_TLL:
363         assert(mf->fww_bit != 0);
364         return (wc->wildcards & mf->fww_bit) != 0;
365
366     case MFF_TUN_ID:
367         return !wc->tun_id_mask;
368
369 #if FLOW_N_REGS > 0
370     case MFF_REG0:
371 #endif
372 #if FLOW_N_REGS > 1
373     case MFF_REG1:
374 #endif
375 #if FLOW_N_REGS > 2
376     case MFF_REG2:
377 #endif
378 #if FLOW_N_REGS > 3
379     case MFF_REG3:
380 #endif
381 #if FLOW_N_REGS > 4
382     case MFF_REG4:
383 #endif
384 #if FLOW_N_REGS > 5
385 #error
386 #endif
387         return !wc->reg_masks[mf->id - MFF_REG0];
388
389     case MFF_ETH_DST:
390         return ((wc->wildcards & (FWW_ETH_MCAST | FWW_DL_DST))
391                 == (FWW_ETH_MCAST | FWW_DL_DST));
392
393     case MFF_VLAN_TCI:
394         return !wc->vlan_tci_mask;
395     case MFF_VLAN_VID:
396         return !(wc->vlan_tci_mask & htons(VLAN_VID_MASK));
397     case MFF_VLAN_PCP:
398         return !(wc->vlan_tci_mask & htons(VLAN_PCP_MASK));
399
400     case MFF_IPV4_SRC:
401         return !wc->nw_src_mask;
402     case MFF_IPV4_DST:
403         return !wc->nw_dst_mask;
404
405     case MFF_IPV6_SRC:
406         return ipv6_mask_is_any(&wc->ipv6_src_mask);
407     case MFF_IPV6_DST:
408         return ipv6_mask_is_any(&wc->ipv6_dst_mask);
409
410     case MFF_ARP_SPA:
411         return !wc->nw_src_mask;
412     case MFF_ARP_TPA:
413         return !wc->nw_dst_mask;
414
415     case MFF_N_IDS:
416     default:
417         NOT_REACHED();
418     }
419 }
420
421 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
422  * Each bit in 'mask' will be set to 1 if the bit is significant for matching
423  * purposes, or to 0 if it is wildcarded.
424  *
425  * The caller is responsible for ensuring that 'wc' corresponds to a flow that
426  * meets 'mf''s prerequisites. */
427 void
428 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
429             union mf_value *mask)
430 {
431     switch (mf->id) {
432     case MFF_IN_PORT:
433     case MFF_ETH_SRC:
434     case MFF_ETH_TYPE:
435     case MFF_IP_PROTO:
436     case MFF_IP_TOS:
437     case MFF_ARP_OP:
438     case MFF_ARP_SHA:
439     case MFF_ARP_THA:
440     case MFF_TCP_SRC:
441     case MFF_TCP_DST:
442     case MFF_UDP_SRC:
443     case MFF_UDP_DST:
444     case MFF_ICMP_TYPE:
445     case MFF_ICMP_CODE:
446     case MFF_ND_TARGET:
447     case MFF_ND_SLL:
448     case MFF_ND_TLL:
449         assert(mf->fww_bit != 0);
450         memset(mask, wc->wildcards & mf->fww_bit ? 0x00 : 0xff, mf->n_bytes);
451         break;
452
453     case MFF_TUN_ID:
454         mask->be64 = wc->tun_id_mask;
455         break;
456
457 #if FLOW_N_REGS > 0
458     case MFF_REG0:
459 #endif
460 #if FLOW_N_REGS > 1
461     case MFF_REG1:
462 #endif
463 #if FLOW_N_REGS > 2
464     case MFF_REG2:
465 #endif
466 #if FLOW_N_REGS > 3
467     case MFF_REG3:
468 #endif
469 #if FLOW_N_REGS > 4
470     case MFF_REG4:
471 #endif
472 #if FLOW_N_REGS > 5
473 #error
474 #endif
475         mask->be32 = htonl(wc->reg_masks[mf->id - MFF_REG0]);
476         break;
477
478     case MFF_ETH_DST:
479         memcpy(mask->mac, flow_wildcards_to_dl_dst_mask(wc->wildcards),
480                ETH_ADDR_LEN);
481         break;
482
483     case MFF_VLAN_TCI:
484         mask->be16 = wc->vlan_tci_mask;
485         break;
486     case MFF_VLAN_VID:
487         mask->be16 = wc->vlan_tci_mask & htons(VLAN_VID_MASK);
488         break;
489     case MFF_VLAN_PCP:
490         mask->u8 = vlan_tci_to_pcp(wc->vlan_tci_mask);
491         break;
492
493     case MFF_IPV4_SRC:
494         mask->be32 = wc->nw_src_mask;
495         break;
496     case MFF_IPV4_DST:
497         mask->be32 = wc->nw_dst_mask;
498         break;
499
500     case MFF_IPV6_SRC:
501         mask->ipv6 = wc->ipv6_src_mask;
502         break;
503     case MFF_IPV6_DST:
504         mask->ipv6 = wc->ipv6_dst_mask;
505         break;
506
507     case MFF_ARP_SPA:
508         mask->be32 = wc->nw_src_mask;
509         break;
510     case MFF_ARP_TPA:
511         mask->be32 = wc->nw_dst_mask;
512         break;
513
514     case MFF_N_IDS:
515     default:
516         NOT_REACHED();
517     }
518 }
519
520 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'.  Returns true
521  * if the mask is valid, false otherwise. */
522 bool
523 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
524 {
525     switch (mf->maskable) {
526     case MFM_NONE:
527         return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
528                 is_all_ones((const uint8_t *) mask, mf->n_bytes));
529
530     case MFM_FULLY:
531         return true;
532
533     case MFM_CIDR:
534         return (mf->n_bytes == 4
535                 ? ip_is_cidr(mask->be32)
536                 : ipv6_is_cidr(&mask->ipv6));
537
538     case MFM_MCAST:
539         return flow_wildcards_is_dl_dst_mask_valid(mask->mac);
540     }
541
542     NOT_REACHED();
543 }
544
545 static bool
546 is_ip_any(const struct flow *flow)
547 {
548     return (flow->dl_type == htons(ETH_TYPE_IP) ||
549             flow->dl_type == htons(ETH_TYPE_IPV6));
550 }
551
552 static bool
553 is_icmpv4(const struct flow *flow)
554 {
555     return (flow->dl_type == htons(ETH_TYPE_IP)
556             && flow->nw_proto == IPPROTO_ICMP);
557 }
558
559 static bool
560 is_icmpv6(const struct flow *flow)
561 {
562     return (flow->dl_type == htons(ETH_TYPE_IPV6)
563             && flow->nw_proto == IPPROTO_ICMPV6);
564 }
565
566 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
567 bool
568 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
569 {
570     switch (mf->prereqs) {
571     case MFP_NONE:
572         return true;
573
574     case MFP_ARP:
575         return flow->dl_type == htons(ETH_TYPE_ARP);
576     case MFP_IPV4:
577         return flow->dl_type == htons(ETH_TYPE_IP);
578     case MFP_IPV6:
579         return flow->dl_type == htons(ETH_TYPE_IPV6);
580     case MFP_IP_ANY:
581         return is_ip_any(flow);
582
583     case MFP_TCP:
584         return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
585     case MFP_UDP:
586         return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
587     case MFP_ICMPV6:
588         return is_icmpv6(flow);
589     case MFP_ICMP_ANY:
590         return is_icmpv4(flow) || is_icmpv6(flow);
591
592     case MFP_ND:
593         return (is_icmpv6(flow)
594                 && flow->icmp_code == htons(0)
595                 && (flow->icmp_type == htons(ND_NEIGHBOR_SOLICIT) ||
596                     flow->icmp_type == htons(ND_NEIGHBOR_ADVERT)));
597     case MFP_ND_SOLICIT:
598         return (is_icmpv6(flow)
599                 && flow->icmp_code == htons(0)
600                 && (flow->icmp_type == htons(ND_NEIGHBOR_SOLICIT)));
601     case MFP_ND_ADVERT:
602         return (is_icmpv6(flow)
603                 && flow->icmp_code == htons(0)
604                 && (flow->icmp_type == htons(ND_NEIGHBOR_ADVERT)));
605     }
606
607     NOT_REACHED();
608 }
609
610 /* Returns true if 'value' may be a valid value *as part of a masked match*,
611  * false otherwise.
612  *
613  * A value is not rejected just because it is not valid for the field in
614  * question, but only if it doesn't make sense to test the bits in question at
615  * all.  For example, the MFF_VLAN_TCI field will never have a nonzero value
616  * without the VLAN_CFI bit being set, but we can't reject those values because
617  * it is still legitimate to test just for those bits (see the documentation
618  * for NXM_OF_VLAN_TCI in nicira-ext.h).  On the other hand, there is never a
619  * reason to set the low bit of MFF_IP_TOS to 1, so we reject that. */
620 bool
621 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
622 {
623     switch (mf->id) {
624     case MFF_TUN_ID:
625     case MFF_IN_PORT:
626 #if FLOW_N_REGS > 0
627     case MFF_REG0:
628 #endif
629 #if FLOW_N_REGS > 1
630     case MFF_REG1:
631 #endif
632 #if FLOW_N_REGS > 2
633     case MFF_REG2:
634 #endif
635 #if FLOW_N_REGS > 3
636     case MFF_REG3:
637 #endif
638 #if FLOW_N_REGS > 4
639     case MFF_REG4:
640 #endif
641 #if FLOW_N_REGS > 5
642 #error
643 #endif
644     case MFF_ETH_SRC:
645     case MFF_ETH_DST:
646     case MFF_ETH_TYPE:
647     case MFF_VLAN_TCI:
648     case MFF_IPV4_SRC:
649     case MFF_IPV4_DST:
650     case MFF_IPV6_SRC:
651     case MFF_IPV6_DST:
652     case MFF_IP_PROTO:
653     case MFF_ARP_SPA:
654     case MFF_ARP_TPA:
655     case MFF_ARP_SHA:
656     case MFF_ARP_THA:
657     case MFF_TCP_SRC:
658     case MFF_TCP_DST:
659     case MFF_UDP_SRC:
660     case MFF_UDP_DST:
661     case MFF_ICMP_TYPE:
662     case MFF_ICMP_CODE:
663     case MFF_ND_TARGET:
664     case MFF_ND_SLL:
665     case MFF_ND_TLL:
666         return true;
667
668     case MFF_IP_TOS:
669         return !(value->u8 & 0x03);
670
671     case MFF_ARP_OP:
672         return !(value->be16 & htons(0xff00));
673
674     case MFF_VLAN_VID:
675         return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
676
677     case MFF_VLAN_PCP:
678         return !(value->u8 & ~7);
679
680     case MFF_N_IDS:
681     default:
682         NOT_REACHED();
683     }
684 }
685
686 /* Copies the value of field 'mf' from 'flow' into 'value'.  The caller is
687  * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
688 void
689 mf_get_value(const struct mf_field *mf, const struct flow *flow,
690              union mf_value *value)
691 {
692     switch (mf->id) {
693     case MFF_TUN_ID:
694         value->be64 = flow->tun_id;
695         break;
696
697     case MFF_IN_PORT:
698         value->be16 = htons(flow->in_port);
699         break;
700
701 #if FLOW_N_REGS > 0
702     case MFF_REG0:
703 #endif
704 #if FLOW_N_REGS > 1
705     case MFF_REG1:
706 #endif
707 #if FLOW_N_REGS > 2
708     case MFF_REG2:
709 #endif
710 #if FLOW_N_REGS > 3
711     case MFF_REG3:
712 #endif
713 #if FLOW_N_REGS > 4
714     case MFF_REG4:
715 #endif
716 #if FLOW_N_REGS > 5
717 #error
718 #endif
719         value->be32 = htonl(flow->regs[0]);
720         break;
721
722     case MFF_ETH_SRC:
723         memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
724         break;
725
726     case MFF_ETH_DST:
727         memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
728         break;
729
730     case MFF_ETH_TYPE:
731         value->be16 = flow->dl_type;
732         break;
733
734     case MFF_VLAN_TCI:
735         value->be16 = flow->vlan_tci;
736         break;
737
738     case MFF_VLAN_VID:
739         value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
740         break;
741
742     case MFF_VLAN_PCP:
743         value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
744         break;
745
746     case MFF_IPV4_SRC:
747         value->be32 = flow->nw_src;
748         break;
749
750     case MFF_IPV4_DST:
751         value->be32 = flow->nw_dst;
752         break;
753
754     case MFF_IPV6_SRC:
755         value->ipv6 = flow->ipv6_src;
756         break;
757
758     case MFF_IPV6_DST:
759         value->ipv6 = flow->ipv6_dst;
760         break;
761
762     case MFF_IP_PROTO:
763         value->u8 = flow->nw_proto;
764         break;
765
766     case MFF_IP_TOS:
767         value->u8 = flow->nw_tos;
768         break;
769
770     case MFF_ARP_OP:
771         value->be16 = htons(flow->nw_proto);
772         break;
773
774     case MFF_ARP_SPA:
775         value->be32 = flow->nw_src;
776         break;
777
778     case MFF_ARP_TPA:
779         value->be32 = flow->nw_dst;
780         break;
781
782     case MFF_ARP_SHA:
783     case MFF_ND_SLL:
784         memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
785         break;
786
787     case MFF_ARP_THA:
788     case MFF_ND_TLL:
789         memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
790         break;
791
792     case MFF_TCP_SRC:
793         value->be16 = flow->tp_src;
794         break;
795
796     case MFF_TCP_DST:
797         value->be16 = flow->tp_dst;
798         break;
799
800     case MFF_UDP_SRC:
801         value->be16 = flow->tp_src;
802         break;
803
804     case MFF_UDP_DST:
805         value->be16 = flow->tp_dst;
806         break;
807
808     case MFF_ICMP_TYPE:
809         value->u8 = ntohs(flow->tp_src);
810         break;
811
812     case MFF_ICMP_CODE:
813         value->u8 = ntohs(flow->tp_dst);
814         break;
815
816     case MFF_ND_TARGET:
817         value->ipv6 = flow->nd_target;
818         break;
819
820     case MFF_N_IDS:
821     default:
822         NOT_REACHED();
823     }
824 }
825
826 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
827  * 'value'.  The caller is responsible for ensuring that 'rule' meets 'mf''s
828  * prerequisites. */
829 void
830 mf_set_value(const struct mf_field *mf,
831              const union mf_value *value, struct cls_rule *rule)
832 {
833     switch (mf->id) {
834     case MFF_TUN_ID:
835         cls_rule_set_tun_id(rule, value->be64);
836         break;
837
838     case MFF_IN_PORT:
839         cls_rule_set_in_port(rule, ntohs(value->be16));
840         break;
841
842 #if FLOW_N_REGS > 0
843     case MFF_REG0:
844 #endif
845 #if FLOW_N_REGS > 1
846     case MFF_REG1:
847 #endif
848 #if FLOW_N_REGS > 2
849     case MFF_REG2:
850 #endif
851 #if FLOW_N_REGS > 3
852     case MFF_REG3:
853 #endif
854 #if FLOW_N_REGS > 4
855     case MFF_REG4:
856 #endif
857 #if FLOW_N_REGS > 5
858 #error
859 #endif
860 #if FLOW_N_REGS > 0
861         cls_rule_set_reg(rule, mf->id - MFF_REG0, ntohl(value->be32));
862         break;
863 #endif
864
865     case MFF_ETH_SRC:
866         cls_rule_set_dl_src(rule, value->mac);
867         break;
868
869     case MFF_ETH_DST:
870         cls_rule_set_dl_dst(rule, value->mac);
871         break;
872
873     case MFF_ETH_TYPE:
874         cls_rule_set_dl_type(rule, value->be16);
875         break;
876
877     case MFF_VLAN_TCI:
878         cls_rule_set_dl_tci(rule, value->be16);
879         break;
880
881     case MFF_VLAN_VID:
882         cls_rule_set_dl_vlan(rule, value->be16);
883         break;
884
885     case MFF_VLAN_PCP:
886         cls_rule_set_dl_vlan_pcp(rule, value->u8);
887         break;
888
889     case MFF_IPV4_SRC:
890         cls_rule_set_nw_src(rule, value->be32);
891         break;
892
893     case MFF_IPV4_DST:
894         cls_rule_set_nw_dst(rule, value->be32);
895         break;
896
897     case MFF_IPV6_SRC:
898         cls_rule_set_ipv6_src(rule, &value->ipv6);
899         break;
900
901     case MFF_IPV6_DST:
902         cls_rule_set_ipv6_dst(rule, &value->ipv6);
903         break;
904
905     case MFF_IP_PROTO:
906         cls_rule_set_nw_proto(rule, value->u8);
907         break;
908
909     case MFF_IP_TOS:
910         cls_rule_set_nw_tos(rule, value->u8);
911         break;
912
913     case MFF_ARP_OP:
914         cls_rule_set_nw_proto(rule, ntohs(value->be16));
915         break;
916
917     case MFF_ARP_SPA:
918         cls_rule_set_nw_src(rule, value->be32);
919         break;
920
921     case MFF_ARP_TPA:
922         cls_rule_set_nw_dst(rule, value->be32);
923         break;
924
925     case MFF_ARP_SHA:
926     case MFF_ND_SLL:
927         cls_rule_set_arp_sha(rule, value->mac);
928         break;
929
930     case MFF_ARP_THA:
931     case MFF_ND_TLL:
932         cls_rule_set_arp_tha(rule, value->mac);
933         break;
934
935     case MFF_TCP_SRC:
936         cls_rule_set_tp_src(rule, value->be16);
937         break;
938
939     case MFF_TCP_DST:
940         cls_rule_set_tp_dst(rule, value->be16);
941         break;
942
943     case MFF_UDP_SRC:
944         cls_rule_set_tp_src(rule, value->be16);
945         break;
946
947     case MFF_UDP_DST:
948         cls_rule_set_tp_dst(rule, value->be16);
949         break;
950
951     case MFF_ICMP_TYPE:
952         cls_rule_set_icmp_type(rule, value->u8);
953         break;
954
955     case MFF_ICMP_CODE:
956         cls_rule_set_icmp_code(rule, value->u8);
957         break;
958
959     case MFF_ND_TARGET:
960         cls_rule_set_nd_target(rule, &value->ipv6);
961         break;
962
963     case MFF_N_IDS:
964     default:
965         NOT_REACHED();
966     }
967 }
968
969 /* Makes 'rule' wildcard field 'mf'.
970  *
971  * The caller is responsible for ensuring that 'rule' meets 'mf''s
972  * prerequisites. */
973 void
974 mf_set_wild(const struct mf_field *mf, struct cls_rule *rule)
975 {
976     switch (mf->id) {
977     case MFF_TUN_ID:
978         cls_rule_set_tun_id_masked(rule, htonll(0), htonll(0));
979         break;
980
981     case MFF_IN_PORT:
982         rule->wc.wildcards |= FWW_IN_PORT;
983         rule->flow.in_port = 0;
984         break;
985
986 #if FLOW_N_REGS > 0
987     case MFF_REG0:
988         cls_rule_set_reg_masked(rule, 0, 0, 0);
989         break;
990 #endif
991 #if FLOW_N_REGS > 1
992     case MFF_REG1:
993         cls_rule_set_reg_masked(rule, 1, 0, 0);
994         break;
995 #endif
996 #if FLOW_N_REGS > 2
997     case MFF_REG2:
998         cls_rule_set_reg_masked(rule, 2, 0, 0);
999         break;
1000 #endif
1001 #if FLOW_N_REGS > 3
1002     case MFF_REG3:
1003         cls_rule_set_reg_masked(rule, 3, 0, 0);
1004         break;
1005 #endif
1006 #if FLOW_N_REGS > 4
1007     case MFF_REG4:
1008         cls_rule_set_reg_masked(rule, 4, 0, 0);
1009         break;
1010 #endif
1011 #if FLOW_N_REGS > 5
1012 #error
1013 #endif
1014
1015     case MFF_ETH_SRC:
1016         rule->wc.wildcards |= FWW_DL_SRC;
1017         memset(rule->flow.dl_src, 0, sizeof rule->flow.dl_src);
1018         break;
1019
1020     case MFF_ETH_DST:
1021         rule->wc.wildcards |= FWW_DL_DST | FWW_ETH_MCAST;
1022         memset(rule->flow.dl_dst, 0, sizeof rule->flow.dl_dst);
1023         break;
1024
1025     case MFF_ETH_TYPE:
1026         rule->wc.wildcards |= FWW_DL_TYPE;
1027         rule->flow.dl_type = htons(0);
1028         break;
1029
1030     case MFF_VLAN_TCI:
1031         cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
1032         break;
1033
1034     case MFF_VLAN_VID:
1035         cls_rule_set_any_vid(rule);
1036         break;
1037
1038     case MFF_VLAN_PCP:
1039         cls_rule_set_any_pcp(rule);
1040         break;
1041
1042     case MFF_IPV4_SRC:
1043     case MFF_ARP_SPA:
1044         cls_rule_set_nw_src_masked(rule, htonl(0), htonl(0));
1045         break;
1046
1047     case MFF_IPV4_DST:
1048     case MFF_ARP_TPA:
1049         cls_rule_set_nw_dst_masked(rule, htonl(0), htonl(0));
1050         break;
1051
1052     case MFF_IPV6_SRC:
1053         memset(&rule->wc.ipv6_src_mask, 0, sizeof rule->wc.ipv6_src_mask);
1054         memset(&rule->flow.ipv6_src, 0, sizeof rule->flow.ipv6_src);
1055         break;
1056
1057     case MFF_IPV6_DST:
1058         memset(&rule->wc.ipv6_dst_mask, 0, sizeof rule->wc.ipv6_dst_mask);
1059         memset(&rule->flow.ipv6_dst, 0, sizeof rule->flow.ipv6_dst);
1060         break;
1061
1062     case MFF_IP_PROTO:
1063         rule->wc.wildcards |= FWW_NW_PROTO;
1064         rule->flow.nw_proto = 0;
1065         break;
1066
1067     case MFF_IP_TOS:
1068         rule->wc.wildcards |= FWW_NW_TOS;
1069         rule->flow.nw_tos = 0;
1070         break;
1071
1072     case MFF_ARP_OP:
1073         rule->wc.wildcards |= FWW_NW_PROTO;
1074         rule->flow.nw_proto = 0;
1075         break;
1076
1077     case MFF_ARP_SHA:
1078     case MFF_ND_SLL:
1079         rule->wc.wildcards |= FWW_ARP_SHA;
1080         memset(rule->flow.arp_sha, 0, sizeof rule->flow.arp_sha);
1081         break;
1082
1083     case MFF_ARP_THA:
1084     case MFF_ND_TLL:
1085         rule->wc.wildcards |= FWW_ARP_THA;
1086         memset(rule->flow.arp_tha, 0, sizeof rule->flow.arp_tha);
1087         break;
1088
1089     case MFF_TCP_SRC:
1090     case MFF_UDP_SRC:
1091     case MFF_ICMP_TYPE:
1092         rule->wc.wildcards |= FWW_TP_SRC;
1093         rule->flow.tp_src = htons(0);
1094         break;
1095
1096     case MFF_TCP_DST:
1097     case MFF_UDP_DST:
1098     case MFF_ICMP_CODE:
1099         rule->wc.wildcards |= FWW_TP_DST;
1100         rule->flow.tp_dst = htons(0);
1101         break;
1102
1103     case MFF_ND_TARGET:
1104         rule->wc.wildcards |= FWW_ND_TARGET;
1105         memset(&rule->flow.nd_target, 0, sizeof rule->flow.nd_target);
1106         break;
1107
1108     case MFF_N_IDS:
1109     default:
1110         NOT_REACHED();
1111     }
1112 }
1113
1114 /* Makes 'rule' match field 'mf' with the specified 'value' and 'mask'.
1115  * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1116  * with a 1-bit indicating that the corresponding value bit must match and a
1117  * 0-bit indicating a don't-care.
1118  *
1119  * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1120  * mf_set_value(mf, value, rule).  If 'mask' points to all-0-bits, then this
1121  * call is equivalent to mf_set_wild(mf, rule).
1122  *
1123  * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()).  The caller
1124  * is responsible for ensuring that 'rule' meets 'mf''s prerequisites. */
1125 void
1126 mf_set(const struct mf_field *mf,
1127        const union mf_value *value, const union mf_value *mask,
1128        struct cls_rule *rule)
1129 {
1130     if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1131         mf_set_value(mf, value, rule);
1132         return;
1133     } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1134         mf_set_wild(mf, rule);
1135         return;
1136     }
1137
1138     switch (mf->id) {
1139     case MFF_IN_PORT:
1140     case MFF_ETH_SRC:
1141     case MFF_ETH_TYPE:
1142     case MFF_VLAN_VID:
1143     case MFF_VLAN_PCP:
1144     case MFF_IP_PROTO:
1145     case MFF_IP_TOS:
1146     case MFF_ARP_OP:
1147     case MFF_ARP_SHA:
1148     case MFF_ARP_THA:
1149     case MFF_TCP_SRC:
1150     case MFF_TCP_DST:
1151     case MFF_UDP_SRC:
1152     case MFF_UDP_DST:
1153     case MFF_ICMP_TYPE:
1154     case MFF_ICMP_CODE:
1155     case MFF_ND_TARGET:
1156     case MFF_ND_SLL:
1157     case MFF_ND_TLL:
1158         NOT_REACHED();
1159
1160     case MFF_TUN_ID:
1161         cls_rule_set_tun_id_masked(rule, value->be64, mask->be64);
1162         break;
1163
1164 #if FLOW_N_REGS > 0
1165     case MFF_REG0:
1166 #endif
1167 #if FLOW_N_REGS > 1
1168     case MFF_REG1:
1169 #endif
1170 #if FLOW_N_REGS > 2
1171     case MFF_REG2:
1172 #endif
1173 #if FLOW_N_REGS > 3
1174     case MFF_REG3:
1175 #endif
1176 #if FLOW_N_REGS > 4
1177     case MFF_REG4:
1178 #endif
1179 #if FLOW_N_REGS > 5
1180 #error
1181 #endif
1182         cls_rule_set_reg_masked(rule, mf->id - MFF_REG0,
1183                                 ntohl(value->be32), ntohl(mask->be32));
1184         break;
1185
1186     case MFF_ETH_DST:
1187         if (flow_wildcards_is_dl_dst_mask_valid(mask->mac)) {
1188             cls_rule_set_dl_dst_masked(rule, value->mac, mask->mac);
1189         }
1190         break;
1191
1192     case MFF_VLAN_TCI:
1193         cls_rule_set_dl_tci_masked(rule, value->be16, mask->be16);
1194         break;
1195
1196     case MFF_IPV4_SRC:
1197         cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1198         break;
1199
1200     case MFF_IPV4_DST:
1201         cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1202         break;
1203
1204     case MFF_IPV6_SRC:
1205         cls_rule_set_ipv6_src_masked(rule, &value->ipv6, &mask->ipv6);
1206         break;
1207
1208     case MFF_IPV6_DST:
1209         cls_rule_set_ipv6_dst_masked(rule, &value->ipv6, &mask->ipv6);
1210         break;
1211
1212     case MFF_ARP_SPA:
1213         cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1214         break;
1215
1216     case MFF_ARP_TPA:
1217         cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1218         break;
1219
1220     case MFF_N_IDS:
1221     default:
1222         NOT_REACHED();
1223     }
1224 }
1225
1226 /* Makes a subfield starting at bit offset 'ofs' and continuing for 'n_bits' in
1227  * 'rule''s field 'mf' exactly match the 'n_bits' least-significant bits of
1228  * 'x'.
1229  *
1230  * Example: suppose that 'mf' is originally the following 2-byte field in
1231  * 'rule':
1232  *
1233  *     value == 0xe00a == 2#1110000000001010
1234  *      mask == 0xfc3f == 2#1111110000111111
1235  *
1236  * The call mf_set_subfield(mf, 0x55, 8, 7, rule) would have the following
1237  * effect (note that 0x55 is 2#1010101):
1238  *
1239  *     value == 0xd50a == 2#1101010100001010
1240  *      mask == 0xff3f == 2#1111111100111111
1241  *
1242  * The caller is responsible for ensuring that the result will be a valid
1243  * wildcard pattern for 'mf'.  The caller is responsible for ensuring that
1244  * 'rule' meets 'mf''s prerequisites. */
1245 void
1246 mf_set_subfield(const struct mf_field *mf, uint64_t x, unsigned int ofs,
1247                 unsigned int n_bits, struct cls_rule *rule)
1248 {
1249     if (ofs == 0 && mf->n_bytes * 8 == n_bits) {
1250         union mf_value value;
1251         int i;
1252
1253         for (i = mf->n_bytes - 1; i >= 0; i--) {
1254             ((uint8_t *) &value)[i] = x;
1255             x >>= 8;
1256         }
1257         mf_set_value(mf, &value, rule);
1258     } else {
1259         union mf_value value, mask;
1260         uint8_t *vp, *mp;
1261         unsigned int byte_ofs;
1262
1263         mf_get(mf, rule, &value, &mask);
1264
1265         byte_ofs = mf->n_bytes - ofs / 8;
1266         vp = &((uint8_t *) &value)[byte_ofs];
1267         mp = &((uint8_t *) &mask)[byte_ofs];
1268         if (ofs % 8) {
1269             unsigned int chunk = MIN(8 - ofs % 8, n_bits);
1270             uint8_t chunk_mask = ((1 << chunk) - 1) << (ofs % 8);
1271
1272             *--vp &= ~chunk_mask;
1273             *vp   |= chunk_mask & (x << (ofs % 8));
1274             *--mp |= chunk_mask;
1275
1276             x >>= chunk;
1277             n_bits -= chunk;
1278             ofs += chunk;
1279         }
1280         while (n_bits >= 8) {
1281             *--vp = x;
1282             *--mp = 0xff;
1283             x >>= 8;
1284             n_bits -= 8;
1285             ofs += 8;
1286         }
1287         if (n_bits) {
1288             uint8_t chunk_mask = (1 << n_bits) - 1;
1289
1290             *--vp &= ~chunk_mask;
1291             *vp   |= chunk_mask & x;
1292             *--mp |= chunk_mask;
1293         }
1294
1295         mf_set(mf, &value, &mask, rule);
1296     }
1297 }
1298
1299 /* Copies the value and wildcard bit pattern for 'mf' from 'rule' into the
1300  * 'value' and 'mask', respectively. */
1301 void
1302 mf_get(const struct mf_field *mf, const struct cls_rule *rule,
1303        union mf_value *value, union mf_value *mask)
1304 {
1305     mf_get_value(mf, &rule->flow, value);
1306     mf_get_mask(mf, &rule->wc, mask);
1307 }
1308
1309 /* Assigns a random value for field 'mf' to 'value'. */
1310 void
1311 mf_random_value(const struct mf_field *mf, union mf_value *value)
1312 {
1313     random_bytes(value, mf->n_bytes);
1314
1315     switch (mf->id) {
1316     case MFF_TUN_ID:
1317     case MFF_IN_PORT:
1318 #if FLOW_N_REGS > 0
1319     case MFF_REG0:
1320 #endif
1321 #if FLOW_N_REGS > 1
1322     case MFF_REG1:
1323 #endif
1324 #if FLOW_N_REGS > 2
1325     case MFF_REG2:
1326 #endif
1327 #if FLOW_N_REGS > 3
1328     case MFF_REG3:
1329 #endif
1330 #if FLOW_N_REGS > 4
1331     case MFF_REG4:
1332 #endif
1333 #if FLOW_N_REGS > 5
1334 #error
1335 #endif
1336     case MFF_ETH_SRC:
1337     case MFF_ETH_DST:
1338     case MFF_ETH_TYPE:
1339     case MFF_VLAN_TCI:
1340     case MFF_IPV4_SRC:
1341     case MFF_IPV4_DST:
1342     case MFF_IPV6_SRC:
1343     case MFF_IPV6_DST:
1344     case MFF_IP_PROTO:
1345     case MFF_ARP_SPA:
1346     case MFF_ARP_TPA:
1347     case MFF_ARP_SHA:
1348     case MFF_ARP_THA:
1349     case MFF_TCP_SRC:
1350     case MFF_TCP_DST:
1351     case MFF_UDP_SRC:
1352     case MFF_UDP_DST:
1353     case MFF_ICMP_TYPE:
1354     case MFF_ICMP_CODE:
1355     case MFF_ND_TARGET:
1356     case MFF_ND_SLL:
1357     case MFF_ND_TLL:
1358         break;
1359
1360     case MFF_IP_TOS:
1361         value->u8 &= ~0x03;
1362         break;
1363
1364     case MFF_ARP_OP:
1365         value->be16 &= htons(0xff);
1366         break;
1367
1368     case MFF_VLAN_VID:
1369         value->be16 &= htons(VLAN_VID_MASK);
1370         break;
1371
1372     case MFF_VLAN_PCP:
1373         value->u8 &= 0x07;
1374         break;
1375
1376     case MFF_N_IDS:
1377     default:
1378         NOT_REACHED();
1379     }
1380 }
1381
1382 static char *
1383 mf_from_integer_string(const struct mf_field *mf, const char *s,
1384                        uint8_t *valuep, uint8_t *maskp)
1385 {
1386     unsigned long long int integer, mask;
1387     char *tail;
1388     int i;
1389
1390     errno = 0;
1391     integer = strtoull(s, &tail, 0);
1392     if (errno || (*tail != '\0' && *tail != '/')) {
1393         goto syntax_error;
1394     }
1395
1396     if (*tail == '/') {
1397         mask = strtoull(tail + 1, &tail, 0);
1398         if (errno || *tail != '\0') {
1399             goto syntax_error;
1400         }
1401     } else {
1402         mask = ULLONG_MAX;
1403     }
1404
1405     for (i = mf->n_bytes - 1; i >= 0; i--) {
1406         valuep[i] = integer;
1407         maskp[i] = mask;
1408         integer >>= 8;
1409         mask >>= 8;
1410     }
1411     if (integer) {
1412         return xasprintf("%s: value too large for %u-byte field %s",
1413                          s, mf->n_bytes, mf->name);
1414     }
1415     return NULL;
1416
1417 syntax_error:
1418     return xasprintf("%s: bad syntax for %s", s, mf->name);
1419 }
1420
1421 static char *
1422 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1423                         uint8_t mac[ETH_ADDR_LEN],
1424                         uint8_t mask[ETH_ADDR_LEN])
1425 {
1426     assert(mf->n_bytes == ETH_ADDR_LEN);
1427
1428     switch (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
1429                    ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))){
1430     case ETH_ADDR_SCAN_COUNT * 2:
1431         return NULL;
1432
1433     case ETH_ADDR_SCAN_COUNT:
1434         memset(mask, 0xff, ETH_ADDR_LEN);
1435         return NULL;
1436
1437     default:
1438         return xasprintf("%s: invalid Ethernet address", s);
1439     }
1440 }
1441
1442 static char *
1443 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1444                     ovs_be32 *ip, ovs_be32 *mask)
1445 {
1446     int prefix;
1447
1448     assert(mf->n_bytes == sizeof *ip);
1449
1450     if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1451                IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
1452         /* OK. */
1453     } else if (sscanf(s, IP_SCAN_FMT"/%d",
1454                       IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
1455         if (prefix <= 0 || prefix > 32) {
1456             return xasprintf("%s: network prefix bits not between 1 and "
1457                              "32", s);
1458         } else if (prefix == 32) {
1459             *mask = htonl(UINT32_MAX);
1460         } else {
1461             *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
1462         }
1463     } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
1464         *mask = htonl(UINT32_MAX);
1465     } else {
1466         return xasprintf("%s: invalid IP address", s);
1467     }
1468     return NULL;
1469 }
1470
1471 static char *
1472 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1473                     struct in6_addr *value, struct in6_addr *mask)
1474 {
1475     char *str = xstrdup(s);
1476     char *save_ptr = NULL;
1477     const char *name, *netmask;
1478     int retval;
1479
1480     assert(mf->n_bytes == sizeof *value);
1481
1482     name = strtok_r(str, "/", &save_ptr);
1483     retval = name ? lookup_ipv6(name, value) : EINVAL;
1484     if (retval) {
1485         char *err;
1486
1487         err = xasprintf("%s: could not convert to IPv6 address", str);
1488         free(str);
1489
1490         return err;
1491     }
1492
1493     netmask = strtok_r(NULL, "/", &save_ptr);
1494     if (netmask) {
1495         int prefix = atoi(netmask);
1496         if (prefix <= 0 || prefix > 128) {
1497             free(str);
1498             return xasprintf("%s: prefix bits not between 1 and 128", s);
1499         } else {
1500             *mask = ipv6_create_mask(prefix);
1501         }
1502     } else {
1503         *mask = in6addr_exact;
1504     }
1505     free(str);
1506
1507     return NULL;
1508 }
1509
1510 static char *
1511 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1512                         ovs_be16 *valuep, ovs_be16 *maskp)
1513 {
1514     uint16_t port;
1515
1516     assert(mf->n_bytes == sizeof(ovs_be16));
1517     if (ofputil_port_from_string(s, &port)) {
1518         *valuep = htons(port);
1519         *maskp = htons(UINT16_MAX);
1520         return NULL;
1521     } else {
1522         return mf_from_integer_string(mf, s,
1523                                       (uint8_t *) valuep, (uint8_t *) maskp);
1524     }
1525 }
1526
1527 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'.  Returns
1528  * NULL if successful, otherwise a malloc()'d string describing the error. */
1529 char *
1530 mf_parse(const struct mf_field *mf, const char *s,
1531          union mf_value *value, union mf_value *mask)
1532 {
1533     if (!strcasecmp(s, "any") || !strcmp(s, "*")) {
1534         memset(value, 0, mf->n_bytes);
1535         memset(mask, 0, mf->n_bytes);
1536         return NULL;
1537     }
1538
1539     switch (mf->string) {
1540     case MFS_DECIMAL:
1541     case MFS_HEXADECIMAL:
1542         return mf_from_integer_string(mf, s,
1543                                       (uint8_t *) value, (uint8_t *) mask);
1544
1545     case MFS_ETHERNET:
1546         return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
1547
1548     case MFS_IPV4:
1549         return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
1550
1551     case MFS_IPV6:
1552         return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
1553
1554     case MFS_OFP_PORT:
1555         return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
1556     }
1557     NOT_REACHED();
1558 }
1559
1560 /* Parses 's', a string value for field 'mf', into 'value'.  Returns NULL if
1561  * successful, otherwise a malloc()'d string describing the error. */
1562 char *
1563 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
1564 {
1565     union mf_value mask;
1566     char *error;
1567
1568     error = mf_parse(mf, s, value, &mask);
1569     if (error) {
1570         return error;
1571     }
1572
1573     if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
1574         return xasprintf("%s: wildcards not allowed here", s);
1575     }
1576     return NULL;
1577 }
1578
1579 static void
1580 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
1581                          const uint8_t *maskp, struct ds *s)
1582 {
1583     unsigned long long int integer;
1584     int i;
1585
1586     assert(mf->n_bytes <= 8);
1587
1588     integer = 0;
1589     for (i = 0; i < mf->n_bytes; i++) {
1590         integer = (integer << 8) | valuep[i];
1591     }
1592     if (mf->string == MFS_HEXADECIMAL) {
1593         ds_put_format(s, "%#llx", integer);
1594     } else {
1595         ds_put_format(s, "%lld", integer);
1596     }
1597
1598     if (maskp) {
1599         unsigned long long int mask;
1600
1601         mask = 0;
1602         for (i = 0; i < mf->n_bytes; i++) {
1603             mask = (mask << 8) | maskp[i];
1604         }
1605
1606         /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
1607          * not sure that that a bit-mask written in decimal is ever easier to
1608          * understand than the same bit-mask written in hexadecimal. */
1609         ds_put_format(s, "/%#llx", mask);
1610     }
1611 }
1612
1613 /* Appends to 's' a string representation of field 'mf' whose value is in
1614  * 'value' and 'mask'.  'mask' may be NULL to indicate an exact match. */
1615 void
1616 mf_format(const struct mf_field *mf,
1617           const union mf_value *value, const union mf_value *mask,
1618           struct ds *s)
1619 {
1620     if (mask) {
1621         if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1622             ds_put_cstr(s, "ANY");
1623             return;
1624         } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1625             mask = NULL;
1626         }
1627     }
1628
1629     switch (mf->string) {
1630     case MFS_OFP_PORT:
1631         if (!mask) {
1632             ofputil_format_port(ntohs(value->be16), s);
1633             break;
1634         }
1635         /* fall through */
1636     case MFS_DECIMAL:
1637     case MFS_HEXADECIMAL:
1638         mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
1639         break;
1640
1641     case MFS_ETHERNET:
1642         ds_put_format(s, ETH_ADDR_FMT, ETH_ADDR_ARGS(value->mac));
1643         if (mask) {
1644             ds_put_format(s, "/"ETH_ADDR_FMT, ETH_ADDR_ARGS(mask->mac));
1645         }
1646         break;
1647
1648     case MFS_IPV4:
1649         ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
1650                          s);
1651         break;
1652
1653     case MFS_IPV6:
1654         print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
1655         break;
1656
1657     default:
1658         NOT_REACHED();
1659     }
1660 }