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