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