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