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