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