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