]> git.proxmox.com Git - mirror_ovs.git/blame - lib/meta-flow.c
ovs-thread: Replace ovsthread_counter by more general ovsthread_stats.
[mirror_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 928 case MFF_MPLS_LABEL:
8bfd0fda 929 return !(wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK));
b02475c5 930 case MFF_MPLS_TC:
8bfd0fda 931 return !(wc->masks.mpls_lse[1] & htonl(MPLS_TC_MASK));
b02475c5 932 case MFF_MPLS_BOS:
8bfd0fda 933 return !(wc->masks.mpls_lse[2] & htonl(MPLS_BOS_MASK));
b02475c5 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:
428b2edd 988 OVS_NOT_REACHED();
6a885fd0
BP
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
428b2edd 1019 OVS_NOT_REACHED();
6a885fd0
BP
1020}
1021
6a885fd0
BP
1022/* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
1023bool
1024mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
1025{
1026 switch (mf->prereqs) {
1027 case MFP_NONE:
1028 return true;
1029
1030 case MFP_ARP:
8087f5ff
MM
1031 return (flow->dl_type == htons(ETH_TYPE_ARP) ||
1032 flow->dl_type == htons(ETH_TYPE_RARP));
6a885fd0
BP
1033 case MFP_IPV4:
1034 return flow->dl_type == htons(ETH_TYPE_IP);
1035 case MFP_IPV6:
1036 return flow->dl_type == htons(ETH_TYPE_IPV6);
8069b0da 1037 case MFP_VLAN_VID:
5921e99a 1038 return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
b02475c5
SH
1039 case MFP_MPLS:
1040 return eth_type_mpls(flow->dl_type);
6a885fd0
BP
1041 case MFP_IP_ANY:
1042 return is_ip_any(flow);
1043
1044 case MFP_TCP:
1045 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
1046 case MFP_UDP:
1047 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
0d56eaf2
JS
1048 case MFP_SCTP:
1049 return is_ip_any(flow) && flow->nw_proto == IPPROTO_SCTP;
268a95e0
BP
1050 case MFP_ICMPV4:
1051 return is_icmpv4(flow);
6a885fd0
BP
1052 case MFP_ICMPV6:
1053 return is_icmpv6(flow);
6a885fd0
BP
1054
1055 case MFP_ND:
1056 return (is_icmpv6(flow)
3ee8a9f0
BP
1057 && flow->tp_dst == htons(0)
1058 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
1059 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
6a885fd0
BP
1060 case MFP_ND_SOLICIT:
1061 return (is_icmpv6(flow)
3ee8a9f0
BP
1062 && flow->tp_dst == htons(0)
1063 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
6a885fd0
BP
1064 case MFP_ND_ADVERT:
1065 return (is_icmpv6(flow)
3ee8a9f0
BP
1066 && flow->tp_dst == htons(0)
1067 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
6a885fd0
BP
1068 }
1069
428b2edd 1070 OVS_NOT_REACHED();
6a885fd0
BP
1071}
1072
b283836c
JR
1073/* Set field and it's prerequisities in the mask.
1074 * This is only ever called for writeable 'mf's, but we do not make the
1075 * distinction here. */
1076void
1077mf_mask_field_and_prereqs(const struct mf_field *mf, struct flow *mask)
1078{
1079 static const union mf_value exact_match_mask = MF_EXACT_MASK_INITIALIZER;
1080
1081 mf_set_flow_value(mf, &exact_match_mask, mask);
1082
1083 switch (mf->prereqs) {
1084 case MFP_ND:
1085 case MFP_ND_SOLICIT:
1086 case MFP_ND_ADVERT:
1087 mask->tp_src = OVS_BE16_MAX;
1088 mask->tp_dst = OVS_BE16_MAX;
1089 /* Fall through. */
1090 case MFP_TCP:
1091 case MFP_UDP:
1092 case MFP_SCTP:
1093 case MFP_ICMPV4:
1094 case MFP_ICMPV6:
1095 mask->nw_proto = 0xff;
1096 /* Fall through. */
1097 case MFP_ARP:
1098 case MFP_IPV4:
1099 case MFP_IPV6:
1100 case MFP_MPLS:
1101 case MFP_IP_ANY:
1102 mask->dl_type = OVS_BE16_MAX;
1103 break;
1104 case MFP_VLAN_VID:
1105 mask->vlan_tci |= htons(VLAN_CFI);
1106 break;
1107 case MFP_NONE:
1108 break;
1109 }
1110}
1111
1112
6a885fd0
BP
1113/* Returns true if 'value' may be a valid value *as part of a masked match*,
1114 * false otherwise.
1115 *
1116 * A value is not rejected just because it is not valid for the field in
1117 * question, but only if it doesn't make sense to test the bits in question at
1118 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
1119 * without the VLAN_CFI bit being set, but we can't reject those values because
1120 * it is still legitimate to test just for those bits (see the documentation
1121 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
530180fd 1122 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
6a885fd0
BP
1123bool
1124mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
1125{
1126 switch (mf->id) {
1127 case MFF_TUN_ID:
4fe3445a
PS
1128 case MFF_TUN_SRC:
1129 case MFF_TUN_DST:
1130 case MFF_TUN_TOS:
1131 case MFF_TUN_TTL:
1132 case MFF_TUN_FLAGS:
969fc56c 1133 case MFF_METADATA:
6a885fd0 1134 case MFF_IN_PORT:
1b567fb9 1135 case MFF_SKB_PRIORITY:
1362e248 1136 case MFF_PKT_MARK:
0d7e2fe4 1137 CASE_MFF_REGS:
6a885fd0
BP
1138 case MFF_ETH_SRC:
1139 case MFF_ETH_DST:
1140 case MFF_ETH_TYPE:
1141 case MFF_VLAN_TCI:
1142 case MFF_IPV4_SRC:
1143 case MFF_IPV4_DST:
1144 case MFF_IPV6_SRC:
1145 case MFF_IPV6_DST:
1146 case MFF_IP_PROTO:
a61680c6 1147 case MFF_IP_TTL:
6a885fd0
BP
1148 case MFF_ARP_SPA:
1149 case MFF_ARP_TPA:
1150 case MFF_ARP_SHA:
1151 case MFF_ARP_THA:
1152 case MFF_TCP_SRC:
1153 case MFF_TCP_DST:
1154 case MFF_UDP_SRC:
1155 case MFF_UDP_DST:
0d56eaf2
JS
1156 case MFF_SCTP_SRC:
1157 case MFF_SCTP_DST:
268a95e0
BP
1158 case MFF_ICMPV4_TYPE:
1159 case MFF_ICMPV4_CODE:
1160 case MFF_ICMPV6_TYPE:
1161 case MFF_ICMPV6_CODE:
6a885fd0
BP
1162 case MFF_ND_TARGET:
1163 case MFF_ND_SLL:
1164 case MFF_ND_TLL:
1165 return true;
1166
72333065 1167 case MFF_IN_PORT_OXM: {
4e022ec0 1168 ofp_port_t port;
72333065
JR
1169 return !ofputil_port_from_ofp11(value->be32, &port);
1170 }
1171
530180fd 1172 case MFF_IP_DSCP:
7257b535 1173 return !(value->u8 & ~IP_DSCP_MASK);
1638b906
BP
1174 case MFF_IP_DSCP_SHIFTED:
1175 return !(value->u8 & (~IP_DSCP_MASK >> 2));
530180fd
JP
1176 case MFF_IP_ECN:
1177 return !(value->u8 & ~IP_ECN_MASK);
7257b535 1178 case MFF_IP_FRAG:
eadef313 1179 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
dc235f7f
JR
1180 case MFF_TCP_FLAGS:
1181 return !(value->be16 & ~htons(0x0fff));
6a885fd0
BP
1182
1183 case MFF_ARP_OP:
1184 return !(value->be16 & htons(0xff00));
1185
441c57a9 1186 case MFF_DL_VLAN:
6a885fd0 1187 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
cc34bc8c
BP
1188 case MFF_VLAN_VID:
1189 return !(value->be16 & htons(VLAN_PCP_MASK));
6a885fd0 1190
441c57a9 1191 case MFF_DL_VLAN_PCP:
cc34bc8c 1192 case MFF_VLAN_PCP:
24016f5a 1193 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
6a885fd0 1194
fa8223b7
JP
1195 case MFF_IPV6_LABEL:
1196 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
1197
b02475c5
SH
1198 case MFF_MPLS_LABEL:
1199 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
1200
1201 case MFF_MPLS_TC:
1202 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
1203
1204 case MFF_MPLS_BOS:
1205 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
1206
6a885fd0
BP
1207 case MFF_N_IDS:
1208 default:
428b2edd 1209 OVS_NOT_REACHED();
6a885fd0
BP
1210 }
1211}
1212
1213/* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
1214 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
1215void
1216mf_get_value(const struct mf_field *mf, const struct flow *flow,
1217 union mf_value *value)
1218{
1219 switch (mf->id) {
1220 case MFF_TUN_ID:
296e07ac 1221 value->be64 = flow->tunnel.tun_id;
6a885fd0 1222 break;
4fe3445a
PS
1223 case MFF_TUN_SRC:
1224 value->be32 = flow->tunnel.ip_src;
1225 break;
1226 case MFF_TUN_DST:
1227 value->be32 = flow->tunnel.ip_dst;
1228 break;
1229 case MFF_TUN_FLAGS:
1230 value->be16 = htons(flow->tunnel.flags);
1231 break;
1232 case MFF_TUN_TTL:
1233 value->u8 = flow->tunnel.ip_ttl;
1234 break;
1235 case MFF_TUN_TOS:
1236 value->u8 = flow->tunnel.ip_tos;
1237 break;
1238
969fc56c
JS
1239 case MFF_METADATA:
1240 value->be64 = flow->metadata;
1241 break;
6a885fd0
BP
1242
1243 case MFF_IN_PORT:
4e022ec0 1244 value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
6a885fd0 1245 break;
72333065 1246 case MFF_IN_PORT_OXM:
4e022ec0 1247 value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
72333065
JR
1248 break;
1249
1b567fb9 1250 case MFF_SKB_PRIORITY:
a81f0a44 1251 value->be32 = htonl(flow->skb_priority);
1b567fb9
AA
1252 break;
1253
1362e248
JG
1254 case MFF_PKT_MARK:
1255 value->be32 = htonl(flow->pkt_mark);
1b567fb9
AA
1256 break;
1257
0d7e2fe4 1258 CASE_MFF_REGS:
2f98b0b7 1259 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
6a885fd0
BP
1260 break;
1261
1262 case MFF_ETH_SRC:
1263 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
1264 break;
1265
1266 case MFF_ETH_DST:
1267 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
1268 break;
1269
1270 case MFF_ETH_TYPE:
1271 value->be16 = flow->dl_type;
1272 break;
1273
1274 case MFF_VLAN_TCI:
1275 value->be16 = flow->vlan_tci;
1276 break;
1277
441c57a9 1278 case MFF_DL_VLAN:
6a885fd0
BP
1279 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
1280 break;
cc34bc8c
BP
1281 case MFF_VLAN_VID:
1282 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
1283 break;
6a885fd0 1284
441c57a9 1285 case MFF_DL_VLAN_PCP:
cc34bc8c 1286 case MFF_VLAN_PCP:
6a885fd0
BP
1287 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
1288 break;
1289
b02475c5 1290 case MFF_MPLS_LABEL:
8bfd0fda 1291 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse[0]));
b02475c5
SH
1292 break;
1293
1294 case MFF_MPLS_TC:
8bfd0fda 1295 value->u8 = mpls_lse_to_tc(flow->mpls_lse[0]);
b02475c5
SH
1296 break;
1297
1298 case MFF_MPLS_BOS:
8bfd0fda
BP
1299 value->u8 = mpls_lse_to_bos(flow->mpls_lse[0]);
1300 break;
b02475c5 1301
6a885fd0
BP
1302 case MFF_IPV4_SRC:
1303 value->be32 = flow->nw_src;
1304 break;
1305
1306 case MFF_IPV4_DST:
1307 value->be32 = flow->nw_dst;
1308 break;
1309
1310 case MFF_IPV6_SRC:
1311 value->ipv6 = flow->ipv6_src;
1312 break;
1313
1314 case MFF_IPV6_DST:
1315 value->ipv6 = flow->ipv6_dst;
1316 break;
1317
fa8223b7
JP
1318 case MFF_IPV6_LABEL:
1319 value->be32 = flow->ipv6_label;
1320 break;
1321
6a885fd0
BP
1322 case MFF_IP_PROTO:
1323 value->u8 = flow->nw_proto;
1324 break;
1325
530180fd 1326 case MFF_IP_DSCP:
eadef313 1327 value->u8 = flow->nw_tos & IP_DSCP_MASK;
7257b535
BP
1328 break;
1329
1638b906
BP
1330 case MFF_IP_DSCP_SHIFTED:
1331 value->u8 = flow->nw_tos >> 2;
1332 break;
1333
530180fd 1334 case MFF_IP_ECN:
eadef313 1335 value->u8 = flow->nw_tos & IP_ECN_MASK;
530180fd
JP
1336 break;
1337
a61680c6
JP
1338 case MFF_IP_TTL:
1339 value->u8 = flow->nw_ttl;
1340 break;
1341
7257b535 1342 case MFF_IP_FRAG:
eadef313 1343 value->u8 = flow->nw_frag;
6a885fd0
BP
1344 break;
1345
1346 case MFF_ARP_OP:
1347 value->be16 = htons(flow->nw_proto);
1348 break;
1349
1350 case MFF_ARP_SPA:
1351 value->be32 = flow->nw_src;
1352 break;
1353
1354 case MFF_ARP_TPA:
1355 value->be32 = flow->nw_dst;
1356 break;
1357
1358 case MFF_ARP_SHA:
1359 case MFF_ND_SLL:
1360 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
1361 break;
1362
1363 case MFF_ARP_THA:
1364 case MFF_ND_TLL:
1365 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
1366 break;
1367
1368 case MFF_TCP_SRC:
6a885fd0 1369 case MFF_UDP_SRC:
0d56eaf2 1370 case MFF_SCTP_SRC:
6a885fd0
BP
1371 value->be16 = flow->tp_src;
1372 break;
1373
71baec06 1374 case MFF_TCP_DST:
6a885fd0 1375 case MFF_UDP_DST:
0d56eaf2 1376 case MFF_SCTP_DST:
6a885fd0
BP
1377 value->be16 = flow->tp_dst;
1378 break;
1379
dc235f7f
JR
1380 case MFF_TCP_FLAGS:
1381 value->be16 = flow->tcp_flags;
1382 break;
1383
268a95e0
BP
1384 case MFF_ICMPV4_TYPE:
1385 case MFF_ICMPV6_TYPE:
6a885fd0
BP
1386 value->u8 = ntohs(flow->tp_src);
1387 break;
1388
268a95e0
BP
1389 case MFF_ICMPV4_CODE:
1390 case MFF_ICMPV6_CODE:
6a885fd0
BP
1391 value->u8 = ntohs(flow->tp_dst);
1392 break;
1393
1394 case MFF_ND_TARGET:
1395 value->ipv6 = flow->nd_target;
1396 break;
1397
1398 case MFF_N_IDS:
1399 default:
428b2edd 1400 OVS_NOT_REACHED();
6a885fd0
BP
1401 }
1402}
1403
81a76618
BP
1404/* Makes 'match' match field 'mf' exactly, with the value matched taken from
1405 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
6a885fd0
BP
1406 * prerequisites. */
1407void
1408mf_set_value(const struct mf_field *mf,
81a76618 1409 const union mf_value *value, struct match *match)
6a885fd0
BP
1410{
1411 switch (mf->id) {
1412 case MFF_TUN_ID:
81a76618 1413 match_set_tun_id(match, value->be64);
6a885fd0 1414 break;
4fe3445a
PS
1415 case MFF_TUN_SRC:
1416 match_set_tun_src(match, value->be32);
1417 break;
1418 case MFF_TUN_DST:
1419 match_set_tun_dst(match, value->be32);
1420 break;
1421 case MFF_TUN_FLAGS:
1422 match_set_tun_flags(match, ntohs(value->be16));
1423 break;
1424 case MFF_TUN_TOS:
1425 match_set_tun_tos(match, value->u8);
1426 break;
1427 case MFF_TUN_TTL:
1428 match_set_tun_ttl(match, value->u8);
1429 break;
1430
969fc56c 1431 case MFF_METADATA:
81a76618 1432 match_set_metadata(match, value->be64);
969fc56c 1433 break;
6a885fd0
BP
1434
1435 case MFF_IN_PORT:
4e022ec0 1436 match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
6a885fd0
BP
1437 break;
1438
72333065 1439 case MFF_IN_PORT_OXM: {
4e022ec0 1440 ofp_port_t port;
bc146369 1441 ofputil_port_from_ofp11(value->be32, &port);
72333065
JR
1442 match_set_in_port(match, port);
1443 break;
1444 }
1445
1b567fb9
AA
1446 case MFF_SKB_PRIORITY:
1447 match_set_skb_priority(match, ntohl(value->be32));
1448 break;
1449
1362e248
JG
1450 case MFF_PKT_MARK:
1451 match_set_pkt_mark(match, ntohl(value->be32));
1b567fb9
AA
1452 break;
1453
0d7e2fe4 1454 CASE_MFF_REGS:
81a76618 1455 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
6a885fd0 1456 break;
6a885fd0
BP
1457
1458 case MFF_ETH_SRC:
81a76618 1459 match_set_dl_src(match, value->mac);
6a885fd0
BP
1460 break;
1461
1462 case MFF_ETH_DST:
81a76618 1463 match_set_dl_dst(match, value->mac);
6a885fd0
BP
1464 break;
1465
1466 case MFF_ETH_TYPE:
81a76618 1467 match_set_dl_type(match, value->be16);
6a885fd0
BP
1468 break;
1469
1470 case MFF_VLAN_TCI:
81a76618 1471 match_set_dl_tci(match, value->be16);
6a885fd0
BP
1472 break;
1473
441c57a9 1474 case MFF_DL_VLAN:
81a76618 1475 match_set_dl_vlan(match, value->be16);
6a885fd0 1476 break;
cc34bc8c 1477 case MFF_VLAN_VID:
81a76618 1478 match_set_vlan_vid(match, value->be16);
cc34bc8c 1479 break;
6a885fd0 1480
441c57a9 1481 case MFF_DL_VLAN_PCP:
cc34bc8c 1482 case MFF_VLAN_PCP:
81a76618 1483 match_set_dl_vlan_pcp(match, value->u8);
6a885fd0
BP
1484 break;
1485
b02475c5 1486 case MFF_MPLS_LABEL:
8bfd0fda 1487 match_set_mpls_label(match, 0, value->be32);
b02475c5
SH
1488 break;
1489
1490 case MFF_MPLS_TC:
8bfd0fda 1491 match_set_mpls_tc(match, 0, value->u8);
b02475c5
SH
1492 break;
1493
1494 case MFF_MPLS_BOS:
8bfd0fda
BP
1495 match_set_mpls_bos(match, 0, value->u8);
1496 break;
b02475c5 1497
6a885fd0 1498 case MFF_IPV4_SRC:
81a76618 1499 match_set_nw_src(match, value->be32);
6a885fd0
BP
1500 break;
1501
1502 case MFF_IPV4_DST:
81a76618 1503 match_set_nw_dst(match, value->be32);
6a885fd0
BP
1504 break;
1505
1506 case MFF_IPV6_SRC:
81a76618 1507 match_set_ipv6_src(match, &value->ipv6);
6a885fd0
BP
1508 break;
1509
1510 case MFF_IPV6_DST:
81a76618 1511 match_set_ipv6_dst(match, &value->ipv6);
6a885fd0
BP
1512 break;
1513
fa8223b7 1514 case MFF_IPV6_LABEL:
81a76618 1515 match_set_ipv6_label(match, value->be32);
fa8223b7
JP
1516 break;
1517
6a885fd0 1518 case MFF_IP_PROTO:
81a76618 1519 match_set_nw_proto(match, value->u8);
6a885fd0
BP
1520 break;
1521
530180fd 1522 case MFF_IP_DSCP:
81a76618 1523 match_set_nw_dscp(match, value->u8);
530180fd
JP
1524 break;
1525
1638b906
BP
1526 case MFF_IP_DSCP_SHIFTED:
1527 match_set_nw_dscp(match, value->u8 << 2);
1528 break;
1529
530180fd 1530 case MFF_IP_ECN:
81a76618 1531 match_set_nw_ecn(match, value->u8);
6a885fd0
BP
1532 break;
1533
a61680c6 1534 case MFF_IP_TTL:
81a76618 1535 match_set_nw_ttl(match, value->u8);
a61680c6
JP
1536 break;
1537
7257b535 1538 case MFF_IP_FRAG:
81a76618 1539 match_set_nw_frag(match, value->u8);
7257b535
BP
1540 break;
1541
6a885fd0 1542 case MFF_ARP_OP:
81a76618 1543 match_set_nw_proto(match, ntohs(value->be16));
6a885fd0
BP
1544 break;
1545
1546 case MFF_ARP_SPA:
81a76618 1547 match_set_nw_src(match, value->be32);
6a885fd0
BP
1548 break;
1549
1550 case MFF_ARP_TPA:
81a76618 1551 match_set_nw_dst(match, value->be32);
6a885fd0
BP
1552 break;
1553
1554 case MFF_ARP_SHA:
1555 case MFF_ND_SLL:
81a76618 1556 match_set_arp_sha(match, value->mac);
6a885fd0
BP
1557 break;
1558
1559 case MFF_ARP_THA:
1560 case MFF_ND_TLL:
81a76618 1561 match_set_arp_tha(match, value->mac);
6a885fd0
BP
1562 break;
1563
1564 case MFF_TCP_SRC:
6a885fd0 1565 case MFF_UDP_SRC:
0d56eaf2 1566 case MFF_SCTP_SRC:
81a76618 1567 match_set_tp_src(match, value->be16);
6a885fd0
BP
1568 break;
1569
71baec06 1570 case MFF_TCP_DST:
6a885fd0 1571 case MFF_UDP_DST:
0d56eaf2 1572 case MFF_SCTP_DST:
81a76618 1573 match_set_tp_dst(match, value->be16);
6a885fd0
BP
1574 break;
1575
dc235f7f
JR
1576 case MFF_TCP_FLAGS:
1577 match_set_tcp_flags(match, value->be16);
1578 break;
1579
268a95e0
BP
1580 case MFF_ICMPV4_TYPE:
1581 case MFF_ICMPV6_TYPE:
81a76618 1582 match_set_icmp_type(match, value->u8);
6a885fd0
BP
1583 break;
1584
268a95e0
BP
1585 case MFF_ICMPV4_CODE:
1586 case MFF_ICMPV6_CODE:
81a76618 1587 match_set_icmp_code(match, value->u8);
6a885fd0
BP
1588 break;
1589
1590 case MFF_ND_TARGET:
81a76618 1591 match_set_nd_target(match, &value->ipv6);
6a885fd0
BP
1592 break;
1593
1594 case MFF_N_IDS:
1595 default:
428b2edd 1596 OVS_NOT_REACHED();
6a885fd0
BP
1597 }
1598}
1599
5a0a5702
GS
1600/* Unwildcard 'mask' member field described by 'mf'. The caller is
1601 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
1602void
1603mf_mask_field(const struct mf_field *mf, struct flow *mask)
1604{
1605 static const union mf_value exact_match_mask = MF_EXACT_MASK_INITIALIZER;
1606
1607 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan()
1608 * as that will be considered as OFP10_VLAN_NONE. So consider it as a
1609 * special case. For the rest, calling mf_set_flow_value() is good
1610 * enough. */
1611 if (mf->id == MFF_DL_VLAN) {
1612 flow_set_dl_vlan(mask, htons(VLAN_VID_MASK));
1613 } else {
1614 mf_set_flow_value(mf, &exact_match_mask, mask);
1615 }
1616}
1617
910fb1d8
JP
1618/* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1619 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
28da1f8f
BP
1620void
1621mf_set_flow_value(const struct mf_field *mf,
1622 const union mf_value *value, struct flow *flow)
1623{
1624 switch (mf->id) {
1625 case MFF_TUN_ID:
296e07ac 1626 flow->tunnel.tun_id = value->be64;
28da1f8f 1627 break;
4fe3445a
PS
1628 case MFF_TUN_SRC:
1629 flow->tunnel.ip_src = value->be32;
1630 break;
1631 case MFF_TUN_DST:
1632 flow->tunnel.ip_dst = value->be32;
1633 break;
1634 case MFF_TUN_FLAGS:
1635 flow->tunnel.flags = ntohs(value->be16);
1636 break;
1637 case MFF_TUN_TOS:
1638 flow->tunnel.ip_tos = value->u8;
1639 break;
1640 case MFF_TUN_TTL:
1641 flow->tunnel.ip_ttl = value->u8;
1642 break;
1643
969fc56c
JS
1644 case MFF_METADATA:
1645 flow->metadata = value->be64;
1646 break;
28da1f8f
BP
1647
1648 case MFF_IN_PORT:
4e022ec0 1649 flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
28da1f8f
BP
1650 break;
1651
72333065 1652 case MFF_IN_PORT_OXM: {
4e022ec0 1653 ofp_port_t port;
bc146369 1654 ofputil_port_from_ofp11(value->be32, &port);
4e022ec0 1655 flow->in_port.ofp_port = port;
72333065
JR
1656 break;
1657 }
1658
1b567fb9
AA
1659 case MFF_SKB_PRIORITY:
1660 flow->skb_priority = ntohl(value->be32);
1661 break;
1662
1362e248
JG
1663 case MFF_PKT_MARK:
1664 flow->pkt_mark = ntohl(value->be32);
1b567fb9
AA
1665 break;
1666
0d7e2fe4 1667 CASE_MFF_REGS:
28da1f8f
BP
1668 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1669 break;
28da1f8f
BP
1670
1671 case MFF_ETH_SRC:
1672 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1673 break;
1674
1675 case MFF_ETH_DST:
b5a36e38 1676 memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
28da1f8f
BP
1677 break;
1678
1679 case MFF_ETH_TYPE:
1680 flow->dl_type = value->be16;
1681 break;
1682
1683 case MFF_VLAN_TCI:
1684 flow->vlan_tci = value->be16;
1685 break;
1686
441c57a9 1687 case MFF_DL_VLAN:
fb0451d9 1688 flow_set_dl_vlan(flow, value->be16);
28da1f8f 1689 break;
cc34bc8c
BP
1690 case MFF_VLAN_VID:
1691 flow_set_vlan_vid(flow, value->be16);
1692 break;
28da1f8f 1693
441c57a9 1694 case MFF_DL_VLAN_PCP:
cc34bc8c 1695 case MFF_VLAN_PCP:
28da1f8f
BP
1696 flow_set_vlan_pcp(flow, value->u8);
1697 break;
1698
b02475c5 1699 case MFF_MPLS_LABEL:
8bfd0fda 1700 flow_set_mpls_label(flow, 0, value->be32);
b02475c5
SH
1701 break;
1702
1703 case MFF_MPLS_TC:
8bfd0fda 1704 flow_set_mpls_tc(flow, 0, value->u8);
b02475c5
SH
1705 break;
1706
1707 case MFF_MPLS_BOS:
8bfd0fda
BP
1708 flow_set_mpls_bos(flow, 0, value->u8);
1709 break;
b02475c5 1710
28da1f8f
BP
1711 case MFF_IPV4_SRC:
1712 flow->nw_src = value->be32;
1713 break;
1714
1715 case MFF_IPV4_DST:
1716 flow->nw_dst = value->be32;
1717 break;
1718
1719 case MFF_IPV6_SRC:
1720 flow->ipv6_src = value->ipv6;
1721 break;
1722
1723 case MFF_IPV6_DST:
1724 flow->ipv6_dst = value->ipv6;
1725 break;
1726
1727 case MFF_IPV6_LABEL:
1728 flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1729 break;
1730
1731 case MFF_IP_PROTO:
1732 flow->nw_proto = value->u8;
1733 break;
1734
1735 case MFF_IP_DSCP:
1736 flow->nw_tos &= ~IP_DSCP_MASK;
1737 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1738 break;
1739
1638b906
BP
1740 case MFF_IP_DSCP_SHIFTED:
1741 flow->nw_tos &= ~IP_DSCP_MASK;
1742 flow->nw_tos |= value->u8 << 2;
1743 break;
1744
28da1f8f
BP
1745 case MFF_IP_ECN:
1746 flow->nw_tos &= ~IP_ECN_MASK;
1747 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1748 break;
1749
1750 case MFF_IP_TTL:
1751 flow->nw_ttl = value->u8;
1752 break;
1753
1754 case MFF_IP_FRAG:
c8abb8af 1755 flow->nw_frag = value->u8 & FLOW_NW_FRAG_MASK;
28da1f8f
BP
1756 break;
1757
1758 case MFF_ARP_OP:
1759 flow->nw_proto = ntohs(value->be16);
1760 break;
1761
1762 case MFF_ARP_SPA:
1763 flow->nw_src = value->be32;
1764 break;
1765
1766 case MFF_ARP_TPA:
1767 flow->nw_dst = value->be32;
1768 break;
1769
1770 case MFF_ARP_SHA:
1771 case MFF_ND_SLL:
1772 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1773 break;
1774
1775 case MFF_ARP_THA:
1776 case MFF_ND_TLL:
1777 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1778 break;
1779
1780 case MFF_TCP_SRC:
1781 case MFF_UDP_SRC:
0d56eaf2 1782 case MFF_SCTP_SRC:
28da1f8f
BP
1783 flow->tp_src = value->be16;
1784 break;
1785
1786 case MFF_TCP_DST:
1787 case MFF_UDP_DST:
0d56eaf2 1788 case MFF_SCTP_DST:
28da1f8f
BP
1789 flow->tp_dst = value->be16;
1790 break;
1791
dc235f7f
JR
1792 case MFF_TCP_FLAGS:
1793 flow->tcp_flags = value->be16;
1794 break;
1795
28da1f8f
BP
1796 case MFF_ICMPV4_TYPE:
1797 case MFF_ICMPV6_TYPE:
1798 flow->tp_src = htons(value->u8);
1799 break;
1800
1801 case MFF_ICMPV4_CODE:
1802 case MFF_ICMPV6_CODE:
1803 flow->tp_dst = htons(value->u8);
1804 break;
1805
1806 case MFF_ND_TARGET:
1807 flow->nd_target = value->ipv6;
1808 break;
1809
1810 case MFF_N_IDS:
1811 default:
428b2edd 1812 OVS_NOT_REACHED();
28da1f8f
BP
1813 }
1814}
1815
ccbe50f8
BP
1816/* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1817 *
1818 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1819 * prerequisites. */
1820bool
1821mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1822{
1823 union mf_value value;
1824
1825 mf_get_value(mf, flow, &value);
1826 return is_all_zeros((const uint8_t *) &value, mf->n_bytes);
1827}
1828
81a76618 1829/* Makes 'match' wildcard field 'mf'.
6a885fd0 1830 *
81a76618 1831 * The caller is responsible for ensuring that 'match' meets 'mf''s
6a885fd0
BP
1832 * prerequisites. */
1833void
81a76618 1834mf_set_wild(const struct mf_field *mf, struct match *match)
6a885fd0
BP
1835{
1836 switch (mf->id) {
1837 case MFF_TUN_ID:
81a76618 1838 match_set_tun_id_masked(match, htonll(0), htonll(0));
6a885fd0 1839 break;
4fe3445a
PS
1840 case MFF_TUN_SRC:
1841 match_set_tun_src_masked(match, htonl(0), htonl(0));
1842 break;
1843 case MFF_TUN_DST:
1844 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1845 break;
1846 case MFF_TUN_FLAGS:
1847 match_set_tun_flags_masked(match, 0, 0);
1848 break;
1849 case MFF_TUN_TOS:
1850 match_set_tun_tos_masked(match, 0, 0);
1851 break;
1852 case MFF_TUN_TTL:
1853 match_set_tun_ttl_masked(match, 0, 0);
1854 break;
1855
969fc56c 1856 case MFF_METADATA:
81a76618 1857 match_set_metadata_masked(match, htonll(0), htonll(0));
801d2b2a 1858 break;
6a885fd0
BP
1859
1860 case MFF_IN_PORT:
72333065 1861 case MFF_IN_PORT_OXM:
4e022ec0
AW
1862 match->flow.in_port.ofp_port = 0;
1863 match->wc.masks.in_port.ofp_port = 0;
6a885fd0
BP
1864 break;
1865
1b567fb9
AA
1866 case MFF_SKB_PRIORITY:
1867 match->flow.skb_priority = 0;
1868 match->wc.masks.skb_priority = 0;
1869 break;
1870
1362e248
JG
1871 case MFF_PKT_MARK:
1872 match->flow.pkt_mark = 0;
1873 match->wc.masks.pkt_mark = 0;
1b567fb9
AA
1874 break;
1875
0d7e2fe4 1876 CASE_MFF_REGS:
81a76618 1877 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
e9358af6 1878 break;
6a885fd0
BP
1879
1880 case MFF_ETH_SRC:
81a76618
BP
1881 memset(match->flow.dl_src, 0, ETH_ADDR_LEN);
1882 memset(match->wc.masks.dl_src, 0, ETH_ADDR_LEN);
6a885fd0
BP
1883 break;
1884
1885 case MFF_ETH_DST:
81a76618
BP
1886 memset(match->flow.dl_dst, 0, ETH_ADDR_LEN);
1887 memset(match->wc.masks.dl_dst, 0, ETH_ADDR_LEN);
6a885fd0
BP
1888 break;
1889
1890 case MFF_ETH_TYPE:
81a76618
BP
1891 match->flow.dl_type = htons(0);
1892 match->wc.masks.dl_type = htons(0);
6a885fd0
BP
1893 break;
1894
1895 case MFF_VLAN_TCI:
81a76618 1896 match_set_dl_tci_masked(match, htons(0), htons(0));
6a885fd0
BP
1897 break;
1898
441c57a9 1899 case MFF_DL_VLAN:
cc34bc8c 1900 case MFF_VLAN_VID:
81a76618 1901 match_set_any_vid(match);
6a885fd0
BP
1902 break;
1903
441c57a9 1904 case MFF_DL_VLAN_PCP:
cc34bc8c 1905 case MFF_VLAN_PCP:
81a76618 1906 match_set_any_pcp(match);
6a885fd0
BP
1907 break;
1908
b02475c5 1909 case MFF_MPLS_LABEL:
8bfd0fda 1910 match_set_any_mpls_label(match, 0);
b02475c5
SH
1911 break;
1912
1913 case MFF_MPLS_TC:
8bfd0fda 1914 match_set_any_mpls_tc(match, 0);
b02475c5
SH
1915 break;
1916
1917 case MFF_MPLS_BOS:
8bfd0fda
BP
1918 match_set_any_mpls_bos(match, 0);
1919 break;
b02475c5 1920
6a885fd0
BP
1921 case MFF_IPV4_SRC:
1922 case MFF_ARP_SPA:
81a76618 1923 match_set_nw_src_masked(match, htonl(0), htonl(0));
6a885fd0
BP
1924 break;
1925
1926 case MFF_IPV4_DST:
1927 case MFF_ARP_TPA:
81a76618 1928 match_set_nw_dst_masked(match, htonl(0), htonl(0));
6a885fd0
BP
1929 break;
1930
1931 case MFF_IPV6_SRC:
81a76618
BP
1932 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1933 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
6a885fd0
BP
1934 break;
1935
1936 case MFF_IPV6_DST:
81a76618
BP
1937 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1938 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
6a885fd0
BP
1939 break;
1940
fa8223b7 1941 case MFF_IPV6_LABEL:
81a76618
BP
1942 match->wc.masks.ipv6_label = htonl(0);
1943 match->flow.ipv6_label = htonl(0);
fa8223b7
JP
1944 break;
1945
6a885fd0 1946 case MFF_IP_PROTO:
81a76618
BP
1947 match->wc.masks.nw_proto = 0;
1948 match->flow.nw_proto = 0;
6a885fd0
BP
1949 break;
1950
530180fd 1951 case MFF_IP_DSCP:
1638b906 1952 case MFF_IP_DSCP_SHIFTED:
81a76618
BP
1953 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1954 match->flow.nw_tos &= ~IP_DSCP_MASK;
7257b535
BP
1955 break;
1956
530180fd 1957 case MFF_IP_ECN:
81a76618
BP
1958 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1959 match->flow.nw_tos &= ~IP_ECN_MASK;
530180fd
JP
1960 break;
1961
a61680c6 1962 case MFF_IP_TTL:
81a76618
BP
1963 match->wc.masks.nw_ttl = 0;
1964 match->flow.nw_ttl = 0;
a61680c6
JP
1965 break;
1966
7257b535 1967 case MFF_IP_FRAG:
81a76618
BP
1968 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
1969 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
6a885fd0
BP
1970 break;
1971
1972 case MFF_ARP_OP:
81a76618
BP
1973 match->wc.masks.nw_proto = 0;
1974 match->flow.nw_proto = 0;
6a885fd0
BP
1975 break;
1976
1977 case MFF_ARP_SHA:
1978 case MFF_ND_SLL:
81a76618
BP
1979 memset(match->flow.arp_sha, 0, ETH_ADDR_LEN);
1980 memset(match->wc.masks.arp_sha, 0, ETH_ADDR_LEN);
6a885fd0
BP
1981 break;
1982
1983 case MFF_ARP_THA:
1984 case MFF_ND_TLL:
81a76618
BP
1985 memset(match->flow.arp_tha, 0, ETH_ADDR_LEN);
1986 memset(match->wc.masks.arp_tha, 0, ETH_ADDR_LEN);
6a885fd0
BP
1987 break;
1988
1989 case MFF_TCP_SRC:
1990 case MFF_UDP_SRC:
0d56eaf2 1991 case MFF_SCTP_SRC:
268a95e0
BP
1992 case MFF_ICMPV4_TYPE:
1993 case MFF_ICMPV6_TYPE:
81a76618
BP
1994 match->wc.masks.tp_src = htons(0);
1995 match->flow.tp_src = htons(0);
6a885fd0
BP
1996 break;
1997
1998 case MFF_TCP_DST:
1999 case MFF_UDP_DST:
0d56eaf2 2000 case MFF_SCTP_DST:
268a95e0
BP
2001 case MFF_ICMPV4_CODE:
2002 case MFF_ICMPV6_CODE:
81a76618
BP
2003 match->wc.masks.tp_dst = htons(0);
2004 match->flow.tp_dst = htons(0);
6a885fd0
BP
2005 break;
2006
dc235f7f
JR
2007 case MFF_TCP_FLAGS:
2008 match->wc.masks.tcp_flags = htons(0);
2009 match->flow.tcp_flags = htons(0);
2010 break;
2011
6a885fd0 2012 case MFF_ND_TARGET:
81a76618
BP
2013 memset(&match->wc.masks.nd_target, 0,
2014 sizeof match->wc.masks.nd_target);
2015 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
6a885fd0
BP
2016 break;
2017
2018 case MFF_N_IDS:
2019 default:
428b2edd 2020 OVS_NOT_REACHED();
6a885fd0
BP
2021 }
2022}
2023
81a76618 2024/* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
6a885fd0
BP
2025 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
2026 * with a 1-bit indicating that the corresponding value bit must match and a
2027 * 0-bit indicating a don't-care.
2028 *
2029 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
81a76618
BP
2030 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
2031 * call is equivalent to mf_set_wild(mf, match).
6a885fd0
BP
2032 *
2033 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
81a76618 2034 * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
db0b6c29 2035enum ofputil_protocol
6a885fd0
BP
2036mf_set(const struct mf_field *mf,
2037 const union mf_value *value, const union mf_value *mask,
81a76618 2038 struct match *match)
6a885fd0
BP
2039{
2040 if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
81a76618 2041 mf_set_value(mf, value, match);
db0b6c29 2042 return mf->usable_protocols;
6a885fd0 2043 } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
81a76618 2044 mf_set_wild(mf, match);
db0b6c29 2045 return OFPUTIL_P_ANY;
6a885fd0
BP
2046 }
2047
2048 switch (mf->id) {
2049 case MFF_IN_PORT:
72333065 2050 case MFF_IN_PORT_OXM:
1b567fb9 2051 case MFF_SKB_PRIORITY:
6a885fd0 2052 case MFF_ETH_TYPE:
441c57a9
BP
2053 case MFF_DL_VLAN:
2054 case MFF_DL_VLAN_PCP:
cc34bc8c 2055 case MFF_VLAN_PCP:
b02475c5
SH
2056 case MFF_MPLS_LABEL:
2057 case MFF_MPLS_TC:
2058 case MFF_MPLS_BOS:
6a885fd0 2059 case MFF_IP_PROTO:
a61680c6 2060 case MFF_IP_TTL:
530180fd 2061 case MFF_IP_DSCP:
1638b906 2062 case MFF_IP_DSCP_SHIFTED:
530180fd 2063 case MFF_IP_ECN:
6a885fd0 2064 case MFF_ARP_OP:
268a95e0
BP
2065 case MFF_ICMPV4_TYPE:
2066 case MFF_ICMPV4_CODE:
2067 case MFF_ICMPV6_TYPE:
2068 case MFF_ICMPV6_CODE:
af69bf19 2069 return OFPUTIL_P_NONE;
6a885fd0
BP
2070
2071 case MFF_TUN_ID:
81a76618 2072 match_set_tun_id_masked(match, value->be64, mask->be64);
6a885fd0 2073 break;
4fe3445a
PS
2074 case MFF_TUN_SRC:
2075 match_set_tun_src_masked(match, value->be32, mask->be32);
2076 break;
2077 case MFF_TUN_DST:
2078 match_set_tun_dst_masked(match, value->be32, mask->be32);
2079 break;
2080 case MFF_TUN_FLAGS:
2081 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
2082 break;
2083 case MFF_TUN_TTL:
2084 match_set_tun_ttl_masked(match, value->u8, mask->u8);
2085 break;
2086 case MFF_TUN_TOS:
2087 match_set_tun_tos_masked(match, value->u8, mask->u8);
2088 break;
2089
969fc56c 2090 case MFF_METADATA:
81a76618 2091 match_set_metadata_masked(match, value->be64, mask->be64);
969fc56c 2092 break;
6a885fd0 2093
0d7e2fe4 2094 CASE_MFF_REGS:
81a76618
BP
2095 match_set_reg_masked(match, mf->id - MFF_REG0,
2096 ntohl(value->be32), ntohl(mask->be32));
6a885fd0 2097 break;
ac923e91
JG
2098
2099 case MFF_PKT_MARK:
2100 match_set_pkt_mark_masked(match, ntohl(value->be32),
2101 ntohl(mask->be32));
2102 break;
6a885fd0
BP
2103
2104 case MFF_ETH_DST:
81a76618 2105 match_set_dl_dst_masked(match, value->mac, mask->mac);
73c0ce34
JS
2106 break;
2107
2108 case MFF_ETH_SRC:
81a76618 2109 match_set_dl_src_masked(match, value->mac, mask->mac);
6a885fd0
BP
2110 break;
2111
e878338b
SH
2112 case MFF_ARP_SHA:
2113 case MFF_ND_SLL:
81a76618 2114 match_set_arp_sha_masked(match, value->mac, mask->mac);
e878338b
SH
2115 break;
2116
2117 case MFF_ARP_THA:
2118 case MFF_ND_TLL:
81a76618 2119 match_set_arp_tha_masked(match, value->mac, mask->mac);
e878338b
SH
2120 break;
2121
6a885fd0 2122 case MFF_VLAN_TCI:
81a76618 2123 match_set_dl_tci_masked(match, value->be16, mask->be16);
6a885fd0
BP
2124 break;
2125
cc34bc8c 2126 case MFF_VLAN_VID:
81a76618 2127 match_set_vlan_vid_masked(match, value->be16, mask->be16);
cc34bc8c
BP
2128 break;
2129
6a885fd0 2130 case MFF_IPV4_SRC:
81a76618 2131 match_set_nw_src_masked(match, value->be32, mask->be32);
db0b6c29 2132 goto cidr_check;
6a885fd0
BP
2133
2134 case MFF_IPV4_DST:
81a76618 2135 match_set_nw_dst_masked(match, value->be32, mask->be32);
db0b6c29 2136 goto cidr_check;
6a885fd0
BP
2137
2138 case MFF_IPV6_SRC:
81a76618 2139 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
6a885fd0
BP
2140 break;
2141
2142 case MFF_IPV6_DST:
81a76618 2143 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
6a885fd0
BP
2144 break;
2145
32455024
SH
2146 case MFF_IPV6_LABEL:
2147 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
81a76618 2148 mf_set_value(mf, value, match);
32455024 2149 } else {
81a76618 2150 match_set_ipv6_label_masked(match, value->be32, mask->be32);
32455024
SH
2151 }
2152 break;
2153
47284b1f 2154 case MFF_ND_TARGET:
81a76618 2155 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
47284b1f
AA
2156 break;
2157
7257b535 2158 case MFF_IP_FRAG:
81a76618 2159 match_set_nw_frag_masked(match, value->u8, mask->u8);
7257b535
BP
2160 break;
2161
6a885fd0 2162 case MFF_ARP_SPA:
81a76618 2163 match_set_nw_src_masked(match, value->be32, mask->be32);
db0b6c29 2164 goto cidr_check;
6a885fd0
BP
2165
2166 case MFF_ARP_TPA:
81a76618 2167 match_set_nw_dst_masked(match, value->be32, mask->be32);
db0b6c29 2168 goto cidr_check;
6a885fd0 2169
73f33563
BP
2170 case MFF_TCP_SRC:
2171 case MFF_UDP_SRC:
0d56eaf2 2172 case MFF_SCTP_SRC:
81a76618 2173 match_set_tp_src_masked(match, value->be16, mask->be16);
73f33563
BP
2174 break;
2175
2176 case MFF_TCP_DST:
2177 case MFF_UDP_DST:
0d56eaf2 2178 case MFF_SCTP_DST:
81a76618 2179 match_set_tp_dst_masked(match, value->be16, mask->be16);
73f33563
BP
2180 break;
2181
dc235f7f
JR
2182 case MFF_TCP_FLAGS:
2183 match_set_tcp_flags_masked(match, value->be16, mask->be16);
2184 break;
2185
6a885fd0
BP
2186 case MFF_N_IDS:
2187 default:
428b2edd 2188 OVS_NOT_REACHED();
6a885fd0 2189 }
db0b6c29
JR
2190
2191 return mf->usable_protocols_bitwise;
2192
2193cidr_check:
2194 return ip_is_cidr(mask->be32) ? mf->usable_protocols :
2195 mf->usable_protocols_bitwise;
6a885fd0
BP
2196}
2197
816fd533
BP
2198static enum ofperr
2199mf_check__(const struct mf_subfield *sf, const struct flow *flow,
2200 const char *type)
6a885fd0 2201{
816fd533
BP
2202 if (!sf->field) {
2203 VLOG_WARN_RL(&rl, "unknown %s field", type);
9e404d1e 2204 return OFPERR_OFPBAC_BAD_SET_TYPE;
816fd533
BP
2205 } else if (!sf->n_bits) {
2206 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
9e404d1e 2207 return OFPERR_OFPBAC_BAD_SET_LEN;
816fd533
BP
2208 } else if (sf->ofs >= sf->field->n_bits) {
2209 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
2210 sf->ofs, sf->field->n_bits, type, sf->field->name);
9e404d1e 2211 return OFPERR_OFPBAC_BAD_SET_LEN;
816fd533
BP
2212 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
2213 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
2214 "of %s field %s", sf->ofs, sf->n_bits,
2215 sf->field->n_bits, type, sf->field->name);
9e404d1e 2216 return OFPERR_OFPBAC_BAD_SET_LEN;
816fd533
BP
2217 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
2218 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
2219 type, sf->field->name);
9e404d1e 2220 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
6a885fd0 2221 } else {
816fd533
BP
2222 return 0;
2223 }
816fd533 2224}
6a885fd0 2225
816fd533
BP
2226/* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
2227 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
2228 * ofp_mkerr()). */
2229enum ofperr
2230mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
2231{
2232 return mf_check__(sf, flow, "source");
2233}
6a885fd0 2234
816fd533
BP
2235/* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
2236 * if so, otherwise an OpenFlow error code (e.g. as returned by
2237 * ofp_mkerr()). */
2238enum ofperr
2239mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
2240{
2241 int error = mf_check__(sf, flow, "destination");
2242 if (!error && !sf->field->writable) {
2243 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2244 sf->field->name);
9e404d1e 2245 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
6a885fd0 2246 }
816fd533 2247 return error;
6a885fd0
BP
2248}
2249
81a76618 2250/* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
6a885fd0
BP
2251 * 'value' and 'mask', respectively. */
2252void
81a76618 2253mf_get(const struct mf_field *mf, const struct match *match,
6a885fd0
BP
2254 union mf_value *value, union mf_value *mask)
2255{
81a76618
BP
2256 mf_get_value(mf, &match->flow, value);
2257 mf_get_mask(mf, &match->wc, mask);
6a885fd0
BP
2258}
2259
6a885fd0
BP
2260static char *
2261mf_from_integer_string(const struct mf_field *mf, const char *s,
2262 uint8_t *valuep, uint8_t *maskp)
2263{
2264 unsigned long long int integer, mask;
2265 char *tail;
2266 int i;
2267
2268 errno = 0;
2269 integer = strtoull(s, &tail, 0);
2270 if (errno || (*tail != '\0' && *tail != '/')) {
2271 goto syntax_error;
2272 }
2273
2274 if (*tail == '/') {
2275 mask = strtoull(tail + 1, &tail, 0);
2276 if (errno || *tail != '\0') {
2277 goto syntax_error;
2278 }
2279 } else {
2280 mask = ULLONG_MAX;
2281 }
2282
2283 for (i = mf->n_bytes - 1; i >= 0; i--) {
2284 valuep[i] = integer;
2285 maskp[i] = mask;
2286 integer >>= 8;
2287 mask >>= 8;
2288 }
2289 if (integer) {
2290 return xasprintf("%s: value too large for %u-byte field %s",
2291 s, mf->n_bytes, mf->name);
2292 }
2293 return NULL;
2294
2295syntax_error:
2296 return xasprintf("%s: bad syntax for %s", s, mf->name);
2297}
2298
2299static char *
2300mf_from_ethernet_string(const struct mf_field *mf, const char *s,
2301 uint8_t mac[ETH_ADDR_LEN],
2302 uint8_t mask[ETH_ADDR_LEN])
2303{
279fb135 2304 int n;
6a885fd0 2305
279fb135 2306 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
6a885fd0 2307
279fb135 2308 n = -1;
c2c28dfd 2309 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(mac), &n)
279fb135 2310 && n == strlen(s)) {
6a885fd0
BP
2311 memset(mask, 0xff, ETH_ADDR_LEN);
2312 return NULL;
279fb135 2313 }
6a885fd0 2314
279fb135 2315 n = -1;
c2c28dfd
BP
2316 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
2317 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask), &n)
279fb135
BP
2318 && n == strlen(s)) {
2319 return NULL;
6a885fd0 2320 }
279fb135
BP
2321
2322 return xasprintf("%s: invalid Ethernet address", s);
6a885fd0
BP
2323}
2324
2325static char *
2326mf_from_ipv4_string(const struct mf_field *mf, const char *s,
2327 ovs_be32 *ip, ovs_be32 *mask)
2328{
2329 int prefix;
2330
cb22974d 2331 ovs_assert(mf->n_bytes == sizeof *ip);
6a885fd0 2332
c2c28dfd
BP
2333 if (ovs_scan(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
2334 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask))) {
6a885fd0 2335 /* OK. */
c2c28dfd 2336 } else if (ovs_scan(s, IP_SCAN_FMT"/%d", IP_SCAN_ARGS(ip), &prefix)) {
6a885fd0
BP
2337 if (prefix <= 0 || prefix > 32) {
2338 return xasprintf("%s: network prefix bits not between 1 and "
2339 "32", s);
2340 } else if (prefix == 32) {
b8266395 2341 *mask = OVS_BE32_MAX;
6a885fd0
BP
2342 } else {
2343 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
2344 }
c2c28dfd 2345 } else if (ovs_scan(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip))) {
b8266395 2346 *mask = OVS_BE32_MAX;
6a885fd0
BP
2347 } else {
2348 return xasprintf("%s: invalid IP address", s);
2349 }
2350 return NULL;
2351}
2352
2353static char *
2354mf_from_ipv6_string(const struct mf_field *mf, const char *s,
2355 struct in6_addr *value, struct in6_addr *mask)
2356{
2357 char *str = xstrdup(s);
2358 char *save_ptr = NULL;
2359 const char *name, *netmask;
2360 int retval;
2361
cb22974d 2362 ovs_assert(mf->n_bytes == sizeof *value);
6a885fd0
BP
2363
2364 name = strtok_r(str, "/", &save_ptr);
2365 retval = name ? lookup_ipv6(name, value) : EINVAL;
2366 if (retval) {
2367 char *err;
2368
2369 err = xasprintf("%s: could not convert to IPv6 address", str);
2370 free(str);
2371
2372 return err;
2373 }
2374
2375 netmask = strtok_r(NULL, "/", &save_ptr);
2376 if (netmask) {
ff0b06ee
BP
2377 if (inet_pton(AF_INET6, netmask, mask) != 1) {
2378 int prefix = atoi(netmask);
2379 if (prefix <= 0 || prefix > 128) {
2380 free(str);
2381 return xasprintf("%s: prefix bits not between 1 and 128", s);
2382 } else {
2383 *mask = ipv6_create_mask(prefix);
2384 }
6a885fd0
BP
2385 }
2386 } else {
2387 *mask = in6addr_exact;
2388 }
2389 free(str);
2390
2391 return NULL;
2392}
2393
2394static char *
2395mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
2396 ovs_be16 *valuep, ovs_be16 *maskp)
2397{
4e022ec0 2398 ofp_port_t port;
6a885fd0 2399
cb22974d 2400 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
33ab38d9
JR
2401
2402 if (ofputil_port_from_string(s, &port)) {
4e022ec0 2403 *valuep = htons(ofp_to_u16(port));
b8266395 2404 *maskp = OVS_BE16_MAX;
6a885fd0 2405 return NULL;
6a885fd0 2406 }
33ab38d9 2407 return xasprintf("%s: port value out of range for %s", s, mf->name);
6a885fd0
BP
2408}
2409
72333065
JR
2410static char *
2411mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
2412 ovs_be32 *valuep, ovs_be32 *maskp)
2413{
4e022ec0 2414 ofp_port_t port;
72333065
JR
2415
2416 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
2417 if (ofputil_port_from_string(s, &port)) {
2418 *valuep = ofputil_port_to_ofp11(port);
b8266395 2419 *maskp = OVS_BE32_MAX;
72333065
JR
2420 return NULL;
2421 }
2422 return xasprintf("%s: port value out of range for %s", s, mf->name);
2423}
2424
7257b535
BP
2425struct frag_handling {
2426 const char *name;
2427 uint8_t mask;
2428 uint8_t value;
2429};
2430
2431static const struct frag_handling all_frags[] = {
eadef313
JP
2432#define A FLOW_NW_FRAG_ANY
2433#define L FLOW_NW_FRAG_LATER
7257b535
BP
2434 /* name mask value */
2435
2436 { "no", A|L, 0 },
2437 { "first", A|L, A },
2438 { "later", A|L, A|L },
2439
2440 { "no", A, 0 },
2441 { "yes", A, A },
2442
2443 { "not_later", L, 0 },
2444 { "later", L, L },
2445#undef A
2446#undef L
2447};
2448
2449static char *
2450mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2451{
2452 const struct frag_handling *h;
2453
2454 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2455 if (!strcasecmp(s, h->name)) {
2456 /* We force the upper bits of the mask on to make mf_parse_value()
2457 * happy (otherwise it will never think it's an exact match.) */
eadef313 2458 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
7257b535
BP
2459 *valuep = h->value;
2460 return NULL;
2461 }
2462 }
2463
2464 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2465 "\"yes\", \"first\", \"later\", \"not_first\"", s);
2466}
2467
4fe3445a
PS
2468static int
2469parse_flow_tun_flags(const char *s_, const char *(*bit_to_string)(uint32_t),
2470 ovs_be16 *res)
2471{
2472 uint32_t result = 0;
2473 char *save_ptr = NULL;
2474 char *name;
2475 int rc = 0;
2476 char *s = xstrdup(s_);
2477
2478 for (name = strtok_r((char *)s, " |", &save_ptr); name;
2479 name = strtok_r(NULL, " |", &save_ptr)) {
2480 int name_len;
2481 unsigned long long int flags;
2482 uint32_t bit;
4fe3445a 2483
c2c28dfd 2484 if (ovs_scan(name, "%lli", &flags)) {
4fe3445a
PS
2485 result |= flags;
2486 continue;
2487 }
2488 name_len = strlen(name);
2489 for (bit = 1; bit; bit <<= 1) {
2490 const char *fname = bit_to_string(bit);
2491 size_t len;
2492
2493 if (!fname) {
2494 continue;
2495 }
2496
2497 len = strlen(fname);
2498 if (len != name_len) {
2499 continue;
2500 }
2501 if (!strncmp(name, fname, len)) {
2502 result |= bit;
2503 break;
2504 }
2505 }
2506
2507 if (!bit) {
2508 rc = -ENOENT;
2509 goto out;
2510 }
2511 }
2512
2513 *res = htons(result);
2514out:
2515 free(s);
2516 return rc;
2517}
2518
2519static char *
b0aa8146 2520mf_from_tun_flags_string(const char *s, ovs_be16 *valuep, ovs_be16 *maskp)
4fe3445a
PS
2521{
2522 if (!parse_flow_tun_flags(s, flow_tun_flag_to_string, valuep)) {
b8266395 2523 *maskp = OVS_BE16_MAX;
4fe3445a
PS
2524 return NULL;
2525 }
2526
2527 return xasprintf("%s: unknown tunnel flags (valid flags are \"df\", "
c49a5e33 2528 "\"csum\", \"key\")", s);
4fe3445a
PS
2529}
2530
61bf6666
JR
2531static char *
2532mf_from_tcp_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
2533{
2534 uint16_t flags = 0;
2535 uint16_t mask = 0;
2536 uint16_t bit;
2537 int n;
2538
2539 if (ovs_scan(s, "%"SCNi16"/%"SCNi16"%n", &flags, &mask, &n) && !s[n]) {
2540 *flagsp = htons(flags);
2541 *maskp = htons(mask);
2542 return NULL;
2543 }
2544 if (ovs_scan(s, "%"SCNi16"%n", &flags, &n) && !s[n]) {
2545 *flagsp = htons(flags);
2546 *maskp = OVS_BE16_MAX;
2547 return NULL;
2548 }
2549
2550 while (*s != '\0') {
2551 bool set;
2552 int name_len;
2553
2554 switch (*s) {
2555 case '+':
2556 set = true;
2557 break;
2558 case '-':
2559 set = false;
2560 break;
2561 default:
2562 return xasprintf("%s: TCP flag must be preceded by '+' (for SET) "
2563 "or '-' (NOT SET)", s);
2564 }
2565 s++;
2566
2567 name_len = strcspn(s,"+-");
2568
2569 for (bit = 1; bit; bit <<= 1) {
2570 const char *fname = packet_tcp_flag_to_string(bit);
2571 size_t len;
2572
2573 if (!fname) {
2574 continue;
2575 }
2576
2577 len = strlen(fname);
2578 if (len != name_len) {
2579 continue;
2580 }
2581 if (!strncmp(s, fname, len)) {
2582 if (mask & bit) {
2583 return xasprintf("%s: Each TCP flag can be specified only "
2584 "once", s);
2585 }
2586 if (set) {
2587 flags |= bit;
2588 }
2589 mask |= bit;
2590 break;
2591 }
2592 }
2593
2594 if (!bit) {
2595 return xasprintf("%s: unknown TCP flag(s)", s);
2596 }
2597 s += name_len;
2598 }
2599
2600 *flagsp = htons(flags);
2601 *maskp = htons(mask);
2602 return NULL;
2603}
2604
2605
6a885fd0
BP
2606/* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2607 * NULL if successful, otherwise a malloc()'d string describing the error. */
2608char *
2609mf_parse(const struct mf_field *mf, const char *s,
2610 union mf_value *value, union mf_value *mask)
2611{
3634dff8
BP
2612 char *error;
2613
bedde04c 2614 if (!strcmp(s, "*")) {
6a885fd0
BP
2615 memset(value, 0, mf->n_bytes);
2616 memset(mask, 0, mf->n_bytes);
2617 return NULL;
2618 }
2619
2620 switch (mf->string) {
2621 case MFS_DECIMAL:
2622 case MFS_HEXADECIMAL:
3634dff8
BP
2623 error = mf_from_integer_string(mf, s,
2624 (uint8_t *) value, (uint8_t *) mask);
2625 break;
6a885fd0
BP
2626
2627 case MFS_ETHERNET:
3634dff8
BP
2628 error = mf_from_ethernet_string(mf, s, value->mac, mask->mac);
2629 break;
6a885fd0
BP
2630
2631 case MFS_IPV4:
3634dff8
BP
2632 error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2633 break;
6a885fd0
BP
2634
2635 case MFS_IPV6:
3634dff8
BP
2636 error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2637 break;
6a885fd0
BP
2638
2639 case MFS_OFP_PORT:
3634dff8
BP
2640 error = mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2641 break;
7257b535 2642
72333065 2643 case MFS_OFP_PORT_OXM:
3634dff8
BP
2644 error = mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
2645 break;
72333065 2646
7257b535 2647 case MFS_FRAG:
3634dff8
BP
2648 error = mf_from_frag_string(s, &value->u8, &mask->u8);
2649 break;
4fe3445a
PS
2650
2651 case MFS_TNL_FLAGS:
cb22974d 2652 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
3634dff8
BP
2653 error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
2654 break;
2655
61bf6666
JR
2656 case MFS_TCP_FLAGS:
2657 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2658 error = mf_from_tcp_flags_string(s, &value->be16, &mask->be16);
2659 break;
2660
3634dff8 2661 default:
428b2edd 2662 OVS_NOT_REACHED();
6a885fd0 2663 }
3634dff8
BP
2664
2665 if (!error && !mf_is_mask_valid(mf, mask)) {
2666 error = xasprintf("%s: invalid mask for field %s", s, mf->name);
2667 }
2668 return error;
6a885fd0
BP
2669}
2670
2671/* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2672 * successful, otherwise a malloc()'d string describing the error. */
2673char *
2674mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2675{
2676 union mf_value mask;
2677 char *error;
2678
2679 error = mf_parse(mf, s, value, &mask);
2680 if (error) {
2681 return error;
2682 }
2683
2684 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2685 return xasprintf("%s: wildcards not allowed here", s);
2686 }
2687 return NULL;
2688}
2689
2690static void
2691mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2692 const uint8_t *maskp, struct ds *s)
2693{
2694 unsigned long long int integer;
2695 int i;
2696
cb22974d 2697 ovs_assert(mf->n_bytes <= 8);
6a885fd0
BP
2698
2699 integer = 0;
2700 for (i = 0; i < mf->n_bytes; i++) {
2701 integer = (integer << 8) | valuep[i];
2702 }
2703 if (mf->string == MFS_HEXADECIMAL) {
2704 ds_put_format(s, "%#llx", integer);
2705 } else {
2706 ds_put_format(s, "%lld", integer);
2707 }
2708
2709 if (maskp) {
2710 unsigned long long int mask;
2711
2712 mask = 0;
2713 for (i = 0; i < mf->n_bytes; i++) {
2714 mask = (mask << 8) | maskp[i];
2715 }
2716
2717 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2718 * not sure that that a bit-mask written in decimal is ever easier to
2719 * understand than the same bit-mask written in hexadecimal. */
2720 ds_put_format(s, "/%#llx", mask);
2721 }
2722}
2723
7257b535 2724static void
8f75dea3 2725mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
7257b535
BP
2726{
2727 const struct frag_handling *h;
7257b535 2728
eadef313 2729 mask &= FLOW_NW_FRAG_MASK;
8f75dea3 2730 value &= mask;
7257b535
BP
2731
2732 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2733 if (value == h->value && mask == h->mask) {
2734 ds_put_cstr(s, h->name);
2735 return;
2736 }
2737 }
2738 ds_put_cstr(s, "<error>");
2739}
2740
4fe3445a
PS
2741static void
2742mf_format_tnl_flags_string(const ovs_be16 *valuep, struct ds *s)
2743{
2744 format_flags(s, flow_tun_flag_to_string, ntohs(*valuep), '|');
2745}
2746
61bf6666
JR
2747static void
2748mf_format_tcp_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2749{
2750 format_flags_masked(s, NULL, packet_tcp_flag_to_string, ntohs(value),
2751 TCP_FLAGS(mask));
2752}
2753
6a885fd0
BP
2754/* Appends to 's' a string representation of field 'mf' whose value is in
2755 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2756void
2757mf_format(const struct mf_field *mf,
2758 const union mf_value *value, const union mf_value *mask,
2759 struct ds *s)
2760{
2761 if (mask) {
2762 if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
2763 ds_put_cstr(s, "ANY");
2764 return;
2765 } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
2766 mask = NULL;
2767 }
2768 }
2769
2770 switch (mf->string) {
72333065
JR
2771 case MFS_OFP_PORT_OXM:
2772 if (!mask) {
4e022ec0 2773 ofp_port_t port;
bc146369 2774 ofputil_port_from_ofp11(value->be32, &port);
72333065
JR
2775 ofputil_format_port(port, s);
2776 break;
2777 }
2778 /* fall through */
6a885fd0
BP
2779 case MFS_OFP_PORT:
2780 if (!mask) {
4e022ec0 2781 ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
6a885fd0
BP
2782 break;
2783 }
2784 /* fall through */
2785 case MFS_DECIMAL:
2786 case MFS_HEXADECIMAL:
2787 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2788 break;
2789
2790 case MFS_ETHERNET:
3b4d8ad3 2791 eth_format_masked(value->mac, mask->mac, s);
6a885fd0
BP
2792 break;
2793
2794 case MFS_IPV4:
b8266395 2795 ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
6a885fd0
BP
2796 break;
2797
2798 case MFS_IPV6:
2799 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2800 break;
2801
7257b535 2802 case MFS_FRAG:
8f75dea3 2803 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
7257b535
BP
2804 break;
2805
4fe3445a
PS
2806 case MFS_TNL_FLAGS:
2807 mf_format_tnl_flags_string(&value->be16, s);
2808 break;
2809
61bf6666
JR
2810 case MFS_TCP_FLAGS:
2811 mf_format_tcp_flags_string(value->be16,
2812 mask ? mask->be16 : OVS_BE16_MAX, s);
2813 break;
2814
6a885fd0 2815 default:
428b2edd 2816 OVS_NOT_REACHED();
6a885fd0
BP
2817 }
2818}
816fd533 2819\f
9bab681f
IY
2820/* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2821 * least-significant bits in 'x'.
2822 */
2823void
2824mf_write_subfield_flow(const struct mf_subfield *sf,
2825 const union mf_subvalue *x, struct flow *flow)
2826{
2827 const struct mf_field *field = sf->field;
2828 union mf_value value;
2829
2830 mf_get_value(field, flow, &value);
158edc8d 2831 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
9bab681f
IY
2832 sf->ofs, sf->n_bits);
2833 mf_set_flow_value(field, &value, flow);
2834}
2835
81a76618 2836/* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
1b35df45 2837 * least-significant bits in 'x'.
615660a9 2838 */
1b35df45
BP
2839void
2840mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
81a76618 2841 struct match *match)
1b35df45
BP
2842{
2843 const struct mf_field *field = sf->field;
2844 union mf_value value, mask;
2845
81a76618 2846 mf_get(field, match, &value, &mask);
1b35df45
BP
2847 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2848 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
81a76618 2849 mf_set(field, &value, &mask, match);
1b35df45
BP
2850}
2851
1b35df45
BP
2852/* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2853 * reading 'flow', e.g. as checked by mf_check_src(). */
2854void
2855mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2856 union mf_subvalue *x)
2857{
2858 union mf_value value;
2859
2860 mf_get_value(sf->field, flow, &value);
2861
2862 memset(x, 0, sizeof *x);
2863 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2864 x, sizeof *x, 0,
2865 sf->n_bits);
2866}
2867
816fd533
BP
2868/* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2869 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2870 * less. */
2871uint64_t
2872mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2873{
2874 union mf_value value;
2875
2876 mf_get_value(sf->field, flow, &value);
2877 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2878}
2879
2880/* Formats 'sf' into 's' in a format normally acceptable to
2881 * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if
2882 * sf->field has no NXM name.) */
2883void
2884mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
2885{
2886 if (!sf->field) {
2887 ds_put_cstr(s, "<unknown>");
2888 } else if (sf->field->nxm_name) {
2889 ds_put_cstr(s, sf->field->nxm_name);
2890 } else if (sf->field->nxm_header) {
2891 uint32_t header = sf->field->nxm_header;
2892 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
2893 } else {
2894 ds_put_cstr(s, sf->field->name);
2895 }
2896
33500edd 2897 if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
816fd533
BP
2898 ds_put_cstr(s, "[]");
2899 } else if (sf->n_bits == 1) {
2900 ds_put_format(s, "[%d]", sf->ofs);
2901 } else {
2902 ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
2903 }
2904}
2905
2906static const struct mf_field *
2907mf_parse_subfield_name(const char *name, int name_len, bool *wild)
2908{
2909 int i;
2910
2911 *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
2912 if (*wild) {
2913 name_len -= 2;
2914 }
2915
2916 for (i = 0; i < MFF_N_IDS; i++) {
2917 const struct mf_field *mf = mf_from_id(i);
2918
2919 if (mf->nxm_name
2920 && !strncmp(mf->nxm_name, name, name_len)
2921 && mf->nxm_name[name_len] == '\0') {
2922 return mf;
2923 }
b5e5143b
BP
2924 if (mf->oxm_name
2925 && !strncmp(mf->oxm_name, name, name_len)
2926 && mf->oxm_name[name_len] == '\0') {
2927 return mf;
2928 }
816fd533
BP
2929 }
2930
2931 return NULL;
2932}
2933
2934/* Parses a subfield from the beginning of '*sp' into 'sf'. If successful,
2935 * returns NULL and advances '*sp' to the first byte following the parsed
2936 * string. On failure, returns a malloc()'d error message, does not modify
2937 * '*sp', and does not properly initialize 'sf'.
2938 *
2939 * The syntax parsed from '*sp' takes the form "header[start..end]" where
2940 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2941 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2942 * may both be omitted (the [] are still required) to indicate an entire
2943 * field. */
bdda5aca 2944char * WARN_UNUSED_RESULT
816fd533
BP
2945mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
2946{
2947 const struct mf_field *field;
2948 const char *name;
2949 int start, end;
2950 const char *s;
2951 int name_len;
2952 bool wild;
2953
2954 s = *sp;
2955 name = s;
2956 name_len = strcspn(s, "[");
2957 if (s[name_len] != '[') {
2958 return xasprintf("%s: missing [ looking for field name", *sp);
2959 }
2960
2961 field = mf_parse_subfield_name(name, name_len, &wild);
2962 if (!field) {
2963 return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
2964 }
2965
2966 s += name_len;
c2c28dfd 2967 if (ovs_scan(s, "[%d..%d]", &start, &end)) {
816fd533 2968 /* Nothing to do. */
c2c28dfd 2969 } else if (ovs_scan(s, "[%d]", &start)) {
816fd533
BP
2970 end = start;
2971 } else if (!strncmp(s, "[]", 2)) {
2972 start = 0;
2973 end = field->n_bits - 1;
2974 } else {
2975 return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2976 "[<start>..<end>]", *sp);
2977 }
2978 s = strchr(s, ']') + 1;
2979
2980 if (start > end) {
2981 return xasprintf("%s: starting bit %d is after ending bit %d",
2982 *sp, start, end);
2983 } else if (start >= field->n_bits) {
2984 return xasprintf("%s: starting bit %d is not valid because field is "
2985 "only %d bits wide", *sp, start, field->n_bits);
2986 } else if (end >= field->n_bits){
2987 return xasprintf("%s: ending bit %d is not valid because field is "
2988 "only %d bits wide", *sp, end, field->n_bits);
2989 }
2990
2991 sf->field = field;
2992 sf->ofs = start;
2993 sf->n_bits = end - start + 1;
2994
2995 *sp = s;
2996 return NULL;
2997}
2998
bdda5aca
BP
2999/* Parses a subfield from the entirety of 's' into 'sf'. Returns NULL if
3000 * successful, otherwise a malloc()'d string describing the error. The caller
3001 * is responsible for freeing the returned string.
816fd533
BP
3002 *
3003 * The syntax parsed from 's' takes the form "header[start..end]" where
3004 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
3005 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
3006 * may both be omitted (the [] are still required) to indicate an entire
3007 * field. */
bdda5aca 3008char * WARN_UNUSED_RESULT
816fd533
BP
3009mf_parse_subfield(struct mf_subfield *sf, const char *s)
3010{
bdda5aca
BP
3011 char *error = mf_parse_subfield__(sf, &s);
3012 if (!error && s[0]) {
3013 error = xstrdup("unexpected input following field syntax");
816fd533 3014 }
bdda5aca 3015 return error;
816fd533 3016}
9bab681f
IY
3017
3018void
3019mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
3020{
3021 int i;
3022
3023 for (i = 0; i < ARRAY_SIZE(subvalue->u8); i++) {
3024 if (subvalue->u8[i]) {
3025 ds_put_format(s, "0x%"PRIx8, subvalue->u8[i]);
3026 for (i++; i < ARRAY_SIZE(subvalue->u8); i++) {
3027 ds_put_format(s, "%02"PRIx8, subvalue->u8[i]);
3028 }
3029 return;
3030 }
3031 }
3032 ds_put_char(s, '0');
3033}