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