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