]> git.proxmox.com Git - ovs.git/blame - lib/meta-flow.c
Update openvswitch to allow linking from C++ projects
[ovs.git] / lib / meta-flow.c
CommitLineData
6a885fd0 1/*
cb22974d 2 * Copyright (c) 2011, 2012, 2013 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
6a885fd0
BP
21#include <errno.h>
22#include <limits.h>
23#include <netinet/icmp6.h>
24#include <netinet/ip6.h>
25
26#include "classifier.h"
27#include "dynamic-string.h"
816fd533 28#include "ofp-errors.h"
6a885fd0 29#include "ofp-util.h"
4663f9e0 30#include "ovs-thread.h"
6a885fd0
BP
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 39
13751fd8
JR
40#define FLOW_U32OFS(FIELD) \
41 offsetof(struct flow, FIELD) % 4 ? -1 : offsetof(struct flow, FIELD) / 4
42
6a885fd0
BP
43#define MF_FIELD_SIZES(MEMBER) \
44 sizeof ((union mf_value *)0)->MEMBER, \
45 8 * sizeof ((union mf_value *)0)->MEMBER
46
7f98c44d
JR
47extern const struct mf_field mf_fields[MFF_N_IDS]; /* Silence a warning. */
48
49const struct mf_field mf_fields[MFF_N_IDS] = {
6a885fd0
BP
50 /* ## -------- ## */
51 /* ## metadata ## */
52 /* ## -------- ## */
53
54 {
44a7e26d 55 MFF_TUN_ID, "tun_id", "tunnel_id",
6a885fd0 56 MF_FIELD_SIZES(be64),
0bdc4bec 57 MFM_FULLY,
6a885fd0
BP
58 MFS_HEXADECIMAL,
59 MFP_NONE,
28da1f8f
BP
60 true,
61 NXM_NX_TUN_ID, "NXM_NX_TUN_ID",
2f2eb46c 62 OXM_OF_TUNNEL_ID, "OXM_OF_TUNNEL_ID",
db0b6c29
JR
63 OFPUTIL_P_NXM_OXM_ANY,
64 OFPUTIL_P_NXM_OXM_ANY,
13751fd8 65 FLOW_U32OFS(tunnel.tun_id),
4fe3445a
PS
66 }, {
67 MFF_TUN_SRC, "tun_src", NULL,
68 MF_FIELD_SIZES(be32),
0ad90c84 69 MFM_FULLY,
4fe3445a
PS
70 MFS_IPV4,
71 MFP_NONE,
0ad90c84
JR
72 true,
73 NXM_NX_TUN_IPV4_SRC, "NXM_NX_TUN_IPV4_SRC",
74 NXM_NX_TUN_IPV4_SRC, "NXM_NX_TUN_IPV4_SRC",
db0b6c29
JR
75 OFPUTIL_P_NXM_OXM_ANY,
76 OFPUTIL_P_NXM_OXM_ANY,
13751fd8 77 FLOW_U32OFS(tunnel.ip_src),
4fe3445a
PS
78 }, {
79 MFF_TUN_DST, "tun_dst", NULL,
80 MF_FIELD_SIZES(be32),
0ad90c84 81 MFM_FULLY,
4fe3445a
PS
82 MFS_IPV4,
83 MFP_NONE,
0ad90c84
JR
84 true,
85 NXM_NX_TUN_IPV4_DST, "NXM_NX_TUN_IPV4_DST",
86 NXM_NX_TUN_IPV4_DST, "NXM_NX_TUN_IPV4_DST",
db0b6c29
JR
87 OFPUTIL_P_NXM_OXM_ANY,
88 OFPUTIL_P_NXM_OXM_ANY,
13751fd8 89 FLOW_U32OFS(tunnel.ip_dst),
4fe3445a
PS
90 }, {
91 MFF_TUN_FLAGS, "tun_flags", NULL,
92 MF_FIELD_SIZES(be16),
93 MFM_NONE,
94 MFS_TNL_FLAGS,
95 MFP_NONE,
96 false,
97 0, NULL,
98 0, NULL,
db0b6c29
JR
99 OFPUTIL_P_NONE,
100 OFPUTIL_P_NONE,
13751fd8 101 -1,
4fe3445a 102 }, {
216a32dd 103 MFF_TUN_TTL, "tun_ttl", NULL,
4fe3445a
PS
104 MF_FIELD_SIZES(u8),
105 MFM_NONE,
106 MFS_DECIMAL,
107 MFP_NONE,
108 false,
109 0, NULL,
110 0, NULL,
db0b6c29
JR
111 OFPUTIL_P_NONE,
112 OFPUTIL_P_NONE,
13751fd8 113 -1,
4fe3445a 114 }, {
216a32dd 115 MFF_TUN_TOS, "tun_tos", NULL,
4fe3445a
PS
116 MF_FIELD_SIZES(u8),
117 MFM_NONE,
118 MFS_DECIMAL,
119 MFP_NONE,
120 false,
121 0, NULL,
122 0, NULL,
db0b6c29
JR
123 OFPUTIL_P_NONE,
124 OFPUTIL_P_NONE,
13751fd8 125 -1,
969fc56c
JS
126 }, {
127 MFF_METADATA, "metadata", NULL,
128 MF_FIELD_SIZES(be64),
0bdc4bec 129 MFM_FULLY,
969fc56c
JS
130 MFS_HEXADECIMAL,
131 MFP_NONE,
132 true,
133 OXM_OF_METADATA, "OXM_OF_METADATA",
134 OXM_OF_METADATA, "OXM_OF_METADATA",
db0b6c29
JR
135 OFPUTIL_P_NXM_OF11_UP,
136 OFPUTIL_P_NXM_OF11_UP,
13751fd8 137 -1,
6a885fd0
BP
138 }, {
139 MFF_IN_PORT, "in_port", NULL,
140 MF_FIELD_SIZES(be16),
0bdc4bec 141 MFM_NONE,
6a885fd0
BP
142 MFS_OFP_PORT,
143 MFP_NONE,
557323cd 144 true,
28da1f8f 145 NXM_OF_IN_PORT, "NXM_OF_IN_PORT",
72333065 146 NXM_OF_IN_PORT, "NXM_OF_IN_PORT",
db0b6c29
JR
147 OFPUTIL_P_ANY, /* OF11+ via mapping to 32 bits. */
148 OFPUTIL_P_NONE,
13751fd8 149 -1,
72333065
JR
150 }, {
151 MFF_IN_PORT_OXM, "in_port_oxm", NULL,
152 MF_FIELD_SIZES(be32),
153 MFM_NONE,
154 MFS_OFP_PORT_OXM,
155 MFP_NONE,
156 true,
157 OXM_OF_IN_PORT, "OXM_OF_IN_PORT",
36f3999b 158 OXM_OF_IN_PORT, "OXM_OF_IN_PORT",
db0b6c29
JR
159 OFPUTIL_P_OF11_UP,
160 OFPUTIL_P_NONE,
13751fd8 161 -1,
1b567fb9
AA
162 }, {
163 MFF_SKB_PRIORITY, "skb_priority", NULL,
164 MF_FIELD_SIZES(be32),
165 MFM_NONE,
166 MFS_HEXADECIMAL,
167 MFP_NONE,
168 false,
169 0, NULL,
170 0, NULL,
db0b6c29
JR
171 OFPUTIL_P_NONE,
172 OFPUTIL_P_NONE,
13751fd8 173 -1,
1b567fb9 174 }, {
1362e248 175 MFF_PKT_MARK, "pkt_mark", NULL,
1b567fb9 176 MF_FIELD_SIZES(be32),
ac923e91 177 MFM_FULLY,
1b567fb9
AA
178 MFS_HEXADECIMAL,
179 MFP_NONE,
ac923e91
JG
180 true,
181 NXM_NX_PKT_MARK, "NXM_NX_PKT_MARK",
182 NXM_NX_PKT_MARK, "NXM_NX_PKT_MARK",
db0b6c29
JR
183 OFPUTIL_P_NXM_OXM_ANY,
184 OFPUTIL_P_NXM_OXM_ANY,
13751fd8 185 -1,
6a885fd0
BP
186 },
187
188#define REGISTER(IDX) \
189 { \
190 MFF_REG##IDX, "reg" #IDX, NULL, \
191 MF_FIELD_SIZES(be32), \
0bdc4bec 192 MFM_FULLY, \
6a885fd0
BP
193 MFS_HEXADECIMAL, \
194 MFP_NONE, \
28da1f8f 195 true, \
b5e5143b
BP
196 NXM_NX_REG(IDX), "NXM_NX_REG" #IDX, \
197 NXM_NX_REG(IDX), "NXM_NX_REG" #IDX, \
db0b6c29
JR
198 OFPUTIL_P_NXM_OXM_ANY, \
199 OFPUTIL_P_NXM_OXM_ANY, \
13751fd8 200 -1, \
6a885fd0
BP
201 }
202#if FLOW_N_REGS > 0
203 REGISTER(0),
204#endif
205#if FLOW_N_REGS > 1
206 REGISTER(1),
207#endif
208#if FLOW_N_REGS > 2
209 REGISTER(2),
210#endif
211#if FLOW_N_REGS > 3
212 REGISTER(3),
213#endif
214#if FLOW_N_REGS > 4
d2c0fed9
JP
215 REGISTER(4),
216#endif
217#if FLOW_N_REGS > 5
e9358af6
EJ
218 REGISTER(5),
219#endif
220#if FLOW_N_REGS > 6
221 REGISTER(6),
222#endif
223#if FLOW_N_REGS > 7
224 REGISTER(7),
225#endif
226#if FLOW_N_REGS > 8
6a885fd0
BP
227#error
228#endif
229
230 /* ## -- ## */
231 /* ## L2 ## */
232 /* ## -- ## */
233
234 {
235 MFF_ETH_SRC, "eth_src", "dl_src",
236 MF_FIELD_SIZES(mac),
0bdc4bec 237 MFM_FULLY,
6a885fd0
BP
238 MFS_ETHERNET,
239 MFP_NONE,
28da1f8f
BP
240 true,
241 NXM_OF_ETH_SRC, "NXM_OF_ETH_SRC",
36f3999b 242 OXM_OF_ETH_SRC, "OXM_OF_ETH_SRC",
db0b6c29
JR
243 OFPUTIL_P_ANY,
244 OFPUTIL_P_NXM_OF11_UP, /* Bitwise masking only with NXM and OF11+! */
13751fd8 245 -1,
6a885fd0
BP
246 }, {
247 MFF_ETH_DST, "eth_dst", "dl_dst",
248 MF_FIELD_SIZES(mac),
0bdc4bec 249 MFM_FULLY,
6a885fd0
BP
250 MFS_ETHERNET,
251 MFP_NONE,
28da1f8f
BP
252 true,
253 NXM_OF_ETH_DST, "NXM_OF_ETH_DST",
36f3999b 254 OXM_OF_ETH_DST, "OXM_OF_ETH_DST",
db0b6c29
JR
255 OFPUTIL_P_ANY,
256 OFPUTIL_P_NXM_OF11_UP, /* Bitwise masking only with NXM and OF11+! */
13751fd8 257 -1,
6a885fd0
BP
258 }, {
259 MFF_ETH_TYPE, "eth_type", "dl_type",
260 MF_FIELD_SIZES(be16),
0bdc4bec 261 MFM_NONE,
6a885fd0
BP
262 MFS_HEXADECIMAL,
263 MFP_NONE,
28da1f8f
BP
264 false,
265 NXM_OF_ETH_TYPE, "NXM_OF_ETH_TYPE",
36f3999b 266 OXM_OF_ETH_TYPE, "OXM_OF_ETH_TYPE",
db0b6c29
JR
267 OFPUTIL_P_ANY,
268 OFPUTIL_P_NONE,
13751fd8 269 -1,
6a885fd0
BP
270 },
271
272 {
273 MFF_VLAN_TCI, "vlan_tci", NULL,
274 MF_FIELD_SIZES(be16),
0bdc4bec 275 MFM_FULLY,
6a885fd0
BP
276 MFS_HEXADECIMAL,
277 MFP_NONE,
28da1f8f
BP
278 true,
279 NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI",
b5e5143b 280 NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI",
db0b6c29
JR
281 OFPUTIL_P_ANY,
282 OFPUTIL_P_NXM_OXM_ANY,
13751fd8 283 -1,
6a885fd0 284 }, {
441c57a9 285 MFF_DL_VLAN, "dl_vlan", NULL,
6a885fd0 286 sizeof(ovs_be16), 12,
0bdc4bec 287 MFM_NONE,
6a885fd0
BP
288 MFS_DECIMAL,
289 MFP_NONE,
28da1f8f 290 true,
cc34bc8c
BP
291 0, NULL,
292 0, NULL,
db0b6c29
JR
293 OFPUTIL_P_ANY,
294 OFPUTIL_P_NXM_OXM_ANY,
13751fd8 295 -1,
cc34bc8c
BP
296 }, {
297 MFF_VLAN_VID, "vlan_vid", NULL,
298 sizeof(ovs_be16), 12,
0bdc4bec 299 MFM_FULLY,
cc34bc8c
BP
300 MFS_DECIMAL,
301 MFP_NONE,
302 true,
b5e5143b 303 OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID",
36f3999b 304 OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID",
db0b6c29
JR
305 OFPUTIL_P_ANY,
306 OFPUTIL_P_NXM_OXM_ANY,
13751fd8 307 -1,
6a885fd0 308 }, {
441c57a9 309 MFF_DL_VLAN_PCP, "dl_vlan_pcp", NULL,
6a885fd0 310 1, 3,
0bdc4bec 311 MFM_NONE,
6a885fd0
BP
312 MFS_DECIMAL,
313 MFP_NONE,
28da1f8f 314 true,
cc34bc8c
BP
315 0, NULL,
316 0, NULL,
db0b6c29
JR
317 OFPUTIL_P_ANY, /* Will be mapped to NXM and OXM. */
318 OFPUTIL_P_NONE,
13751fd8 319 -1,
cc34bc8c
BP
320 }, {
321 MFF_VLAN_PCP, "vlan_pcp", NULL,
322 1, 3,
0bdc4bec 323 MFM_NONE,
cc34bc8c 324 MFS_DECIMAL,
8069b0da 325 MFP_VLAN_VID,
cc34bc8c 326 true,
b5e5143b 327 OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP",
36f3999b 328 OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP",
db0b6c29
JR
329 OFPUTIL_P_ANY, /* Will be mapped to OF10 and NXM. */
330 OFPUTIL_P_NONE,
13751fd8 331 -1,
6a885fd0
BP
332 },
333
b02475c5
SH
334 /* ## ---- ## */
335 /* ## L2.5 ## */
336 /* ## ---- ## */
337 {
338 MFF_MPLS_LABEL, "mpls_label", NULL,
339 4, 20,
340 MFM_NONE,
341 MFS_DECIMAL,
342 MFP_MPLS,
343 true,
344 OXM_OF_MPLS_LABEL, "OXM_OF_MPLS_LABEL",
345 OXM_OF_MPLS_LABEL, "OXM_OF_MPLS_LABEL",
db0b6c29
JR
346 OFPUTIL_P_NXM_OF11_UP,
347 OFPUTIL_P_NONE,
13751fd8 348 -1,
b02475c5
SH
349 }, {
350 MFF_MPLS_TC, "mpls_tc", NULL,
351 1, 3,
352 MFM_NONE,
353 MFS_DECIMAL,
354 MFP_MPLS,
355 true,
356 OXM_OF_MPLS_TC, "OXM_OF_MPLS_TC",
357 OXM_OF_MPLS_TC, "OXM_OF_MPLS_TC",
db0b6c29
JR
358 OFPUTIL_P_NXM_OF11_UP,
359 OFPUTIL_P_NONE,
13751fd8 360 -1,
b02475c5
SH
361 }, {
362 MFF_MPLS_BOS, "mpls_bos", NULL,
363 1, 1,
364 MFM_NONE,
365 MFS_DECIMAL,
366 MFP_MPLS,
367 false,
368 OXM_OF_MPLS_BOS, "OXM_OF_MPLS_BOS",
369 OXM_OF_MPLS_BOS, "OXM_OF_MPLS_BOS",
db0b6c29
JR
370 OFPUTIL_P_NXM_OXM_ANY,
371 OFPUTIL_P_NONE,
13751fd8 372 -1,
b02475c5
SH
373 },
374
6a885fd0
BP
375 /* ## -- ## */
376 /* ## L3 ## */
377 /* ## -- ## */
378
379 {
380 MFF_IPV4_SRC, "ip_src", "nw_src",
381 MF_FIELD_SIZES(be32),
0bdc4bec 382 MFM_FULLY,
6a885fd0
BP
383 MFS_IPV4,
384 MFP_IPV4,
28da1f8f
BP
385 true,
386 NXM_OF_IP_SRC, "NXM_OF_IP_SRC",
36f3999b 387 OXM_OF_IPV4_SRC, "OXM_OF_IPV4_SRC",
db0b6c29
JR
388 OFPUTIL_P_ANY,
389 OFPUTIL_P_NXM_OF11_UP,
13751fd8 390 FLOW_U32OFS(nw_src),
6a885fd0
BP
391 }, {
392 MFF_IPV4_DST, "ip_dst", "nw_dst",
393 MF_FIELD_SIZES(be32),
0bdc4bec 394 MFM_FULLY,
6a885fd0
BP
395 MFS_IPV4,
396 MFP_IPV4,
28da1f8f
BP
397 true,
398 NXM_OF_IP_DST, "NXM_OF_IP_DST",
36f3999b 399 OXM_OF_IPV4_DST, "OXM_OF_IPV4_DST",
db0b6c29
JR
400 OFPUTIL_P_ANY,
401 OFPUTIL_P_NXM_OF11_UP,
13751fd8 402 FLOW_U32OFS(nw_dst),
6a885fd0
BP
403 },
404
405 {
406 MFF_IPV6_SRC, "ipv6_src", NULL,
407 MF_FIELD_SIZES(ipv6),
0bdc4bec 408 MFM_FULLY,
6a885fd0
BP
409 MFS_IPV6,
410 MFP_IPV6,
28da1f8f
BP
411 true,
412 NXM_NX_IPV6_SRC, "NXM_NX_IPV6_SRC",
36f3999b 413 OXM_OF_IPV6_SRC, "OXM_OF_IPV6_SRC",
db0b6c29
JR
414 OFPUTIL_P_NXM_OXM_ANY,
415 OFPUTIL_P_NXM_OXM_ANY,
13751fd8 416 FLOW_U32OFS(ipv6_src),
6a885fd0
BP
417 }, {
418 MFF_IPV6_DST, "ipv6_dst", NULL,
419 MF_FIELD_SIZES(ipv6),
0bdc4bec 420 MFM_FULLY,
6a885fd0
BP
421 MFS_IPV6,
422 MFP_IPV6,
28da1f8f
BP
423 true,
424 NXM_NX_IPV6_DST, "NXM_NX_IPV6_DST",
36f3999b 425 OXM_OF_IPV6_DST, "OXM_OF_IPV6_DST",
db0b6c29
JR
426 OFPUTIL_P_NXM_OXM_ANY,
427 OFPUTIL_P_NXM_OXM_ANY,
13751fd8 428 FLOW_U32OFS(ipv6_dst),
6a885fd0 429 },
fa8223b7
JP
430 {
431 MFF_IPV6_LABEL, "ipv6_label", NULL,
432 4, 20,
0bdc4bec 433 MFM_FULLY,
fa8223b7
JP
434 MFS_HEXADECIMAL,
435 MFP_IPV6,
28da1f8f
BP
436 false,
437 NXM_NX_IPV6_LABEL, "NXM_NX_IPV6_LABEL",
36f3999b 438 OXM_OF_IPV6_FLABEL, "OXM_OF_IPV6_FLABEL",
db0b6c29
JR
439 OFPUTIL_P_NXM_OXM_ANY,
440 OFPUTIL_P_NXM_OXM_ANY,
13751fd8 441 -1,
fa8223b7 442 },
6a885fd0
BP
443
444 {
44a7e26d 445 MFF_IP_PROTO, "nw_proto", "ip_proto",
6a885fd0 446 MF_FIELD_SIZES(u8),
0bdc4bec 447 MFM_NONE,
6a885fd0
BP
448 MFS_DECIMAL,
449 MFP_IP_ANY,
28da1f8f
BP
450 false,
451 NXM_OF_IP_PROTO, "NXM_OF_IP_PROTO",
36f3999b 452 OXM_OF_IP_PROTO, "OXM_OF_IP_PROTO",
db0b6c29
JR
453 OFPUTIL_P_ANY,
454 OFPUTIL_P_NONE,
13751fd8 455 -1,
6a885fd0 456 }, {
530180fd 457 MFF_IP_DSCP, "nw_tos", NULL,
6a885fd0 458 MF_FIELD_SIZES(u8),
0bdc4bec 459 MFM_NONE,
6a885fd0
BP
460 MFS_DECIMAL,
461 MFP_IP_ANY,
28da1f8f 462 true,
36f3999b 463 NXM_OF_IP_TOS, "NXM_OF_IP_TOS",
1638b906 464 NXM_OF_IP_TOS, "NXM_OF_IP_TOS",
db0b6c29
JR
465 OFPUTIL_P_ANY, /* Will be shifted for OXM. */
466 OFPUTIL_P_NONE,
13751fd8 467 -1,
1638b906 468 }, {
44a7e26d
JR
469 MFF_IP_DSCP_SHIFTED, "ip_dscp", NULL,
470 1, 6,
1638b906
BP
471 MFM_NONE,
472 MFS_DECIMAL,
473 MFP_IP_ANY,
474 true,
475 OXM_OF_IP_DSCP, "OXM_OF_IP_DSCP",
36f3999b 476 OXM_OF_IP_DSCP, "OXM_OF_IP_DSCP",
db0b6c29
JR
477 OFPUTIL_P_ANY, /* Will be shifted for non-OXM. */
478 OFPUTIL_P_NONE,
13751fd8 479 -1,
530180fd 480 }, {
44a7e26d 481 MFF_IP_ECN, "nw_ecn", "ip_ecn",
530180fd 482 1, 2,
0bdc4bec 483 MFM_NONE,
530180fd
JP
484 MFS_DECIMAL,
485 MFP_IP_ANY,
28da1f8f
BP
486 true,
487 NXM_NX_IP_ECN, "NXM_NX_IP_ECN",
36f3999b 488 OXM_OF_IP_ECN, "OXM_OF_IP_ECN",
db0b6c29
JR
489 OFPUTIL_P_NXM_OXM_ANY,
490 OFPUTIL_P_NONE,
13751fd8 491 -1,
a61680c6
JP
492 }, {
493 MFF_IP_TTL, "nw_ttl", NULL,
494 MF_FIELD_SIZES(u8),
0bdc4bec 495 MFM_NONE,
a61680c6
JP
496 MFS_DECIMAL,
497 MFP_IP_ANY,
28da1f8f 498 true,
36f3999b 499 NXM_NX_IP_TTL, "NXM_NX_IP_TTL",
b5e5143b 500 NXM_NX_IP_TTL, "NXM_NX_IP_TTL",
db0b6c29
JR
501 OFPUTIL_P_NXM_OXM_ANY,
502 OFPUTIL_P_NONE,
13751fd8 503 -1,
7257b535
BP
504 }, {
505 MFF_IP_FRAG, "ip_frag", NULL,
506 1, 2,
0bdc4bec 507 MFM_FULLY,
7257b535
BP
508 MFS_FRAG,
509 MFP_IP_ANY,
28da1f8f 510 false,
36f3999b 511 NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG",
b5e5143b 512 NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG",
db0b6c29
JR
513 OFPUTIL_P_NXM_OXM_ANY,
514 OFPUTIL_P_NXM_OXM_ANY,
13751fd8 515 -1,
6a885fd0
BP
516 },
517
518 {
519 MFF_ARP_OP, "arp_op", NULL,
520 MF_FIELD_SIZES(be16),
0bdc4bec 521 MFM_NONE,
6a885fd0
BP
522 MFS_DECIMAL,
523 MFP_ARP,
f6c8a6b1 524 true,
28da1f8f 525 NXM_OF_ARP_OP, "NXM_OF_ARP_OP",
36f3999b 526 OXM_OF_ARP_OP, "OXM_OF_ARP_OP",
db0b6c29
JR
527 OFPUTIL_P_ANY,
528 OFPUTIL_P_NONE,
13751fd8 529 -1,
6a885fd0
BP
530 }, {
531 MFF_ARP_SPA, "arp_spa", NULL,
532 MF_FIELD_SIZES(be32),
0bdc4bec 533 MFM_FULLY,
6a885fd0
BP
534 MFS_IPV4,
535 MFP_ARP,
f6c8a6b1 536 true,
28da1f8f 537 NXM_OF_ARP_SPA, "NXM_OF_ARP_SPA",
36f3999b 538 OXM_OF_ARP_SPA, "OXM_OF_ARP_SPA",
db0b6c29
JR
539 OFPUTIL_P_ANY,
540 OFPUTIL_P_NXM_OF11_UP,
13751fd8 541 -1,
6a885fd0
BP
542 }, {
543 MFF_ARP_TPA, "arp_tpa", NULL,
544 MF_FIELD_SIZES(be32),
0bdc4bec 545 MFM_FULLY,
6a885fd0
BP
546 MFS_IPV4,
547 MFP_ARP,
f6c8a6b1 548 true,
28da1f8f 549 NXM_OF_ARP_TPA, "NXM_OF_ARP_TPA",
36f3999b 550 OXM_OF_ARP_TPA, "OXM_OF_ARP_TPA",
db0b6c29
JR
551 OFPUTIL_P_ANY,
552 OFPUTIL_P_NXM_OF11_UP,
13751fd8 553 -1,
6a885fd0
BP
554 }, {
555 MFF_ARP_SHA, "arp_sha", NULL,
556 MF_FIELD_SIZES(mac),
0bdc4bec 557 MFM_FULLY,
6a885fd0
BP
558 MFS_ETHERNET,
559 MFP_ARP,
f6c8a6b1 560 true,
28da1f8f 561 NXM_NX_ARP_SHA, "NXM_NX_ARP_SHA",
36f3999b 562 OXM_OF_ARP_SHA, "OXM_OF_ARP_SHA",
db0b6c29
JR
563 OFPUTIL_P_NXM_OXM_ANY,
564 OFPUTIL_P_NXM_OXM_ANY,
13751fd8 565 -1,
6a885fd0
BP
566 }, {
567 MFF_ARP_THA, "arp_tha", NULL,
568 MF_FIELD_SIZES(mac),
0bdc4bec 569 MFM_FULLY,
6a885fd0
BP
570 MFS_ETHERNET,
571 MFP_ARP,
f6c8a6b1 572 true,
28da1f8f 573 NXM_NX_ARP_THA, "NXM_NX_ARP_THA",
36f3999b 574 OXM_OF_ARP_THA, "OXM_OF_ARP_THA",
db0b6c29
JR
575 OFPUTIL_P_NXM_OXM_ANY,
576 OFPUTIL_P_NXM_OXM_ANY,
13751fd8 577 -1,
6a885fd0
BP
578 },
579
580 /* ## -- ## */
581 /* ## L4 ## */
582 /* ## -- ## */
583
584 {
585 MFF_TCP_SRC, "tcp_src", "tp_src",
586 MF_FIELD_SIZES(be16),
0bdc4bec 587 MFM_FULLY,
6a885fd0
BP
588 MFS_DECIMAL,
589 MFP_TCP,
28da1f8f
BP
590 true,
591 NXM_OF_TCP_SRC, "NXM_OF_TCP_SRC",
36f3999b 592 OXM_OF_TCP_SRC, "OXM_OF_TCP_SRC",
db0b6c29
JR
593 OFPUTIL_P_ANY,
594 OFPUTIL_P_NXM_OXM_ANY,
13751fd8 595 -1,
6a885fd0
BP
596 }, {
597 MFF_TCP_DST, "tcp_dst", "tp_dst",
598 MF_FIELD_SIZES(be16),
0bdc4bec 599 MFM_FULLY,
6a885fd0
BP
600 MFS_DECIMAL,
601 MFP_TCP,
28da1f8f
BP
602 true,
603 NXM_OF_TCP_DST, "NXM_OF_TCP_DST",
36f3999b 604 OXM_OF_TCP_DST, "OXM_OF_TCP_DST",
db0b6c29
JR
605 OFPUTIL_P_ANY,
606 OFPUTIL_P_NXM_OXM_ANY,
13751fd8 607 -1,
dc235f7f
JR
608 }, {
609 MFF_TCP_FLAGS, "tcp_flags", NULL,
610 2, 12,
611 MFM_FULLY,
61bf6666 612 MFS_TCP_FLAGS,
dc235f7f
JR
613 MFP_TCP,
614 false,
615 NXM_NX_TCP_FLAGS, "NXM_NX_TCP_FLAGS",
616 NXM_NX_TCP_FLAGS, "NXM_NX_TCP_FLAGS",
617 OFPUTIL_P_NXM_OXM_ANY,
618 OFPUTIL_P_NXM_OXM_ANY,
13751fd8 619 -1,
6a885fd0
BP
620 },
621
622 {
623 MFF_UDP_SRC, "udp_src", NULL,
624 MF_FIELD_SIZES(be16),
0bdc4bec 625 MFM_FULLY,
6a885fd0
BP
626 MFS_DECIMAL,
627 MFP_UDP,
28da1f8f
BP
628 true,
629 NXM_OF_UDP_SRC, "NXM_OF_UDP_SRC",
36f3999b 630 OXM_OF_UDP_SRC, "OXM_OF_UDP_SRC",
db0b6c29
JR
631 OFPUTIL_P_ANY,
632 OFPUTIL_P_NXM_OXM_ANY,
13751fd8 633 -1,
6a885fd0
BP
634 }, {
635 MFF_UDP_DST, "udp_dst", NULL,
636 MF_FIELD_SIZES(be16),
0bdc4bec 637 MFM_FULLY,
6a885fd0
BP
638 MFS_DECIMAL,
639 MFP_UDP,
28da1f8f
BP
640 true,
641 NXM_OF_UDP_DST, "NXM_OF_UDP_DST",
36f3999b 642 OXM_OF_UDP_DST, "OXM_OF_UDP_DST",
db0b6c29
JR
643 OFPUTIL_P_ANY,
644 OFPUTIL_P_NXM_OXM_ANY,
13751fd8 645 -1,
6a885fd0
BP
646 },
647
0d56eaf2
JS
648 {
649 MFF_SCTP_SRC, "sctp_src", NULL,
650 MF_FIELD_SIZES(be16),
651 MFM_FULLY,
652 MFS_DECIMAL,
653 MFP_SCTP,
654 true,
655 OXM_OF_SCTP_SRC, "OXM_OF_SCTP_SRC",
656 OXM_OF_SCTP_SRC, "OXM_OF_SCTP_SRC",
db0b6c29
JR
657 OFPUTIL_P_NXM_OF11_UP,
658 OFPUTIL_P_NXM_OXM_ANY,
13751fd8 659 -1,
0d56eaf2
JS
660 }, {
661 MFF_SCTP_DST, "sctp_dst", NULL,
662 MF_FIELD_SIZES(be16),
663 MFM_FULLY,
664 MFS_DECIMAL,
665 MFP_SCTP,
666 true,
667 OXM_OF_SCTP_DST, "OXM_OF_SCTP_DST",
668 OXM_OF_SCTP_DST, "OXM_OF_SCTP_DST",
db0b6c29
JR
669 OFPUTIL_P_NXM_OF11_UP,
670 OFPUTIL_P_NXM_OXM_ANY,
13751fd8 671 -1,
0d56eaf2
JS
672 },
673
6a885fd0 674 {
268a95e0 675 MFF_ICMPV4_TYPE, "icmp_type", NULL,
6a885fd0 676 MF_FIELD_SIZES(u8),
0bdc4bec 677 MFM_NONE,
6a885fd0 678 MFS_DECIMAL,
268a95e0 679 MFP_ICMPV4,
28da1f8f
BP
680 false,
681 NXM_OF_ICMP_TYPE, "NXM_OF_ICMP_TYPE",
36f3999b 682 OXM_OF_ICMPV4_TYPE, "OXM_OF_ICMPV4_TYPE",
db0b6c29
JR
683 OFPUTIL_P_ANY,
684 OFPUTIL_P_NONE,
13751fd8 685 -1,
6a885fd0 686 }, {
268a95e0 687 MFF_ICMPV4_CODE, "icmp_code", NULL,
6a885fd0 688 MF_FIELD_SIZES(u8),
0bdc4bec 689 MFM_NONE,
6a885fd0 690 MFS_DECIMAL,
268a95e0 691 MFP_ICMPV4,
28da1f8f
BP
692 false,
693 NXM_OF_ICMP_CODE, "NXM_OF_ICMP_CODE",
36f3999b 694 OXM_OF_ICMPV4_CODE, "OXM_OF_ICMPV4_CODE",
db0b6c29
JR
695 OFPUTIL_P_ANY,
696 OFPUTIL_P_NONE,
13751fd8 697 -1,
28da1f8f
BP
698 },
699
700 {
268a95e0
BP
701 MFF_ICMPV6_TYPE, "icmpv6_type", NULL,
702 MF_FIELD_SIZES(u8),
0bdc4bec 703 MFM_NONE,
268a95e0
BP
704 MFS_DECIMAL,
705 MFP_ICMPV6,
28da1f8f
BP
706 false,
707 NXM_NX_ICMPV6_TYPE, "NXM_NX_ICMPV6_TYPE",
36f3999b 708 OXM_OF_ICMPV6_TYPE, "OXM_OF_ICMPV6_TYPE",
db0b6c29
JR
709 OFPUTIL_P_NXM_OXM_ANY,
710 OFPUTIL_P_NONE,
13751fd8 711 -1,
268a95e0
BP
712 }, {
713 MFF_ICMPV6_CODE, "icmpv6_code", NULL,
714 MF_FIELD_SIZES(u8),
0bdc4bec 715 MFM_NONE,
268a95e0
BP
716 MFS_DECIMAL,
717 MFP_ICMPV6,
28da1f8f
BP
718 false,
719 NXM_NX_ICMPV6_CODE, "NXM_NX_ICMPV6_CODE",
36f3999b 720 OXM_OF_ICMPV6_CODE, "OXM_OF_ICMPV6_CODE",
db0b6c29
JR
721 OFPUTIL_P_NXM_OXM_ANY,
722 OFPUTIL_P_NONE,
13751fd8 723 -1,
6a885fd0
BP
724 },
725
726 /* ## ---- ## */
727 /* ## L"5" ## */
728 /* ## ---- ## */
729
730 {
731 MFF_ND_TARGET, "nd_target", NULL,
732 MF_FIELD_SIZES(ipv6),
0bdc4bec 733 MFM_FULLY,
6a885fd0
BP
734 MFS_IPV6,
735 MFP_ND,
28da1f8f
BP
736 false,
737 NXM_NX_ND_TARGET, "NXM_NX_ND_TARGET",
36f3999b 738 OXM_OF_IPV6_ND_TARGET, "OXM_OF_IPV6_ND_TARGET",
db0b6c29
JR
739 OFPUTIL_P_NXM_OXM_ANY,
740 OFPUTIL_P_NXM_OXM_ANY,
13751fd8 741 -1,
6a885fd0
BP
742 }, {
743 MFF_ND_SLL, "nd_sll", NULL,
744 MF_FIELD_SIZES(mac),
0bdc4bec 745 MFM_FULLY,
6a885fd0
BP
746 MFS_ETHERNET,
747 MFP_ND_SOLICIT,
28da1f8f
BP
748 false,
749 NXM_NX_ND_SLL, "NXM_NX_ND_SLL",
36f3999b 750 OXM_OF_IPV6_ND_SLL, "OXM_OF_IPV6_ND_SLL",
db0b6c29
JR
751 OFPUTIL_P_NXM_OXM_ANY,
752 OFPUTIL_P_NXM_OXM_ANY,
13751fd8 753 -1,
6a885fd0
BP
754 }, {
755 MFF_ND_TLL, "nd_tll", NULL,
756 MF_FIELD_SIZES(mac),
0bdc4bec 757 MFM_FULLY,
6a885fd0
BP
758 MFS_ETHERNET,
759 MFP_ND_ADVERT,
28da1f8f
BP
760 false,
761 NXM_NX_ND_TLL, "NXM_NX_ND_TLL",
36f3999b 762 OXM_OF_IPV6_ND_TLL, "OXM_OF_IPV6_ND_TLL",
db0b6c29
JR
763 OFPUTIL_P_NXM_OXM_ANY,
764 OFPUTIL_P_NXM_OXM_ANY,
13751fd8 765 -1,
6a885fd0
BP
766 }
767};
768
b5e5143b 769/* Maps an NXM or OXM header value to an mf_field. */
28da1f8f 770struct nxm_field {
b5e5143b
BP
771 struct hmap_node hmap_node; /* In 'all_fields' hmap. */
772 uint32_t header; /* NXM or OXM header value. */
28da1f8f
BP
773 const struct mf_field *mf;
774};
775
b5e5143b 776/* Contains 'struct nxm_field's. */
4663f9e0
BP
777static struct hmap all_fields;
778
779/* Maps from an mf_field's 'name' or 'extra_name' to the mf_field. */
780static struct shash mf_by_name;
28da1f8f 781
816fd533
BP
782/* Rate limit for parse errors. These always indicate a bug in an OpenFlow
783 * controller and so there's not much point in showing a lot of them. */
784static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
785
b5e5143b 786const struct mf_field *mf_from_nxm_header__(uint32_t header);
4663f9e0 787static void nxm_init(void);
b5e5143b 788
6a885fd0
BP
789/* Returns the field with the given 'name', or a null pointer if no field has
790 * that name. */
791const struct mf_field *
792mf_from_name(const char *name)
793{
4663f9e0 794 nxm_init();
6a885fd0
BP
795 return shash_find_data(&mf_by_name, name);
796}
797
28da1f8f 798static void
b5e5143b 799add_nxm_field(uint32_t header, const struct mf_field *mf)
28da1f8f
BP
800{
801 struct nxm_field *f;
802
803 f = xmalloc(sizeof *f);
b5e5143b
BP
804 hmap_insert(&all_fields, &f->hmap_node, hash_int(header, 0));
805 f->header = header;
28da1f8f
BP
806 f->mf = mf;
807}
808
b5ae8913
SH
809static void
810nxm_init_add_field(const struct mf_field *mf, uint32_t header)
811{
b5e5143b 812 if (header) {
cb22974d 813 ovs_assert(!mf_from_nxm_header__(header));
b5e5143b
BP
814 add_nxm_field(header, mf);
815 if (mf->maskable != MFM_NONE) {
816 add_nxm_field(NXM_MAKE_WILD_HEADER(header), mf);
817 }
b5ae8913 818 }
b5ae8913 819}
b5ae8913 820
28da1f8f 821static void
4663f9e0 822nxm_do_init(void)
28da1f8f 823{
216a32dd 824 int i;
28da1f8f 825
4663f9e0
BP
826 hmap_init(&all_fields);
827 shash_init(&mf_by_name);
216a32dd
JR
828 for (i = 0; i < MFF_N_IDS; i++) {
829 const struct mf_field *mf = &mf_fields[i];
830
831 ovs_assert(mf->id == i); /* Fields must be in the enum order. */
832
b5ae8913 833 nxm_init_add_field(mf, mf->nxm_header);
b5e5143b
BP
834 if (mf->oxm_header != mf->nxm_header) {
835 nxm_init_add_field(mf, mf->oxm_header);
836 }
4663f9e0
BP
837
838 shash_add_once(&mf_by_name, mf->name, mf);
839 if (mf->extra_name) {
840 shash_add_once(&mf_by_name, mf->extra_name, mf);
841 }
28da1f8f 842 }
28da1f8f
BP
843}
844
4663f9e0
BP
845static void
846nxm_init(void)
847{
848 static pthread_once_t once = PTHREAD_ONCE_INIT;
849 pthread_once(&once, nxm_do_init);
850}
851
28da1f8f
BP
852const struct mf_field *
853mf_from_nxm_header(uint32_t header)
854{
4663f9e0 855 nxm_init();
b5e5143b
BP
856 return mf_from_nxm_header__(header);
857}
28da1f8f 858
b5e5143b
BP
859const struct mf_field *
860mf_from_nxm_header__(uint32_t header)
861{
862 const struct nxm_field *f;
863
864 HMAP_FOR_EACH_IN_BUCKET (f, hmap_node, hash_int(header, 0), &all_fields) {
865 if (f->header == header) {
28da1f8f
BP
866 return f->mf;
867 }
868 }
869
870 return NULL;
871}
872
6a885fd0
BP
873/* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
874 * specifies at least one bit in the field.
875 *
876 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
877 * meets 'mf''s prerequisites. */
878bool
879mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
880{
881 switch (mf->id) {
4fe3445a 882 case MFF_TUN_SRC:
0ad90c84 883 return !wc->masks.tunnel.ip_src;
4fe3445a 884 case MFF_TUN_DST:
0ad90c84
JR
885 return !wc->masks.tunnel.ip_dst;
886 case MFF_TUN_ID:
4fe3445a
PS
887 case MFF_TUN_TOS:
888 case MFF_TUN_TTL:
889 case MFF_TUN_FLAGS:
296e07ac 890 return !wc->masks.tunnel.tun_id;
969fc56c 891 case MFF_METADATA:
26720e24 892 return !wc->masks.metadata;
0bdc4bec 893 case MFF_IN_PORT:
72333065 894 case MFF_IN_PORT_OXM:
4e022ec0 895 return !wc->masks.in_port.ofp_port;
1b567fb9
AA
896 case MFF_SKB_PRIORITY:
897 return !wc->masks.skb_priority;
1362e248
JG
898 case MFF_PKT_MARK:
899 return !wc->masks.pkt_mark;
0d7e2fe4 900 CASE_MFF_REGS:
26720e24 901 return !wc->masks.regs[mf->id - MFF_REG0];
6a885fd0 902
73c0ce34 903 case MFF_ETH_SRC:
26720e24 904 return eth_addr_is_zero(wc->masks.dl_src);
6a885fd0 905 case MFF_ETH_DST:
26720e24 906 return eth_addr_is_zero(wc->masks.dl_dst);
e2170cff 907 case MFF_ETH_TYPE:
26720e24 908 return !wc->masks.dl_type;
6a885fd0 909
e878338b
SH
910 case MFF_ARP_SHA:
911 case MFF_ND_SLL:
26720e24 912 return eth_addr_is_zero(wc->masks.arp_sha);
e878338b
SH
913
914 case MFF_ARP_THA:
915 case MFF_ND_TLL:
26720e24 916 return eth_addr_is_zero(wc->masks.arp_tha);
e878338b 917
6a885fd0 918 case MFF_VLAN_TCI:
26720e24 919 return !wc->masks.vlan_tci;
441c57a9 920 case MFF_DL_VLAN:
26720e24 921 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK));
cc34bc8c 922 case MFF_VLAN_VID:
26720e24 923 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI));
441c57a9 924 case MFF_DL_VLAN_PCP:
cc34bc8c 925 case MFF_VLAN_PCP:
26720e24 926 return !(wc->masks.vlan_tci & htons(VLAN_PCP_MASK));
6a885fd0 927
b02475c5
SH
928 case MFF_MPLS_LABEL:
929 return !(wc->masks.mpls_lse & htonl(MPLS_LABEL_MASK));
930 case MFF_MPLS_TC:
931 return !(wc->masks.mpls_lse & htonl(MPLS_TC_MASK));
932 case MFF_MPLS_BOS:
933 return !(wc->masks.mpls_lse & htonl(MPLS_BOS_MASK));
934
6a885fd0 935 case MFF_IPV4_SRC:
26720e24 936 return !wc->masks.nw_src;
6a885fd0 937 case MFF_IPV4_DST:
26720e24 938 return !wc->masks.nw_dst;
6a885fd0
BP
939
940 case MFF_IPV6_SRC:
26720e24 941 return ipv6_mask_is_any(&wc->masks.ipv6_src);
6a885fd0 942 case MFF_IPV6_DST:
26720e24 943 return ipv6_mask_is_any(&wc->masks.ipv6_dst);
6a885fd0 944
32455024 945 case MFF_IPV6_LABEL:
26720e24 946 return !wc->masks.ipv6_label;
32455024 947
851d3105 948 case MFF_IP_PROTO:
26720e24 949 return !wc->masks.nw_proto;
5d9499c4 950 case MFF_IP_DSCP:
1638b906 951 case MFF_IP_DSCP_SHIFTED:
26720e24 952 return !(wc->masks.nw_tos & IP_DSCP_MASK);
5d9499c4 953 case MFF_IP_ECN:
26720e24 954 return !(wc->masks.nw_tos & IP_ECN_MASK);
3840c406 955 case MFF_IP_TTL:
26720e24 956 return !wc->masks.nw_ttl;
5d9499c4 957
47284b1f 958 case MFF_ND_TARGET:
26720e24 959 return ipv6_mask_is_any(&wc->masks.nd_target);
47284b1f 960
7257b535 961 case MFF_IP_FRAG:
26720e24 962 return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
7257b535 963
851d3105 964 case MFF_ARP_OP:
26720e24 965 return !wc->masks.nw_proto;
6a885fd0 966 case MFF_ARP_SPA:
26720e24 967 return !wc->masks.nw_src;
6a885fd0 968 case MFF_ARP_TPA:
26720e24 969 return !wc->masks.nw_dst;
6a885fd0 970
73f33563
BP
971 case MFF_TCP_SRC:
972 case MFF_UDP_SRC:
0d56eaf2 973 case MFF_SCTP_SRC:
73f33563
BP
974 case MFF_ICMPV4_TYPE:
975 case MFF_ICMPV6_TYPE:
26720e24 976 return !wc->masks.tp_src;
73f33563
BP
977 case MFF_TCP_DST:
978 case MFF_UDP_DST:
0d56eaf2 979 case MFF_SCTP_DST:
73f33563
BP
980 case MFF_ICMPV4_CODE:
981 case MFF_ICMPV6_CODE:
26720e24 982 return !wc->masks.tp_dst;
dc235f7f
JR
983 case MFF_TCP_FLAGS:
984 return !wc->masks.tcp_flags;
73f33563 985
6a885fd0
BP
986 case MFF_N_IDS:
987 default:
988 NOT_REACHED();
989 }
990}
991
992/* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
993 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
994 * purposes, or to 0 if it is wildcarded.
995 *
996 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
997 * meets 'mf''s prerequisites. */
998void
999mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
1000 union mf_value *mask)
1001{
fb15feb0 1002 mf_get_value(mf, &wc->masks, mask);
6a885fd0
BP
1003}
1004
1005/* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
1006 * if the mask is valid, false otherwise. */
1007bool
1008mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
1009{
1010 switch (mf->maskable) {
1011 case MFM_NONE:
1012 return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
1013 is_all_ones((const uint8_t *) mask, mf->n_bytes));
1014
1015 case MFM_FULLY:
1016 return true;
6a885fd0
BP
1017 }
1018
1019 NOT_REACHED();
1020}
1021
6a885fd0
BP
1022static bool
1023is_icmpv4(const struct flow *flow)
1024{
1025 return (flow->dl_type == htons(ETH_TYPE_IP)
1026 && flow->nw_proto == IPPROTO_ICMP);
1027}
1028
1029static bool
1030is_icmpv6(const struct flow *flow)
1031{
1032 return (flow->dl_type == htons(ETH_TYPE_IPV6)
1033 && flow->nw_proto == IPPROTO_ICMPV6);
1034}
1035
1036/* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
1037bool
1038mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
1039{
1040 switch (mf->prereqs) {
1041 case MFP_NONE:
1042 return true;
1043
1044 case MFP_ARP:
8087f5ff
MM
1045 return (flow->dl_type == htons(ETH_TYPE_ARP) ||
1046 flow->dl_type == htons(ETH_TYPE_RARP));
6a885fd0
BP
1047 case MFP_IPV4:
1048 return flow->dl_type == htons(ETH_TYPE_IP);
1049 case MFP_IPV6:
1050 return flow->dl_type == htons(ETH_TYPE_IPV6);
8069b0da 1051 case MFP_VLAN_VID:
5921e99a 1052 return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
b02475c5
SH
1053 case MFP_MPLS:
1054 return eth_type_mpls(flow->dl_type);
6a885fd0
BP
1055 case MFP_IP_ANY:
1056 return is_ip_any(flow);
1057
1058 case MFP_TCP:
1059 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
1060 case MFP_UDP:
1061 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
0d56eaf2
JS
1062 case MFP_SCTP:
1063 return is_ip_any(flow) && flow->nw_proto == IPPROTO_SCTP;
268a95e0
BP
1064 case MFP_ICMPV4:
1065 return is_icmpv4(flow);
6a885fd0
BP
1066 case MFP_ICMPV6:
1067 return is_icmpv6(flow);
6a885fd0
BP
1068
1069 case MFP_ND:
1070 return (is_icmpv6(flow)
3ee8a9f0
BP
1071 && flow->tp_dst == htons(0)
1072 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
1073 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
6a885fd0
BP
1074 case MFP_ND_SOLICIT:
1075 return (is_icmpv6(flow)
3ee8a9f0
BP
1076 && flow->tp_dst == htons(0)
1077 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
6a885fd0
BP
1078 case MFP_ND_ADVERT:
1079 return (is_icmpv6(flow)
3ee8a9f0
BP
1080 && flow->tp_dst == htons(0)
1081 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
6a885fd0
BP
1082 }
1083
1084 NOT_REACHED();
1085}
1086
b283836c
JR
1087/* Set field and it's prerequisities in the mask.
1088 * This is only ever called for writeable 'mf's, but we do not make the
1089 * distinction here. */
1090void
1091mf_mask_field_and_prereqs(const struct mf_field *mf, struct flow *mask)
1092{
1093 static const union mf_value exact_match_mask = MF_EXACT_MASK_INITIALIZER;
1094
1095 mf_set_flow_value(mf, &exact_match_mask, mask);
1096
1097 switch (mf->prereqs) {
1098 case MFP_ND:
1099 case MFP_ND_SOLICIT:
1100 case MFP_ND_ADVERT:
1101 mask->tp_src = OVS_BE16_MAX;
1102 mask->tp_dst = OVS_BE16_MAX;
1103 /* Fall through. */
1104 case MFP_TCP:
1105 case MFP_UDP:
1106 case MFP_SCTP:
1107 case MFP_ICMPV4:
1108 case MFP_ICMPV6:
1109 mask->nw_proto = 0xff;
1110 /* Fall through. */
1111 case MFP_ARP:
1112 case MFP_IPV4:
1113 case MFP_IPV6:
1114 case MFP_MPLS:
1115 case MFP_IP_ANY:
1116 mask->dl_type = OVS_BE16_MAX;
1117 break;
1118 case MFP_VLAN_VID:
1119 mask->vlan_tci |= htons(VLAN_CFI);
1120 break;
1121 case MFP_NONE:
1122 break;
1123 }
1124}
1125
1126
6a885fd0
BP
1127/* Returns true if 'value' may be a valid value *as part of a masked match*,
1128 * false otherwise.
1129 *
1130 * A value is not rejected just because it is not valid for the field in
1131 * question, but only if it doesn't make sense to test the bits in question at
1132 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
1133 * without the VLAN_CFI bit being set, but we can't reject those values because
1134 * it is still legitimate to test just for those bits (see the documentation
1135 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
530180fd 1136 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
6a885fd0
BP
1137bool
1138mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
1139{
1140 switch (mf->id) {
1141 case MFF_TUN_ID:
4fe3445a
PS
1142 case MFF_TUN_SRC:
1143 case MFF_TUN_DST:
1144 case MFF_TUN_TOS:
1145 case MFF_TUN_TTL:
1146 case MFF_TUN_FLAGS:
969fc56c 1147 case MFF_METADATA:
6a885fd0 1148 case MFF_IN_PORT:
1b567fb9 1149 case MFF_SKB_PRIORITY:
1362e248 1150 case MFF_PKT_MARK:
0d7e2fe4 1151 CASE_MFF_REGS:
6a885fd0
BP
1152 case MFF_ETH_SRC:
1153 case MFF_ETH_DST:
1154 case MFF_ETH_TYPE:
1155 case MFF_VLAN_TCI:
1156 case MFF_IPV4_SRC:
1157 case MFF_IPV4_DST:
1158 case MFF_IPV6_SRC:
1159 case MFF_IPV6_DST:
1160 case MFF_IP_PROTO:
a61680c6 1161 case MFF_IP_TTL:
6a885fd0
BP
1162 case MFF_ARP_SPA:
1163 case MFF_ARP_TPA:
1164 case MFF_ARP_SHA:
1165 case MFF_ARP_THA:
1166 case MFF_TCP_SRC:
1167 case MFF_TCP_DST:
1168 case MFF_UDP_SRC:
1169 case MFF_UDP_DST:
0d56eaf2
JS
1170 case MFF_SCTP_SRC:
1171 case MFF_SCTP_DST:
268a95e0
BP
1172 case MFF_ICMPV4_TYPE:
1173 case MFF_ICMPV4_CODE:
1174 case MFF_ICMPV6_TYPE:
1175 case MFF_ICMPV6_CODE:
6a885fd0
BP
1176 case MFF_ND_TARGET:
1177 case MFF_ND_SLL:
1178 case MFF_ND_TLL:
1179 return true;
1180
72333065 1181 case MFF_IN_PORT_OXM: {
4e022ec0 1182 ofp_port_t port;
72333065
JR
1183 return !ofputil_port_from_ofp11(value->be32, &port);
1184 }
1185
530180fd 1186 case MFF_IP_DSCP:
7257b535 1187 return !(value->u8 & ~IP_DSCP_MASK);
1638b906
BP
1188 case MFF_IP_DSCP_SHIFTED:
1189 return !(value->u8 & (~IP_DSCP_MASK >> 2));
530180fd
JP
1190 case MFF_IP_ECN:
1191 return !(value->u8 & ~IP_ECN_MASK);
7257b535 1192 case MFF_IP_FRAG:
eadef313 1193 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
dc235f7f
JR
1194 case MFF_TCP_FLAGS:
1195 return !(value->be16 & ~htons(0x0fff));
6a885fd0
BP
1196
1197 case MFF_ARP_OP:
1198 return !(value->be16 & htons(0xff00));
1199
441c57a9 1200 case MFF_DL_VLAN:
6a885fd0 1201 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
cc34bc8c
BP
1202 case MFF_VLAN_VID:
1203 return !(value->be16 & htons(VLAN_PCP_MASK));
6a885fd0 1204
441c57a9 1205 case MFF_DL_VLAN_PCP:
cc34bc8c 1206 case MFF_VLAN_PCP:
24016f5a 1207 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
6a885fd0 1208
fa8223b7
JP
1209 case MFF_IPV6_LABEL:
1210 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
1211
b02475c5
SH
1212 case MFF_MPLS_LABEL:
1213 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
1214
1215 case MFF_MPLS_TC:
1216 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
1217
1218 case MFF_MPLS_BOS:
1219 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
1220
6a885fd0
BP
1221 case MFF_N_IDS:
1222 default:
1223 NOT_REACHED();
1224 }
1225}
1226
1227/* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
1228 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
1229void
1230mf_get_value(const struct mf_field *mf, const struct flow *flow,
1231 union mf_value *value)
1232{
1233 switch (mf->id) {
1234 case MFF_TUN_ID:
296e07ac 1235 value->be64 = flow->tunnel.tun_id;
6a885fd0 1236 break;
4fe3445a
PS
1237 case MFF_TUN_SRC:
1238 value->be32 = flow->tunnel.ip_src;
1239 break;
1240 case MFF_TUN_DST:
1241 value->be32 = flow->tunnel.ip_dst;
1242 break;
1243 case MFF_TUN_FLAGS:
1244 value->be16 = htons(flow->tunnel.flags);
1245 break;
1246 case MFF_TUN_TTL:
1247 value->u8 = flow->tunnel.ip_ttl;
1248 break;
1249 case MFF_TUN_TOS:
1250 value->u8 = flow->tunnel.ip_tos;
1251 break;
1252
969fc56c
JS
1253 case MFF_METADATA:
1254 value->be64 = flow->metadata;
1255 break;
6a885fd0
BP
1256
1257 case MFF_IN_PORT:
4e022ec0 1258 value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
6a885fd0 1259 break;
72333065 1260 case MFF_IN_PORT_OXM:
4e022ec0 1261 value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
72333065
JR
1262 break;
1263
1b567fb9 1264 case MFF_SKB_PRIORITY:
a81f0a44 1265 value->be32 = htonl(flow->skb_priority);
1b567fb9
AA
1266 break;
1267
1362e248
JG
1268 case MFF_PKT_MARK:
1269 value->be32 = htonl(flow->pkt_mark);
1b567fb9
AA
1270 break;
1271
0d7e2fe4 1272 CASE_MFF_REGS:
2f98b0b7 1273 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
6a885fd0
BP
1274 break;
1275
1276 case MFF_ETH_SRC:
1277 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
1278 break;
1279
1280 case MFF_ETH_DST:
1281 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
1282 break;
1283
1284 case MFF_ETH_TYPE:
1285 value->be16 = flow->dl_type;
1286 break;
1287
1288 case MFF_VLAN_TCI:
1289 value->be16 = flow->vlan_tci;
1290 break;
1291
441c57a9 1292 case MFF_DL_VLAN:
6a885fd0
BP
1293 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
1294 break;
cc34bc8c
BP
1295 case MFF_VLAN_VID:
1296 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
1297 break;
6a885fd0 1298
441c57a9 1299 case MFF_DL_VLAN_PCP:
cc34bc8c 1300 case MFF_VLAN_PCP:
6a885fd0
BP
1301 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
1302 break;
1303
b02475c5
SH
1304 case MFF_MPLS_LABEL:
1305 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse));
1306 break;
1307
1308 case MFF_MPLS_TC:
1309 value->u8 = mpls_lse_to_tc(flow->mpls_lse);
1310 break;
1311
1312 case MFF_MPLS_BOS:
1313 value->u8 = mpls_lse_to_bos(flow->mpls_lse);
1314 break;
1315
6a885fd0
BP
1316 case MFF_IPV4_SRC:
1317 value->be32 = flow->nw_src;
1318 break;
1319
1320 case MFF_IPV4_DST:
1321 value->be32 = flow->nw_dst;
1322 break;
1323
1324 case MFF_IPV6_SRC:
1325 value->ipv6 = flow->ipv6_src;
1326 break;
1327
1328 case MFF_IPV6_DST:
1329 value->ipv6 = flow->ipv6_dst;
1330 break;
1331
fa8223b7
JP
1332 case MFF_IPV6_LABEL:
1333 value->be32 = flow->ipv6_label;
1334 break;
1335
6a885fd0
BP
1336 case MFF_IP_PROTO:
1337 value->u8 = flow->nw_proto;
1338 break;
1339
530180fd 1340 case MFF_IP_DSCP:
eadef313 1341 value->u8 = flow->nw_tos & IP_DSCP_MASK;
7257b535
BP
1342 break;
1343
1638b906
BP
1344 case MFF_IP_DSCP_SHIFTED:
1345 value->u8 = flow->nw_tos >> 2;
1346 break;
1347
530180fd 1348 case MFF_IP_ECN:
eadef313 1349 value->u8 = flow->nw_tos & IP_ECN_MASK;
530180fd
JP
1350 break;
1351
a61680c6
JP
1352 case MFF_IP_TTL:
1353 value->u8 = flow->nw_ttl;
1354 break;
1355
7257b535 1356 case MFF_IP_FRAG:
eadef313 1357 value->u8 = flow->nw_frag;
6a885fd0
BP
1358 break;
1359
1360 case MFF_ARP_OP:
1361 value->be16 = htons(flow->nw_proto);
1362 break;
1363
1364 case MFF_ARP_SPA:
1365 value->be32 = flow->nw_src;
1366 break;
1367
1368 case MFF_ARP_TPA:
1369 value->be32 = flow->nw_dst;
1370 break;
1371
1372 case MFF_ARP_SHA:
1373 case MFF_ND_SLL:
1374 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
1375 break;
1376
1377 case MFF_ARP_THA:
1378 case MFF_ND_TLL:
1379 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
1380 break;
1381
1382 case MFF_TCP_SRC:
6a885fd0 1383 case MFF_UDP_SRC:
0d56eaf2 1384 case MFF_SCTP_SRC:
6a885fd0
BP
1385 value->be16 = flow->tp_src;
1386 break;
1387
71baec06 1388 case MFF_TCP_DST:
6a885fd0 1389 case MFF_UDP_DST:
0d56eaf2 1390 case MFF_SCTP_DST:
6a885fd0
BP
1391 value->be16 = flow->tp_dst;
1392 break;
1393
dc235f7f
JR
1394 case MFF_TCP_FLAGS:
1395 value->be16 = flow->tcp_flags;
1396 break;
1397
268a95e0
BP
1398 case MFF_ICMPV4_TYPE:
1399 case MFF_ICMPV6_TYPE:
6a885fd0
BP
1400 value->u8 = ntohs(flow->tp_src);
1401 break;
1402
268a95e0
BP
1403 case MFF_ICMPV4_CODE:
1404 case MFF_ICMPV6_CODE:
6a885fd0
BP
1405 value->u8 = ntohs(flow->tp_dst);
1406 break;
1407
1408 case MFF_ND_TARGET:
1409 value->ipv6 = flow->nd_target;
1410 break;
1411
1412 case MFF_N_IDS:
1413 default:
1414 NOT_REACHED();
1415 }
1416}
1417
81a76618
BP
1418/* Makes 'match' match field 'mf' exactly, with the value matched taken from
1419 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
6a885fd0
BP
1420 * prerequisites. */
1421void
1422mf_set_value(const struct mf_field *mf,
81a76618 1423 const union mf_value *value, struct match *match)
6a885fd0
BP
1424{
1425 switch (mf->id) {
1426 case MFF_TUN_ID:
81a76618 1427 match_set_tun_id(match, value->be64);
6a885fd0 1428 break;
4fe3445a
PS
1429 case MFF_TUN_SRC:
1430 match_set_tun_src(match, value->be32);
1431 break;
1432 case MFF_TUN_DST:
1433 match_set_tun_dst(match, value->be32);
1434 break;
1435 case MFF_TUN_FLAGS:
1436 match_set_tun_flags(match, ntohs(value->be16));
1437 break;
1438 case MFF_TUN_TOS:
1439 match_set_tun_tos(match, value->u8);
1440 break;
1441 case MFF_TUN_TTL:
1442 match_set_tun_ttl(match, value->u8);
1443 break;
1444
969fc56c 1445 case MFF_METADATA:
81a76618 1446 match_set_metadata(match, value->be64);
969fc56c 1447 break;
6a885fd0
BP
1448
1449 case MFF_IN_PORT:
4e022ec0 1450 match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
6a885fd0
BP
1451 break;
1452
72333065 1453 case MFF_IN_PORT_OXM: {
4e022ec0 1454 ofp_port_t port;
bc146369 1455 ofputil_port_from_ofp11(value->be32, &port);
72333065
JR
1456 match_set_in_port(match, port);
1457 break;
1458 }
1459
1b567fb9
AA
1460 case MFF_SKB_PRIORITY:
1461 match_set_skb_priority(match, ntohl(value->be32));
1462 break;
1463
1362e248
JG
1464 case MFF_PKT_MARK:
1465 match_set_pkt_mark(match, ntohl(value->be32));
1b567fb9
AA
1466 break;
1467
0d7e2fe4 1468 CASE_MFF_REGS:
81a76618 1469 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
6a885fd0 1470 break;
6a885fd0
BP
1471
1472 case MFF_ETH_SRC:
81a76618 1473 match_set_dl_src(match, value->mac);
6a885fd0
BP
1474 break;
1475
1476 case MFF_ETH_DST:
81a76618 1477 match_set_dl_dst(match, value->mac);
6a885fd0
BP
1478 break;
1479
1480 case MFF_ETH_TYPE:
81a76618 1481 match_set_dl_type(match, value->be16);
6a885fd0
BP
1482 break;
1483
1484 case MFF_VLAN_TCI:
81a76618 1485 match_set_dl_tci(match, value->be16);
6a885fd0
BP
1486 break;
1487
441c57a9 1488 case MFF_DL_VLAN:
81a76618 1489 match_set_dl_vlan(match, value->be16);
6a885fd0 1490 break;
cc34bc8c 1491 case MFF_VLAN_VID:
81a76618 1492 match_set_vlan_vid(match, value->be16);
cc34bc8c 1493 break;
6a885fd0 1494
441c57a9 1495 case MFF_DL_VLAN_PCP:
cc34bc8c 1496 case MFF_VLAN_PCP:
81a76618 1497 match_set_dl_vlan_pcp(match, value->u8);
6a885fd0
BP
1498 break;
1499
b02475c5
SH
1500 case MFF_MPLS_LABEL:
1501 match_set_mpls_label(match, value->be32);
1502 break;
1503
1504 case MFF_MPLS_TC:
1505 match_set_mpls_tc(match, value->u8);
1506 break;
1507
1508 case MFF_MPLS_BOS:
1509 match_set_mpls_bos(match, value->u8);
1510 break;
1511
6a885fd0 1512 case MFF_IPV4_SRC:
81a76618 1513 match_set_nw_src(match, value->be32);
6a885fd0
BP
1514 break;
1515
1516 case MFF_IPV4_DST:
81a76618 1517 match_set_nw_dst(match, value->be32);
6a885fd0
BP
1518 break;
1519
1520 case MFF_IPV6_SRC:
81a76618 1521 match_set_ipv6_src(match, &value->ipv6);
6a885fd0
BP
1522 break;
1523
1524 case MFF_IPV6_DST:
81a76618 1525 match_set_ipv6_dst(match, &value->ipv6);
6a885fd0
BP
1526 break;
1527
fa8223b7 1528 case MFF_IPV6_LABEL:
81a76618 1529 match_set_ipv6_label(match, value->be32);
fa8223b7
JP
1530 break;
1531
6a885fd0 1532 case MFF_IP_PROTO:
81a76618 1533 match_set_nw_proto(match, value->u8);
6a885fd0
BP
1534 break;
1535
530180fd 1536 case MFF_IP_DSCP:
81a76618 1537 match_set_nw_dscp(match, value->u8);
530180fd
JP
1538 break;
1539
1638b906
BP
1540 case MFF_IP_DSCP_SHIFTED:
1541 match_set_nw_dscp(match, value->u8 << 2);
1542 break;
1543
530180fd 1544 case MFF_IP_ECN:
81a76618 1545 match_set_nw_ecn(match, value->u8);
6a885fd0
BP
1546 break;
1547
a61680c6 1548 case MFF_IP_TTL:
81a76618 1549 match_set_nw_ttl(match, value->u8);
a61680c6
JP
1550 break;
1551
7257b535 1552 case MFF_IP_FRAG:
81a76618 1553 match_set_nw_frag(match, value->u8);
7257b535
BP
1554 break;
1555
6a885fd0 1556 case MFF_ARP_OP:
81a76618 1557 match_set_nw_proto(match, ntohs(value->be16));
6a885fd0
BP
1558 break;
1559
1560 case MFF_ARP_SPA:
81a76618 1561 match_set_nw_src(match, value->be32);
6a885fd0
BP
1562 break;
1563
1564 case MFF_ARP_TPA:
81a76618 1565 match_set_nw_dst(match, value->be32);
6a885fd0
BP
1566 break;
1567
1568 case MFF_ARP_SHA:
1569 case MFF_ND_SLL:
81a76618 1570 match_set_arp_sha(match, value->mac);
6a885fd0
BP
1571 break;
1572
1573 case MFF_ARP_THA:
1574 case MFF_ND_TLL:
81a76618 1575 match_set_arp_tha(match, value->mac);
6a885fd0
BP
1576 break;
1577
1578 case MFF_TCP_SRC:
6a885fd0 1579 case MFF_UDP_SRC:
0d56eaf2 1580 case MFF_SCTP_SRC:
81a76618 1581 match_set_tp_src(match, value->be16);
6a885fd0
BP
1582 break;
1583
71baec06 1584 case MFF_TCP_DST:
6a885fd0 1585 case MFF_UDP_DST:
0d56eaf2 1586 case MFF_SCTP_DST:
81a76618 1587 match_set_tp_dst(match, value->be16);
6a885fd0
BP
1588 break;
1589
dc235f7f
JR
1590 case MFF_TCP_FLAGS:
1591 match_set_tcp_flags(match, value->be16);
1592 break;
1593
268a95e0
BP
1594 case MFF_ICMPV4_TYPE:
1595 case MFF_ICMPV6_TYPE:
81a76618 1596 match_set_icmp_type(match, value->u8);
6a885fd0
BP
1597 break;
1598
268a95e0
BP
1599 case MFF_ICMPV4_CODE:
1600 case MFF_ICMPV6_CODE:
81a76618 1601 match_set_icmp_code(match, value->u8);
6a885fd0
BP
1602 break;
1603
1604 case MFF_ND_TARGET:
81a76618 1605 match_set_nd_target(match, &value->ipv6);
6a885fd0
BP
1606 break;
1607
1608 case MFF_N_IDS:
1609 default:
1610 NOT_REACHED();
1611 }
1612}
1613
5a0a5702
GS
1614/* Unwildcard 'mask' member field described by 'mf'. The caller is
1615 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
1616void
1617mf_mask_field(const struct mf_field *mf, struct flow *mask)
1618{
1619 static const union mf_value exact_match_mask = MF_EXACT_MASK_INITIALIZER;
1620
1621 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan()
1622 * as that will be considered as OFP10_VLAN_NONE. So consider it as a
1623 * special case. For the rest, calling mf_set_flow_value() is good
1624 * enough. */
1625 if (mf->id == MFF_DL_VLAN) {
1626 flow_set_dl_vlan(mask, htons(VLAN_VID_MASK));
1627 } else {
1628 mf_set_flow_value(mf, &exact_match_mask, mask);
1629 }
1630}
1631
910fb1d8
JP
1632/* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1633 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
28da1f8f
BP
1634void
1635mf_set_flow_value(const struct mf_field *mf,
1636 const union mf_value *value, struct flow *flow)
1637{
1638 switch (mf->id) {
1639 case MFF_TUN_ID:
296e07ac 1640 flow->tunnel.tun_id = value->be64;
28da1f8f 1641 break;
4fe3445a
PS
1642 case MFF_TUN_SRC:
1643 flow->tunnel.ip_src = value->be32;
1644 break;
1645 case MFF_TUN_DST:
1646 flow->tunnel.ip_dst = value->be32;
1647 break;
1648 case MFF_TUN_FLAGS:
1649 flow->tunnel.flags = ntohs(value->be16);
1650 break;
1651 case MFF_TUN_TOS:
1652 flow->tunnel.ip_tos = value->u8;
1653 break;
1654 case MFF_TUN_TTL:
1655 flow->tunnel.ip_ttl = value->u8;
1656 break;
1657
969fc56c
JS
1658 case MFF_METADATA:
1659 flow->metadata = value->be64;
1660 break;
28da1f8f
BP
1661
1662 case MFF_IN_PORT:
4e022ec0 1663 flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
28da1f8f
BP
1664 break;
1665
72333065 1666 case MFF_IN_PORT_OXM: {
4e022ec0 1667 ofp_port_t port;
bc146369 1668 ofputil_port_from_ofp11(value->be32, &port);
4e022ec0 1669 flow->in_port.ofp_port = port;
72333065
JR
1670 break;
1671 }
1672
1b567fb9
AA
1673 case MFF_SKB_PRIORITY:
1674 flow->skb_priority = ntohl(value->be32);
1675 break;
1676
1362e248
JG
1677 case MFF_PKT_MARK:
1678 flow->pkt_mark = ntohl(value->be32);
1b567fb9
AA
1679 break;
1680
0d7e2fe4 1681 CASE_MFF_REGS:
28da1f8f
BP
1682 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1683 break;
28da1f8f
BP
1684
1685 case MFF_ETH_SRC:
1686 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1687 break;
1688
1689 case MFF_ETH_DST:
b5a36e38 1690 memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
28da1f8f
BP
1691 break;
1692
1693 case MFF_ETH_TYPE:
1694 flow->dl_type = value->be16;
1695 break;
1696
1697 case MFF_VLAN_TCI:
1698 flow->vlan_tci = value->be16;
1699 break;
1700
441c57a9 1701 case MFF_DL_VLAN:
fb0451d9 1702 flow_set_dl_vlan(flow, value->be16);
28da1f8f 1703 break;
cc34bc8c
BP
1704 case MFF_VLAN_VID:
1705 flow_set_vlan_vid(flow, value->be16);
1706 break;
28da1f8f 1707
441c57a9 1708 case MFF_DL_VLAN_PCP:
cc34bc8c 1709 case MFF_VLAN_PCP:
28da1f8f
BP
1710 flow_set_vlan_pcp(flow, value->u8);
1711 break;
1712
b02475c5
SH
1713 case MFF_MPLS_LABEL:
1714 flow_set_mpls_label(flow, value->be32);
1715 break;
1716
1717 case MFF_MPLS_TC:
1718 flow_set_mpls_tc(flow, value->u8);
1719 break;
1720
1721 case MFF_MPLS_BOS:
1722 flow_set_mpls_bos(flow, value->u8);
1723 break;
1724
28da1f8f
BP
1725 case MFF_IPV4_SRC:
1726 flow->nw_src = value->be32;
1727 break;
1728
1729 case MFF_IPV4_DST:
1730 flow->nw_dst = value->be32;
1731 break;
1732
1733 case MFF_IPV6_SRC:
1734 flow->ipv6_src = value->ipv6;
1735 break;
1736
1737 case MFF_IPV6_DST:
1738 flow->ipv6_dst = value->ipv6;
1739 break;
1740
1741 case MFF_IPV6_LABEL:
1742 flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1743 break;
1744
1745 case MFF_IP_PROTO:
1746 flow->nw_proto = value->u8;
1747 break;
1748
1749 case MFF_IP_DSCP:
1750 flow->nw_tos &= ~IP_DSCP_MASK;
1751 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1752 break;
1753
1638b906
BP
1754 case MFF_IP_DSCP_SHIFTED:
1755 flow->nw_tos &= ~IP_DSCP_MASK;
1756 flow->nw_tos |= value->u8 << 2;
1757 break;
1758
28da1f8f
BP
1759 case MFF_IP_ECN:
1760 flow->nw_tos &= ~IP_ECN_MASK;
1761 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1762 break;
1763
1764 case MFF_IP_TTL:
1765 flow->nw_ttl = value->u8;
1766 break;
1767
1768 case MFF_IP_FRAG:
1769 flow->nw_frag &= value->u8;
1770 break;
1771
1772 case MFF_ARP_OP:
1773 flow->nw_proto = ntohs(value->be16);
1774 break;
1775
1776 case MFF_ARP_SPA:
1777 flow->nw_src = value->be32;
1778 break;
1779
1780 case MFF_ARP_TPA:
1781 flow->nw_dst = value->be32;
1782 break;
1783
1784 case MFF_ARP_SHA:
1785 case MFF_ND_SLL:
1786 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1787 break;
1788
1789 case MFF_ARP_THA:
1790 case MFF_ND_TLL:
1791 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1792 break;
1793
1794 case MFF_TCP_SRC:
1795 case MFF_UDP_SRC:
0d56eaf2 1796 case MFF_SCTP_SRC:
28da1f8f
BP
1797 flow->tp_src = value->be16;
1798 break;
1799
1800 case MFF_TCP_DST:
1801 case MFF_UDP_DST:
0d56eaf2 1802 case MFF_SCTP_DST:
28da1f8f
BP
1803 flow->tp_dst = value->be16;
1804 break;
1805
dc235f7f
JR
1806 case MFF_TCP_FLAGS:
1807 flow->tcp_flags = value->be16;
1808 break;
1809
28da1f8f
BP
1810 case MFF_ICMPV4_TYPE:
1811 case MFF_ICMPV6_TYPE:
1812 flow->tp_src = htons(value->u8);
1813 break;
1814
1815 case MFF_ICMPV4_CODE:
1816 case MFF_ICMPV6_CODE:
1817 flow->tp_dst = htons(value->u8);
1818 break;
1819
1820 case MFF_ND_TARGET:
1821 flow->nd_target = value->ipv6;
1822 break;
1823
1824 case MFF_N_IDS:
1825 default:
1826 NOT_REACHED();
1827 }
1828}
1829
ccbe50f8
BP
1830/* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1831 *
1832 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1833 * prerequisites. */
1834bool
1835mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1836{
1837 union mf_value value;
1838
1839 mf_get_value(mf, flow, &value);
1840 return is_all_zeros((const uint8_t *) &value, mf->n_bytes);
1841}
1842
81a76618 1843/* Makes 'match' wildcard field 'mf'.
6a885fd0 1844 *
81a76618 1845 * The caller is responsible for ensuring that 'match' meets 'mf''s
6a885fd0
BP
1846 * prerequisites. */
1847void
81a76618 1848mf_set_wild(const struct mf_field *mf, struct match *match)
6a885fd0
BP
1849{
1850 switch (mf->id) {
1851 case MFF_TUN_ID:
81a76618 1852 match_set_tun_id_masked(match, htonll(0), htonll(0));
6a885fd0 1853 break;
4fe3445a
PS
1854 case MFF_TUN_SRC:
1855 match_set_tun_src_masked(match, htonl(0), htonl(0));
1856 break;
1857 case MFF_TUN_DST:
1858 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1859 break;
1860 case MFF_TUN_FLAGS:
1861 match_set_tun_flags_masked(match, 0, 0);
1862 break;
1863 case MFF_TUN_TOS:
1864 match_set_tun_tos_masked(match, 0, 0);
1865 break;
1866 case MFF_TUN_TTL:
1867 match_set_tun_ttl_masked(match, 0, 0);
1868 break;
1869
969fc56c 1870 case MFF_METADATA:
81a76618 1871 match_set_metadata_masked(match, htonll(0), htonll(0));
801d2b2a 1872 break;
6a885fd0
BP
1873
1874 case MFF_IN_PORT:
72333065 1875 case MFF_IN_PORT_OXM:
4e022ec0
AW
1876 match->flow.in_port.ofp_port = 0;
1877 match->wc.masks.in_port.ofp_port = 0;
6a885fd0
BP
1878 break;
1879
1b567fb9
AA
1880 case MFF_SKB_PRIORITY:
1881 match->flow.skb_priority = 0;
1882 match->wc.masks.skb_priority = 0;
1883 break;
1884
1362e248
JG
1885 case MFF_PKT_MARK:
1886 match->flow.pkt_mark = 0;
1887 match->wc.masks.pkt_mark = 0;
1b567fb9
AA
1888 break;
1889
0d7e2fe4 1890 CASE_MFF_REGS:
81a76618 1891 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
e9358af6 1892 break;
6a885fd0
BP
1893
1894 case MFF_ETH_SRC:
81a76618
BP
1895 memset(match->flow.dl_src, 0, ETH_ADDR_LEN);
1896 memset(match->wc.masks.dl_src, 0, ETH_ADDR_LEN);
6a885fd0
BP
1897 break;
1898
1899 case MFF_ETH_DST:
81a76618
BP
1900 memset(match->flow.dl_dst, 0, ETH_ADDR_LEN);
1901 memset(match->wc.masks.dl_dst, 0, ETH_ADDR_LEN);
6a885fd0
BP
1902 break;
1903
1904 case MFF_ETH_TYPE:
81a76618
BP
1905 match->flow.dl_type = htons(0);
1906 match->wc.masks.dl_type = htons(0);
6a885fd0
BP
1907 break;
1908
1909 case MFF_VLAN_TCI:
81a76618 1910 match_set_dl_tci_masked(match, htons(0), htons(0));
6a885fd0
BP
1911 break;
1912
441c57a9 1913 case MFF_DL_VLAN:
cc34bc8c 1914 case MFF_VLAN_VID:
81a76618 1915 match_set_any_vid(match);
6a885fd0
BP
1916 break;
1917
441c57a9 1918 case MFF_DL_VLAN_PCP:
cc34bc8c 1919 case MFF_VLAN_PCP:
81a76618 1920 match_set_any_pcp(match);
6a885fd0
BP
1921 break;
1922
b02475c5
SH
1923 case MFF_MPLS_LABEL:
1924 match_set_any_mpls_label(match);
1925 break;
1926
1927 case MFF_MPLS_TC:
1928 match_set_any_mpls_tc(match);
1929 break;
1930
1931 case MFF_MPLS_BOS:
1932 match_set_any_mpls_bos(match);
1933 break;
1934
6a885fd0
BP
1935 case MFF_IPV4_SRC:
1936 case MFF_ARP_SPA:
81a76618 1937 match_set_nw_src_masked(match, htonl(0), htonl(0));
6a885fd0
BP
1938 break;
1939
1940 case MFF_IPV4_DST:
1941 case MFF_ARP_TPA:
81a76618 1942 match_set_nw_dst_masked(match, htonl(0), htonl(0));
6a885fd0
BP
1943 break;
1944
1945 case MFF_IPV6_SRC:
81a76618
BP
1946 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1947 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
6a885fd0
BP
1948 break;
1949
1950 case MFF_IPV6_DST:
81a76618
BP
1951 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1952 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
6a885fd0
BP
1953 break;
1954
fa8223b7 1955 case MFF_IPV6_LABEL:
81a76618
BP
1956 match->wc.masks.ipv6_label = htonl(0);
1957 match->flow.ipv6_label = htonl(0);
fa8223b7
JP
1958 break;
1959
6a885fd0 1960 case MFF_IP_PROTO:
81a76618
BP
1961 match->wc.masks.nw_proto = 0;
1962 match->flow.nw_proto = 0;
6a885fd0
BP
1963 break;
1964
530180fd 1965 case MFF_IP_DSCP:
1638b906 1966 case MFF_IP_DSCP_SHIFTED:
81a76618
BP
1967 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1968 match->flow.nw_tos &= ~IP_DSCP_MASK;
7257b535
BP
1969 break;
1970
530180fd 1971 case MFF_IP_ECN:
81a76618
BP
1972 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1973 match->flow.nw_tos &= ~IP_ECN_MASK;
530180fd
JP
1974 break;
1975
a61680c6 1976 case MFF_IP_TTL:
81a76618
BP
1977 match->wc.masks.nw_ttl = 0;
1978 match->flow.nw_ttl = 0;
a61680c6
JP
1979 break;
1980
7257b535 1981 case MFF_IP_FRAG:
81a76618
BP
1982 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
1983 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
6a885fd0
BP
1984 break;
1985
1986 case MFF_ARP_OP:
81a76618
BP
1987 match->wc.masks.nw_proto = 0;
1988 match->flow.nw_proto = 0;
6a885fd0
BP
1989 break;
1990
1991 case MFF_ARP_SHA:
1992 case MFF_ND_SLL:
81a76618
BP
1993 memset(match->flow.arp_sha, 0, ETH_ADDR_LEN);
1994 memset(match->wc.masks.arp_sha, 0, ETH_ADDR_LEN);
6a885fd0
BP
1995 break;
1996
1997 case MFF_ARP_THA:
1998 case MFF_ND_TLL:
81a76618
BP
1999 memset(match->flow.arp_tha, 0, ETH_ADDR_LEN);
2000 memset(match->wc.masks.arp_tha, 0, ETH_ADDR_LEN);
6a885fd0
BP
2001 break;
2002
2003 case MFF_TCP_SRC:
2004 case MFF_UDP_SRC:
0d56eaf2 2005 case MFF_SCTP_SRC:
268a95e0
BP
2006 case MFF_ICMPV4_TYPE:
2007 case MFF_ICMPV6_TYPE:
81a76618
BP
2008 match->wc.masks.tp_src = htons(0);
2009 match->flow.tp_src = htons(0);
6a885fd0
BP
2010 break;
2011
2012 case MFF_TCP_DST:
2013 case MFF_UDP_DST:
0d56eaf2 2014 case MFF_SCTP_DST:
268a95e0
BP
2015 case MFF_ICMPV4_CODE:
2016 case MFF_ICMPV6_CODE:
81a76618
BP
2017 match->wc.masks.tp_dst = htons(0);
2018 match->flow.tp_dst = htons(0);
6a885fd0
BP
2019 break;
2020
dc235f7f
JR
2021 case MFF_TCP_FLAGS:
2022 match->wc.masks.tcp_flags = htons(0);
2023 match->flow.tcp_flags = htons(0);
2024 break;
2025
6a885fd0 2026 case MFF_ND_TARGET:
81a76618
BP
2027 memset(&match->wc.masks.nd_target, 0,
2028 sizeof match->wc.masks.nd_target);
2029 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
6a885fd0
BP
2030 break;
2031
2032 case MFF_N_IDS:
2033 default:
2034 NOT_REACHED();
2035 }
2036}
2037
81a76618 2038/* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
6a885fd0
BP
2039 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
2040 * with a 1-bit indicating that the corresponding value bit must match and a
2041 * 0-bit indicating a don't-care.
2042 *
2043 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
81a76618
BP
2044 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
2045 * call is equivalent to mf_set_wild(mf, match).
6a885fd0
BP
2046 *
2047 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
81a76618 2048 * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
db0b6c29 2049enum ofputil_protocol
6a885fd0
BP
2050mf_set(const struct mf_field *mf,
2051 const union mf_value *value, const union mf_value *mask,
81a76618 2052 struct match *match)
6a885fd0
BP
2053{
2054 if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
81a76618 2055 mf_set_value(mf, value, match);
db0b6c29 2056 return mf->usable_protocols;
6a885fd0 2057 } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
81a76618 2058 mf_set_wild(mf, match);
db0b6c29 2059 return OFPUTIL_P_ANY;
6a885fd0
BP
2060 }
2061
2062 switch (mf->id) {
2063 case MFF_IN_PORT:
72333065 2064 case MFF_IN_PORT_OXM:
1b567fb9 2065 case MFF_SKB_PRIORITY:
6a885fd0 2066 case MFF_ETH_TYPE:
441c57a9
BP
2067 case MFF_DL_VLAN:
2068 case MFF_DL_VLAN_PCP:
cc34bc8c 2069 case MFF_VLAN_PCP:
b02475c5
SH
2070 case MFF_MPLS_LABEL:
2071 case MFF_MPLS_TC:
2072 case MFF_MPLS_BOS:
6a885fd0 2073 case MFF_IP_PROTO:
a61680c6 2074 case MFF_IP_TTL:
530180fd 2075 case MFF_IP_DSCP:
1638b906 2076 case MFF_IP_DSCP_SHIFTED:
530180fd 2077 case MFF_IP_ECN:
6a885fd0 2078 case MFF_ARP_OP:
268a95e0
BP
2079 case MFF_ICMPV4_TYPE:
2080 case MFF_ICMPV4_CODE:
2081 case MFF_ICMPV6_TYPE:
2082 case MFF_ICMPV6_CODE:
af69bf19 2083 return OFPUTIL_P_NONE;
6a885fd0
BP
2084
2085 case MFF_TUN_ID:
81a76618 2086 match_set_tun_id_masked(match, value->be64, mask->be64);
6a885fd0 2087 break;
4fe3445a
PS
2088 case MFF_TUN_SRC:
2089 match_set_tun_src_masked(match, value->be32, mask->be32);
2090 break;
2091 case MFF_TUN_DST:
2092 match_set_tun_dst_masked(match, value->be32, mask->be32);
2093 break;
2094 case MFF_TUN_FLAGS:
2095 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
2096 break;
2097 case MFF_TUN_TTL:
2098 match_set_tun_ttl_masked(match, value->u8, mask->u8);
2099 break;
2100 case MFF_TUN_TOS:
2101 match_set_tun_tos_masked(match, value->u8, mask->u8);
2102 break;
2103
969fc56c 2104 case MFF_METADATA:
81a76618 2105 match_set_metadata_masked(match, value->be64, mask->be64);
969fc56c 2106 break;
6a885fd0 2107
0d7e2fe4 2108 CASE_MFF_REGS:
81a76618
BP
2109 match_set_reg_masked(match, mf->id - MFF_REG0,
2110 ntohl(value->be32), ntohl(mask->be32));
6a885fd0 2111 break;
ac923e91
JG
2112
2113 case MFF_PKT_MARK:
2114 match_set_pkt_mark_masked(match, ntohl(value->be32),
2115 ntohl(mask->be32));
2116 break;
6a885fd0
BP
2117
2118 case MFF_ETH_DST:
81a76618 2119 match_set_dl_dst_masked(match, value->mac, mask->mac);
73c0ce34
JS
2120 break;
2121
2122 case MFF_ETH_SRC:
81a76618 2123 match_set_dl_src_masked(match, value->mac, mask->mac);
6a885fd0
BP
2124 break;
2125
e878338b
SH
2126 case MFF_ARP_SHA:
2127 case MFF_ND_SLL:
81a76618 2128 match_set_arp_sha_masked(match, value->mac, mask->mac);
e878338b
SH
2129 break;
2130
2131 case MFF_ARP_THA:
2132 case MFF_ND_TLL:
81a76618 2133 match_set_arp_tha_masked(match, value->mac, mask->mac);
e878338b
SH
2134 break;
2135
6a885fd0 2136 case MFF_VLAN_TCI:
81a76618 2137 match_set_dl_tci_masked(match, value->be16, mask->be16);
6a885fd0
BP
2138 break;
2139
cc34bc8c 2140 case MFF_VLAN_VID:
81a76618 2141 match_set_vlan_vid_masked(match, value->be16, mask->be16);
cc34bc8c
BP
2142 break;
2143
6a885fd0 2144 case MFF_IPV4_SRC:
81a76618 2145 match_set_nw_src_masked(match, value->be32, mask->be32);
db0b6c29 2146 goto cidr_check;
6a885fd0
BP
2147
2148 case MFF_IPV4_DST:
81a76618 2149 match_set_nw_dst_masked(match, value->be32, mask->be32);
db0b6c29 2150 goto cidr_check;
6a885fd0
BP
2151
2152 case MFF_IPV6_SRC:
81a76618 2153 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
6a885fd0
BP
2154 break;
2155
2156 case MFF_IPV6_DST:
81a76618 2157 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
6a885fd0
BP
2158 break;
2159
32455024
SH
2160 case MFF_IPV6_LABEL:
2161 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
81a76618 2162 mf_set_value(mf, value, match);
32455024 2163 } else {
81a76618 2164 match_set_ipv6_label_masked(match, value->be32, mask->be32);
32455024
SH
2165 }
2166 break;
2167
47284b1f 2168 case MFF_ND_TARGET:
81a76618 2169 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
47284b1f
AA
2170 break;
2171
7257b535 2172 case MFF_IP_FRAG:
81a76618 2173 match_set_nw_frag_masked(match, value->u8, mask->u8);
7257b535
BP
2174 break;
2175
6a885fd0 2176 case MFF_ARP_SPA:
81a76618 2177 match_set_nw_src_masked(match, value->be32, mask->be32);
db0b6c29 2178 goto cidr_check;
6a885fd0
BP
2179
2180 case MFF_ARP_TPA:
81a76618 2181 match_set_nw_dst_masked(match, value->be32, mask->be32);
db0b6c29 2182 goto cidr_check;
6a885fd0 2183
73f33563
BP
2184 case MFF_TCP_SRC:
2185 case MFF_UDP_SRC:
0d56eaf2 2186 case MFF_SCTP_SRC:
81a76618 2187 match_set_tp_src_masked(match, value->be16, mask->be16);
73f33563
BP
2188 break;
2189
2190 case MFF_TCP_DST:
2191 case MFF_UDP_DST:
0d56eaf2 2192 case MFF_SCTP_DST:
81a76618 2193 match_set_tp_dst_masked(match, value->be16, mask->be16);
73f33563
BP
2194 break;
2195
dc235f7f
JR
2196 case MFF_TCP_FLAGS:
2197 match_set_tcp_flags_masked(match, value->be16, mask->be16);
2198 break;
2199
6a885fd0
BP
2200 case MFF_N_IDS:
2201 default:
2202 NOT_REACHED();
2203 }
db0b6c29
JR
2204
2205 return mf->usable_protocols_bitwise;
2206
2207cidr_check:
2208 return ip_is_cidr(mask->be32) ? mf->usable_protocols :
2209 mf->usable_protocols_bitwise;
6a885fd0
BP
2210}
2211
816fd533
BP
2212static enum ofperr
2213mf_check__(const struct mf_subfield *sf, const struct flow *flow,
2214 const char *type)
6a885fd0 2215{
816fd533
BP
2216 if (!sf->field) {
2217 VLOG_WARN_RL(&rl, "unknown %s field", type);
9e404d1e 2218 return OFPERR_OFPBAC_BAD_SET_TYPE;
816fd533
BP
2219 } else if (!sf->n_bits) {
2220 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
9e404d1e 2221 return OFPERR_OFPBAC_BAD_SET_LEN;
816fd533
BP
2222 } else if (sf->ofs >= sf->field->n_bits) {
2223 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
2224 sf->ofs, sf->field->n_bits, type, sf->field->name);
9e404d1e 2225 return OFPERR_OFPBAC_BAD_SET_LEN;
816fd533
BP
2226 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
2227 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
2228 "of %s field %s", sf->ofs, sf->n_bits,
2229 sf->field->n_bits, type, sf->field->name);
9e404d1e 2230 return OFPERR_OFPBAC_BAD_SET_LEN;
816fd533
BP
2231 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
2232 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
2233 type, sf->field->name);
9e404d1e 2234 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
6a885fd0 2235 } else {
816fd533
BP
2236 return 0;
2237 }
816fd533 2238}
6a885fd0 2239
816fd533
BP
2240/* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
2241 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
2242 * ofp_mkerr()). */
2243enum ofperr
2244mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
2245{
2246 return mf_check__(sf, flow, "source");
2247}
6a885fd0 2248
816fd533
BP
2249/* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
2250 * if so, otherwise an OpenFlow error code (e.g. as returned by
2251 * ofp_mkerr()). */
2252enum ofperr
2253mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
2254{
2255 int error = mf_check__(sf, flow, "destination");
2256 if (!error && !sf->field->writable) {
2257 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2258 sf->field->name);
9e404d1e 2259 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
6a885fd0 2260 }
816fd533 2261 return error;
6a885fd0
BP
2262}
2263
81a76618 2264/* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
6a885fd0
BP
2265 * 'value' and 'mask', respectively. */
2266void
81a76618 2267mf_get(const struct mf_field *mf, const struct match *match,
6a885fd0
BP
2268 union mf_value *value, union mf_value *mask)
2269{
81a76618
BP
2270 mf_get_value(mf, &match->flow, value);
2271 mf_get_mask(mf, &match->wc, mask);
6a885fd0
BP
2272}
2273
6a885fd0
BP
2274static char *
2275mf_from_integer_string(const struct mf_field *mf, const char *s,
2276 uint8_t *valuep, uint8_t *maskp)
2277{
2278 unsigned long long int integer, mask;
2279 char *tail;
2280 int i;
2281
2282 errno = 0;
2283 integer = strtoull(s, &tail, 0);
2284 if (errno || (*tail != '\0' && *tail != '/')) {
2285 goto syntax_error;
2286 }
2287
2288 if (*tail == '/') {
2289 mask = strtoull(tail + 1, &tail, 0);
2290 if (errno || *tail != '\0') {
2291 goto syntax_error;
2292 }
2293 } else {
2294 mask = ULLONG_MAX;
2295 }
2296
2297 for (i = mf->n_bytes - 1; i >= 0; i--) {
2298 valuep[i] = integer;
2299 maskp[i] = mask;
2300 integer >>= 8;
2301 mask >>= 8;
2302 }
2303 if (integer) {
2304 return xasprintf("%s: value too large for %u-byte field %s",
2305 s, mf->n_bytes, mf->name);
2306 }
2307 return NULL;
2308
2309syntax_error:
2310 return xasprintf("%s: bad syntax for %s", s, mf->name);
2311}
2312
2313static char *
2314mf_from_ethernet_string(const struct mf_field *mf, const char *s,
2315 uint8_t mac[ETH_ADDR_LEN],
2316 uint8_t mask[ETH_ADDR_LEN])
2317{
279fb135 2318 int n;
6a885fd0 2319
279fb135 2320 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
6a885fd0 2321
279fb135 2322 n = -1;
c2c28dfd 2323 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(mac), &n)
279fb135 2324 && n == strlen(s)) {
6a885fd0
BP
2325 memset(mask, 0xff, ETH_ADDR_LEN);
2326 return NULL;
279fb135 2327 }
6a885fd0 2328
279fb135 2329 n = -1;
c2c28dfd
BP
2330 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
2331 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask), &n)
279fb135
BP
2332 && n == strlen(s)) {
2333 return NULL;
6a885fd0 2334 }
279fb135
BP
2335
2336 return xasprintf("%s: invalid Ethernet address", s);
6a885fd0
BP
2337}
2338
2339static char *
2340mf_from_ipv4_string(const struct mf_field *mf, const char *s,
2341 ovs_be32 *ip, ovs_be32 *mask)
2342{
2343 int prefix;
2344
cb22974d 2345 ovs_assert(mf->n_bytes == sizeof *ip);
6a885fd0 2346
c2c28dfd
BP
2347 if (ovs_scan(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
2348 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask))) {
6a885fd0 2349 /* OK. */
c2c28dfd 2350 } else if (ovs_scan(s, IP_SCAN_FMT"/%d", IP_SCAN_ARGS(ip), &prefix)) {
6a885fd0
BP
2351 if (prefix <= 0 || prefix > 32) {
2352 return xasprintf("%s: network prefix bits not between 1 and "
2353 "32", s);
2354 } else if (prefix == 32) {
b8266395 2355 *mask = OVS_BE32_MAX;
6a885fd0
BP
2356 } else {
2357 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
2358 }
c2c28dfd 2359 } else if (ovs_scan(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip))) {
b8266395 2360 *mask = OVS_BE32_MAX;
6a885fd0
BP
2361 } else {
2362 return xasprintf("%s: invalid IP address", s);
2363 }
2364 return NULL;
2365}
2366
2367static char *
2368mf_from_ipv6_string(const struct mf_field *mf, const char *s,
2369 struct in6_addr *value, struct in6_addr *mask)
2370{
2371 char *str = xstrdup(s);
2372 char *save_ptr = NULL;
2373 const char *name, *netmask;
2374 int retval;
2375
cb22974d 2376 ovs_assert(mf->n_bytes == sizeof *value);
6a885fd0
BP
2377
2378 name = strtok_r(str, "/", &save_ptr);
2379 retval = name ? lookup_ipv6(name, value) : EINVAL;
2380 if (retval) {
2381 char *err;
2382
2383 err = xasprintf("%s: could not convert to IPv6 address", str);
2384 free(str);
2385
2386 return err;
2387 }
2388
2389 netmask = strtok_r(NULL, "/", &save_ptr);
2390 if (netmask) {
ff0b06ee
BP
2391 if (inet_pton(AF_INET6, netmask, mask) != 1) {
2392 int prefix = atoi(netmask);
2393 if (prefix <= 0 || prefix > 128) {
2394 free(str);
2395 return xasprintf("%s: prefix bits not between 1 and 128", s);
2396 } else {
2397 *mask = ipv6_create_mask(prefix);
2398 }
6a885fd0
BP
2399 }
2400 } else {
2401 *mask = in6addr_exact;
2402 }
2403 free(str);
2404
2405 return NULL;
2406}
2407
2408static char *
2409mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
2410 ovs_be16 *valuep, ovs_be16 *maskp)
2411{
4e022ec0 2412 ofp_port_t port;
6a885fd0 2413
cb22974d 2414 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
33ab38d9
JR
2415
2416 if (ofputil_port_from_string(s, &port)) {
4e022ec0 2417 *valuep = htons(ofp_to_u16(port));
b8266395 2418 *maskp = OVS_BE16_MAX;
6a885fd0 2419 return NULL;
6a885fd0 2420 }
33ab38d9 2421 return xasprintf("%s: port value out of range for %s", s, mf->name);
6a885fd0
BP
2422}
2423
72333065
JR
2424static char *
2425mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
2426 ovs_be32 *valuep, ovs_be32 *maskp)
2427{
4e022ec0 2428 ofp_port_t port;
72333065
JR
2429
2430 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
2431 if (ofputil_port_from_string(s, &port)) {
2432 *valuep = ofputil_port_to_ofp11(port);
b8266395 2433 *maskp = OVS_BE32_MAX;
72333065
JR
2434 return NULL;
2435 }
2436 return xasprintf("%s: port value out of range for %s", s, mf->name);
2437}
2438
7257b535
BP
2439struct frag_handling {
2440 const char *name;
2441 uint8_t mask;
2442 uint8_t value;
2443};
2444
2445static const struct frag_handling all_frags[] = {
eadef313
JP
2446#define A FLOW_NW_FRAG_ANY
2447#define L FLOW_NW_FRAG_LATER
7257b535
BP
2448 /* name mask value */
2449
2450 { "no", A|L, 0 },
2451 { "first", A|L, A },
2452 { "later", A|L, A|L },
2453
2454 { "no", A, 0 },
2455 { "yes", A, A },
2456
2457 { "not_later", L, 0 },
2458 { "later", L, L },
2459#undef A
2460#undef L
2461};
2462
2463static char *
2464mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2465{
2466 const struct frag_handling *h;
2467
2468 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2469 if (!strcasecmp(s, h->name)) {
2470 /* We force the upper bits of the mask on to make mf_parse_value()
2471 * happy (otherwise it will never think it's an exact match.) */
eadef313 2472 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
7257b535
BP
2473 *valuep = h->value;
2474 return NULL;
2475 }
2476 }
2477
2478 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2479 "\"yes\", \"first\", \"later\", \"not_first\"", s);
2480}
2481
4fe3445a
PS
2482static int
2483parse_flow_tun_flags(const char *s_, const char *(*bit_to_string)(uint32_t),
2484 ovs_be16 *res)
2485{
2486 uint32_t result = 0;
2487 char *save_ptr = NULL;
2488 char *name;
2489 int rc = 0;
2490 char *s = xstrdup(s_);
2491
2492 for (name = strtok_r((char *)s, " |", &save_ptr); name;
2493 name = strtok_r(NULL, " |", &save_ptr)) {
2494 int name_len;
2495 unsigned long long int flags;
2496 uint32_t bit;
4fe3445a 2497
c2c28dfd 2498 if (ovs_scan(name, "%lli", &flags)) {
4fe3445a
PS
2499 result |= flags;
2500 continue;
2501 }
2502 name_len = strlen(name);
2503 for (bit = 1; bit; bit <<= 1) {
2504 const char *fname = bit_to_string(bit);
2505 size_t len;
2506
2507 if (!fname) {
2508 continue;
2509 }
2510
2511 len = strlen(fname);
2512 if (len != name_len) {
2513 continue;
2514 }
2515 if (!strncmp(name, fname, len)) {
2516 result |= bit;
2517 break;
2518 }
2519 }
2520
2521 if (!bit) {
2522 rc = -ENOENT;
2523 goto out;
2524 }
2525 }
2526
2527 *res = htons(result);
2528out:
2529 free(s);
2530 return rc;
2531}
2532
2533static char *
b0aa8146 2534mf_from_tun_flags_string(const char *s, ovs_be16 *valuep, ovs_be16 *maskp)
4fe3445a
PS
2535{
2536 if (!parse_flow_tun_flags(s, flow_tun_flag_to_string, valuep)) {
b8266395 2537 *maskp = OVS_BE16_MAX;
4fe3445a
PS
2538 return NULL;
2539 }
2540
2541 return xasprintf("%s: unknown tunnel flags (valid flags are \"df\", "
c49a5e33 2542 "\"csum\", \"key\")", s);
4fe3445a
PS
2543}
2544
61bf6666
JR
2545static char *
2546mf_from_tcp_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
2547{
2548 uint16_t flags = 0;
2549 uint16_t mask = 0;
2550 uint16_t bit;
2551 int n;
2552
2553 if (ovs_scan(s, "%"SCNi16"/%"SCNi16"%n", &flags, &mask, &n) && !s[n]) {
2554 *flagsp = htons(flags);
2555 *maskp = htons(mask);
2556 return NULL;
2557 }
2558 if (ovs_scan(s, "%"SCNi16"%n", &flags, &n) && !s[n]) {
2559 *flagsp = htons(flags);
2560 *maskp = OVS_BE16_MAX;
2561 return NULL;
2562 }
2563
2564 while (*s != '\0') {
2565 bool set;
2566 int name_len;
2567
2568 switch (*s) {
2569 case '+':
2570 set = true;
2571 break;
2572 case '-':
2573 set = false;
2574 break;
2575 default:
2576 return xasprintf("%s: TCP flag must be preceded by '+' (for SET) "
2577 "or '-' (NOT SET)", s);
2578 }
2579 s++;
2580
2581 name_len = strcspn(s,"+-");
2582
2583 for (bit = 1; bit; bit <<= 1) {
2584 const char *fname = packet_tcp_flag_to_string(bit);
2585 size_t len;
2586
2587 if (!fname) {
2588 continue;
2589 }
2590
2591 len = strlen(fname);
2592 if (len != name_len) {
2593 continue;
2594 }
2595 if (!strncmp(s, fname, len)) {
2596 if (mask & bit) {
2597 return xasprintf("%s: Each TCP flag can be specified only "
2598 "once", s);
2599 }
2600 if (set) {
2601 flags |= bit;
2602 }
2603 mask |= bit;
2604 break;
2605 }
2606 }
2607
2608 if (!bit) {
2609 return xasprintf("%s: unknown TCP flag(s)", s);
2610 }
2611 s += name_len;
2612 }
2613
2614 *flagsp = htons(flags);
2615 *maskp = htons(mask);
2616 return NULL;
2617}
2618
2619
6a885fd0
BP
2620/* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2621 * NULL if successful, otherwise a malloc()'d string describing the error. */
2622char *
2623mf_parse(const struct mf_field *mf, const char *s,
2624 union mf_value *value, union mf_value *mask)
2625{
3634dff8
BP
2626 char *error;
2627
bedde04c 2628 if (!strcmp(s, "*")) {
6a885fd0
BP
2629 memset(value, 0, mf->n_bytes);
2630 memset(mask, 0, mf->n_bytes);
2631 return NULL;
2632 }
2633
2634 switch (mf->string) {
2635 case MFS_DECIMAL:
2636 case MFS_HEXADECIMAL:
3634dff8
BP
2637 error = mf_from_integer_string(mf, s,
2638 (uint8_t *) value, (uint8_t *) mask);
2639 break;
6a885fd0
BP
2640
2641 case MFS_ETHERNET:
3634dff8
BP
2642 error = mf_from_ethernet_string(mf, s, value->mac, mask->mac);
2643 break;
6a885fd0
BP
2644
2645 case MFS_IPV4:
3634dff8
BP
2646 error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2647 break;
6a885fd0
BP
2648
2649 case MFS_IPV6:
3634dff8
BP
2650 error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2651 break;
6a885fd0
BP
2652
2653 case MFS_OFP_PORT:
3634dff8
BP
2654 error = mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2655 break;
7257b535 2656
72333065 2657 case MFS_OFP_PORT_OXM:
3634dff8
BP
2658 error = mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
2659 break;
72333065 2660
7257b535 2661 case MFS_FRAG:
3634dff8
BP
2662 error = mf_from_frag_string(s, &value->u8, &mask->u8);
2663 break;
4fe3445a
PS
2664
2665 case MFS_TNL_FLAGS:
cb22974d 2666 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
3634dff8
BP
2667 error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
2668 break;
2669
61bf6666
JR
2670 case MFS_TCP_FLAGS:
2671 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2672 error = mf_from_tcp_flags_string(s, &value->be16, &mask->be16);
2673 break;
2674
3634dff8
BP
2675 default:
2676 NOT_REACHED();
6a885fd0 2677 }
3634dff8
BP
2678
2679 if (!error && !mf_is_mask_valid(mf, mask)) {
2680 error = xasprintf("%s: invalid mask for field %s", s, mf->name);
2681 }
2682 return error;
6a885fd0
BP
2683}
2684
2685/* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2686 * successful, otherwise a malloc()'d string describing the error. */
2687char *
2688mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2689{
2690 union mf_value mask;
2691 char *error;
2692
2693 error = mf_parse(mf, s, value, &mask);
2694 if (error) {
2695 return error;
2696 }
2697
2698 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2699 return xasprintf("%s: wildcards not allowed here", s);
2700 }
2701 return NULL;
2702}
2703
2704static void
2705mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2706 const uint8_t *maskp, struct ds *s)
2707{
2708 unsigned long long int integer;
2709 int i;
2710
cb22974d 2711 ovs_assert(mf->n_bytes <= 8);
6a885fd0
BP
2712
2713 integer = 0;
2714 for (i = 0; i < mf->n_bytes; i++) {
2715 integer = (integer << 8) | valuep[i];
2716 }
2717 if (mf->string == MFS_HEXADECIMAL) {
2718 ds_put_format(s, "%#llx", integer);
2719 } else {
2720 ds_put_format(s, "%lld", integer);
2721 }
2722
2723 if (maskp) {
2724 unsigned long long int mask;
2725
2726 mask = 0;
2727 for (i = 0; i < mf->n_bytes; i++) {
2728 mask = (mask << 8) | maskp[i];
2729 }
2730
2731 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2732 * not sure that that a bit-mask written in decimal is ever easier to
2733 * understand than the same bit-mask written in hexadecimal. */
2734 ds_put_format(s, "/%#llx", mask);
2735 }
2736}
2737
7257b535 2738static void
8f75dea3 2739mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
7257b535
BP
2740{
2741 const struct frag_handling *h;
7257b535 2742
eadef313 2743 mask &= FLOW_NW_FRAG_MASK;
8f75dea3 2744 value &= mask;
7257b535
BP
2745
2746 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2747 if (value == h->value && mask == h->mask) {
2748 ds_put_cstr(s, h->name);
2749 return;
2750 }
2751 }
2752 ds_put_cstr(s, "<error>");
2753}
2754
4fe3445a
PS
2755static void
2756mf_format_tnl_flags_string(const ovs_be16 *valuep, struct ds *s)
2757{
2758 format_flags(s, flow_tun_flag_to_string, ntohs(*valuep), '|');
2759}
2760
61bf6666
JR
2761static void
2762mf_format_tcp_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2763{
2764 format_flags_masked(s, NULL, packet_tcp_flag_to_string, ntohs(value),
2765 TCP_FLAGS(mask));
2766}
2767
6a885fd0
BP
2768/* Appends to 's' a string representation of field 'mf' whose value is in
2769 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2770void
2771mf_format(const struct mf_field *mf,
2772 const union mf_value *value, const union mf_value *mask,
2773 struct ds *s)
2774{
2775 if (mask) {
2776 if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
2777 ds_put_cstr(s, "ANY");
2778 return;
2779 } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
2780 mask = NULL;
2781 }
2782 }
2783
2784 switch (mf->string) {
72333065
JR
2785 case MFS_OFP_PORT_OXM:
2786 if (!mask) {
4e022ec0 2787 ofp_port_t port;
bc146369 2788 ofputil_port_from_ofp11(value->be32, &port);
72333065
JR
2789 ofputil_format_port(port, s);
2790 break;
2791 }
2792 /* fall through */
6a885fd0
BP
2793 case MFS_OFP_PORT:
2794 if (!mask) {
4e022ec0 2795 ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
6a885fd0
BP
2796 break;
2797 }
2798 /* fall through */
2799 case MFS_DECIMAL:
2800 case MFS_HEXADECIMAL:
2801 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2802 break;
2803
2804 case MFS_ETHERNET:
3b4d8ad3 2805 eth_format_masked(value->mac, mask->mac, s);
6a885fd0
BP
2806 break;
2807
2808 case MFS_IPV4:
b8266395 2809 ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
6a885fd0
BP
2810 break;
2811
2812 case MFS_IPV6:
2813 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2814 break;
2815
7257b535 2816 case MFS_FRAG:
8f75dea3 2817 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
7257b535
BP
2818 break;
2819
4fe3445a
PS
2820 case MFS_TNL_FLAGS:
2821 mf_format_tnl_flags_string(&value->be16, s);
2822 break;
2823
61bf6666
JR
2824 case MFS_TCP_FLAGS:
2825 mf_format_tcp_flags_string(value->be16,
2826 mask ? mask->be16 : OVS_BE16_MAX, s);
2827 break;
2828
6a885fd0
BP
2829 default:
2830 NOT_REACHED();
2831 }
2832}
816fd533 2833\f
9bab681f
IY
2834/* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2835 * least-significant bits in 'x'.
2836 */
2837void
2838mf_write_subfield_flow(const struct mf_subfield *sf,
2839 const union mf_subvalue *x, struct flow *flow)
2840{
2841 const struct mf_field *field = sf->field;
2842 union mf_value value;
2843
2844 mf_get_value(field, flow, &value);
158edc8d 2845 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
9bab681f
IY
2846 sf->ofs, sf->n_bits);
2847 mf_set_flow_value(field, &value, flow);
2848}
2849
81a76618 2850/* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
1b35df45 2851 * least-significant bits in 'x'.
615660a9 2852 */
1b35df45
BP
2853void
2854mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
81a76618 2855 struct match *match)
1b35df45
BP
2856{
2857 const struct mf_field *field = sf->field;
2858 union mf_value value, mask;
2859
81a76618 2860 mf_get(field, match, &value, &mask);
1b35df45
BP
2861 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2862 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
81a76618 2863 mf_set(field, &value, &mask, match);
1b35df45
BP
2864}
2865
1b35df45
BP
2866/* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2867 * reading 'flow', e.g. as checked by mf_check_src(). */
2868void
2869mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2870 union mf_subvalue *x)
2871{
2872 union mf_value value;
2873
2874 mf_get_value(sf->field, flow, &value);
2875
2876 memset(x, 0, sizeof *x);
2877 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2878 x, sizeof *x, 0,
2879 sf->n_bits);
2880}
2881
816fd533
BP
2882/* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2883 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2884 * less. */
2885uint64_t
2886mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2887{
2888 union mf_value value;
2889
2890 mf_get_value(sf->field, flow, &value);
2891 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2892}
2893
2894/* Formats 'sf' into 's' in a format normally acceptable to
2895 * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if
2896 * sf->field has no NXM name.) */
2897void
2898mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
2899{
2900 if (!sf->field) {
2901 ds_put_cstr(s, "<unknown>");
2902 } else if (sf->field->nxm_name) {
2903 ds_put_cstr(s, sf->field->nxm_name);
2904 } else if (sf->field->nxm_header) {
2905 uint32_t header = sf->field->nxm_header;
2906 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
2907 } else {
2908 ds_put_cstr(s, sf->field->name);
2909 }
2910
33500edd 2911 if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
816fd533
BP
2912 ds_put_cstr(s, "[]");
2913 } else if (sf->n_bits == 1) {
2914 ds_put_format(s, "[%d]", sf->ofs);
2915 } else {
2916 ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
2917 }
2918}
2919
2920static const struct mf_field *
2921mf_parse_subfield_name(const char *name, int name_len, bool *wild)
2922{
2923 int i;
2924
2925 *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
2926 if (*wild) {
2927 name_len -= 2;
2928 }
2929
2930 for (i = 0; i < MFF_N_IDS; i++) {
2931 const struct mf_field *mf = mf_from_id(i);
2932
2933 if (mf->nxm_name
2934 && !strncmp(mf->nxm_name, name, name_len)
2935 && mf->nxm_name[name_len] == '\0') {
2936 return mf;
2937 }
b5e5143b
BP
2938 if (mf->oxm_name
2939 && !strncmp(mf->oxm_name, name, name_len)
2940 && mf->oxm_name[name_len] == '\0') {
2941 return mf;
2942 }
816fd533
BP
2943 }
2944
2945 return NULL;
2946}
2947
2948/* Parses a subfield from the beginning of '*sp' into 'sf'. If successful,
2949 * returns NULL and advances '*sp' to the first byte following the parsed
2950 * string. On failure, returns a malloc()'d error message, does not modify
2951 * '*sp', and does not properly initialize 'sf'.
2952 *
2953 * The syntax parsed from '*sp' takes the form "header[start..end]" where
2954 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2955 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2956 * may both be omitted (the [] are still required) to indicate an entire
2957 * field. */
bdda5aca 2958char * WARN_UNUSED_RESULT
816fd533
BP
2959mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
2960{
2961 const struct mf_field *field;
2962 const char *name;
2963 int start, end;
2964 const char *s;
2965 int name_len;
2966 bool wild;
2967
2968 s = *sp;
2969 name = s;
2970 name_len = strcspn(s, "[");
2971 if (s[name_len] != '[') {
2972 return xasprintf("%s: missing [ looking for field name", *sp);
2973 }
2974
2975 field = mf_parse_subfield_name(name, name_len, &wild);
2976 if (!field) {
2977 return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
2978 }
2979
2980 s += name_len;
c2c28dfd 2981 if (ovs_scan(s, "[%d..%d]", &start, &end)) {
816fd533 2982 /* Nothing to do. */
c2c28dfd 2983 } else if (ovs_scan(s, "[%d]", &start)) {
816fd533
BP
2984 end = start;
2985 } else if (!strncmp(s, "[]", 2)) {
2986 start = 0;
2987 end = field->n_bits - 1;
2988 } else {
2989 return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2990 "[<start>..<end>]", *sp);
2991 }
2992 s = strchr(s, ']') + 1;
2993
2994 if (start > end) {
2995 return xasprintf("%s: starting bit %d is after ending bit %d",
2996 *sp, start, end);
2997 } else if (start >= field->n_bits) {
2998 return xasprintf("%s: starting bit %d is not valid because field is "
2999 "only %d bits wide", *sp, start, field->n_bits);
3000 } else if (end >= field->n_bits){
3001 return xasprintf("%s: ending bit %d is not valid because field is "
3002 "only %d bits wide", *sp, end, field->n_bits);
3003 }
3004
3005 sf->field = field;
3006 sf->ofs = start;
3007 sf->n_bits = end - start + 1;
3008
3009 *sp = s;
3010 return NULL;
3011}
3012
bdda5aca
BP
3013/* Parses a subfield from the entirety of 's' into 'sf'. Returns NULL if
3014 * successful, otherwise a malloc()'d string describing the error. The caller
3015 * is responsible for freeing the returned string.
816fd533
BP
3016 *
3017 * The syntax parsed from 's' takes the form "header[start..end]" where
3018 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
3019 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
3020 * may both be omitted (the [] are still required) to indicate an entire
3021 * field. */
bdda5aca 3022char * WARN_UNUSED_RESULT
816fd533
BP
3023mf_parse_subfield(struct mf_subfield *sf, const char *s)
3024{
bdda5aca
BP
3025 char *error = mf_parse_subfield__(sf, &s);
3026 if (!error && s[0]) {
3027 error = xstrdup("unexpected input following field syntax");
816fd533 3028 }
bdda5aca 3029 return error;
816fd533 3030}
9bab681f
IY
3031
3032void
3033mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
3034{
3035 int i;
3036
3037 for (i = 0; i < ARRAY_SIZE(subvalue->u8); i++) {
3038 if (subvalue->u8[i]) {
3039 ds_put_format(s, "0x%"PRIx8, subvalue->u8[i]);
3040 for (i++; i < ARRAY_SIZE(subvalue->u8); i++) {
3041 ds_put_format(s, "%02"PRIx8, subvalue->u8[i]);
3042 }
3043 return;
3044 }
3045 }
3046 ds_put_char(s, '0');
3047}