2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <arpa/inet.h>
23 #include <netinet/in.h>
24 #include <netinet/icmp6.h>
28 #include "byte-order.h"
31 #include "dynamic-string.h"
38 #include "unaligned.h"
40 #include "openvswitch/vlog.h"
42 VLOG_DEFINE_THIS_MODULE(odp_util
);
44 /* The interface between userspace and kernel uses an "OVS_*" prefix.
45 * Since this is fairly non-specific for the OVS userspace components,
46 * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
47 * interactions with the datapath.
50 /* The set of characters that may separate one action or one key attribute
52 static const char *delimiters
= ", \t\r\n";
54 static const char *hex_chars
= "0123456789abcdefABCDEF";
58 const struct attr_len_tbl
*next
;
61 #define ATTR_LEN_INVALID -1
62 #define ATTR_LEN_VARIABLE -2
63 #define ATTR_LEN_NESTED -3
65 static int parse_odp_key_mask_attr(const char *, const struct simap
*port_names
,
66 struct ofpbuf
*, struct ofpbuf
*);
67 static void format_odp_key_attr(const struct nlattr
*a
,
68 const struct nlattr
*ma
,
69 const struct hmap
*portno_names
, struct ds
*ds
,
72 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
75 * - For an action whose argument has a fixed length, returned that
76 * nonnegative length in bytes.
78 * - For an action with a variable-length argument, returns ATTR_LEN_VARIABLE.
80 * - For an invalid 'type', returns ATTR_LEN_INVALID. */
82 odp_action_len(uint16_t type
)
84 if (type
> OVS_ACTION_ATTR_MAX
) {
88 switch ((enum ovs_action_attr
) type
) {
89 case OVS_ACTION_ATTR_OUTPUT
: return sizeof(uint32_t);
90 case OVS_ACTION_ATTR_TUNNEL_PUSH
: return ATTR_LEN_VARIABLE
;
91 case OVS_ACTION_ATTR_TUNNEL_POP
: return sizeof(uint32_t);
92 case OVS_ACTION_ATTR_USERSPACE
: return ATTR_LEN_VARIABLE
;
93 case OVS_ACTION_ATTR_PUSH_VLAN
: return sizeof(struct ovs_action_push_vlan
);
94 case OVS_ACTION_ATTR_POP_VLAN
: return 0;
95 case OVS_ACTION_ATTR_PUSH_MPLS
: return sizeof(struct ovs_action_push_mpls
);
96 case OVS_ACTION_ATTR_POP_MPLS
: return sizeof(ovs_be16
);
97 case OVS_ACTION_ATTR_RECIRC
: return sizeof(uint32_t);
98 case OVS_ACTION_ATTR_HASH
: return sizeof(struct ovs_action_hash
);
99 case OVS_ACTION_ATTR_SET
: return ATTR_LEN_VARIABLE
;
100 case OVS_ACTION_ATTR_SET_MASKED
: return ATTR_LEN_VARIABLE
;
101 case OVS_ACTION_ATTR_SAMPLE
: return ATTR_LEN_VARIABLE
;
103 case OVS_ACTION_ATTR_UNSPEC
:
104 case __OVS_ACTION_ATTR_MAX
:
105 return ATTR_LEN_INVALID
;
108 return ATTR_LEN_INVALID
;
111 /* Returns a string form of 'attr'. The return value is either a statically
112 * allocated constant string or the 'bufsize'-byte buffer 'namebuf'. 'bufsize'
113 * should be at least OVS_KEY_ATTR_BUFSIZE. */
114 enum { OVS_KEY_ATTR_BUFSIZE
= 3 + INT_STRLEN(unsigned int) + 1 };
116 ovs_key_attr_to_string(enum ovs_key_attr attr
, char *namebuf
, size_t bufsize
)
119 case OVS_KEY_ATTR_UNSPEC
: return "unspec";
120 case OVS_KEY_ATTR_ENCAP
: return "encap";
121 case OVS_KEY_ATTR_PRIORITY
: return "skb_priority";
122 case OVS_KEY_ATTR_SKB_MARK
: return "skb_mark";
123 case OVS_KEY_ATTR_TUNNEL
: return "tunnel";
124 case OVS_KEY_ATTR_IN_PORT
: return "in_port";
125 case OVS_KEY_ATTR_ETHERNET
: return "eth";
126 case OVS_KEY_ATTR_VLAN
: return "vlan";
127 case OVS_KEY_ATTR_ETHERTYPE
: return "eth_type";
128 case OVS_KEY_ATTR_IPV4
: return "ipv4";
129 case OVS_KEY_ATTR_IPV6
: return "ipv6";
130 case OVS_KEY_ATTR_TCP
: return "tcp";
131 case OVS_KEY_ATTR_TCP_FLAGS
: return "tcp_flags";
132 case OVS_KEY_ATTR_UDP
: return "udp";
133 case OVS_KEY_ATTR_SCTP
: return "sctp";
134 case OVS_KEY_ATTR_ICMP
: return "icmp";
135 case OVS_KEY_ATTR_ICMPV6
: return "icmpv6";
136 case OVS_KEY_ATTR_ARP
: return "arp";
137 case OVS_KEY_ATTR_ND
: return "nd";
138 case OVS_KEY_ATTR_MPLS
: return "mpls";
139 case OVS_KEY_ATTR_DP_HASH
: return "dp_hash";
140 case OVS_KEY_ATTR_RECIRC_ID
: return "recirc_id";
142 case __OVS_KEY_ATTR_MAX
:
144 snprintf(namebuf
, bufsize
, "key%u", (unsigned int) attr
);
150 format_generic_odp_action(struct ds
*ds
, const struct nlattr
*a
)
152 size_t len
= nl_attr_get_size(a
);
154 ds_put_format(ds
, "action%"PRId16
, nl_attr_type(a
));
156 const uint8_t *unspec
;
159 unspec
= nl_attr_get(a
);
160 for (i
= 0; i
< len
; i
++) {
161 ds_put_char(ds
, i
? ' ': '(');
162 ds_put_format(ds
, "%02x", unspec
[i
]);
164 ds_put_char(ds
, ')');
169 format_odp_sample_action(struct ds
*ds
, const struct nlattr
*attr
)
171 static const struct nl_policy ovs_sample_policy
[] = {
172 [OVS_SAMPLE_ATTR_PROBABILITY
] = { .type
= NL_A_U32
},
173 [OVS_SAMPLE_ATTR_ACTIONS
] = { .type
= NL_A_NESTED
}
175 struct nlattr
*a
[ARRAY_SIZE(ovs_sample_policy
)];
177 const struct nlattr
*nla_acts
;
180 ds_put_cstr(ds
, "sample");
182 if (!nl_parse_nested(attr
, ovs_sample_policy
, a
, ARRAY_SIZE(a
))) {
183 ds_put_cstr(ds
, "(error)");
187 percentage
= (100.0 * nl_attr_get_u32(a
[OVS_SAMPLE_ATTR_PROBABILITY
])) /
190 ds_put_format(ds
, "(sample=%.1f%%,", percentage
);
192 ds_put_cstr(ds
, "actions(");
193 nla_acts
= nl_attr_get(a
[OVS_SAMPLE_ATTR_ACTIONS
]);
194 len
= nl_attr_get_size(a
[OVS_SAMPLE_ATTR_ACTIONS
]);
195 format_odp_actions(ds
, nla_acts
, len
);
196 ds_put_format(ds
, "))");
200 slow_path_reason_to_string(uint32_t reason
)
202 switch ((enum slow_path_reason
) reason
) {
203 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING;
212 slow_path_reason_to_explanation(enum slow_path_reason reason
)
215 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION;
224 parse_flags(const char *s
, const char *(*bit_to_string
)(uint32_t),
225 uint32_t *res_flags
, uint32_t allowed
, uint32_t *res_mask
)
230 /* Parse masked flags in numeric format? */
231 if (res_mask
&& ovs_scan(s
, "%"SCNi32
"/%"SCNi32
"%n",
232 res_flags
, res_mask
, &n
) && n
> 0) {
233 if (*res_flags
& ~allowed
|| *res_mask
& ~allowed
) {
241 if (res_mask
&& (*s
== '+' || *s
== '-')) {
242 uint32_t flags
= 0, mask
= 0;
244 /* Parse masked flags. */
245 while (s
[0] != ')') {
252 } else if (s
[0] == '-') {
260 name_len
= strcspn(s
, "+-)");
262 for (bit
= 1; bit
; bit
<<= 1) {
263 const char *fname
= bit_to_string(bit
);
271 if (len
!= name_len
) {
274 if (!strncmp(s
, fname
, len
)) {
276 /* bit already set. */
279 if (!(bit
& allowed
)) {
291 return -EINVAL
; /* Unknown flag name */
302 /* Parse unmasked flags. If a flag is present, it is set, otherwise
304 while (s
[n
] != ')') {
305 unsigned long long int flags
;
309 if (ovs_scan(&s
[n
], "%lli%n", &flags
, &n0
)) {
310 if (flags
& ~allowed
) {
313 n
+= n0
+ (s
[n
+ n0
] == ',');
318 for (bit
= 1; bit
; bit
<<= 1) {
319 const char *name
= bit_to_string(bit
);
327 if (!strncmp(s
+ n
, name
, len
) &&
328 (s
[n
+ len
] == ',' || s
[n
+ len
] == ')')) {
329 if (!(bit
& allowed
)) {
333 n
+= len
+ (s
[n
+ len
] == ',');
345 *res_mask
= UINT32_MAX
;
351 format_odp_userspace_action(struct ds
*ds
, const struct nlattr
*attr
)
353 static const struct nl_policy ovs_userspace_policy
[] = {
354 [OVS_USERSPACE_ATTR_PID
] = { .type
= NL_A_U32
},
355 [OVS_USERSPACE_ATTR_USERDATA
] = { .type
= NL_A_UNSPEC
,
357 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
] = { .type
= NL_A_U32
,
360 struct nlattr
*a
[ARRAY_SIZE(ovs_userspace_policy
)];
361 const struct nlattr
*userdata_attr
;
362 const struct nlattr
*tunnel_out_port_attr
;
364 if (!nl_parse_nested(attr
, ovs_userspace_policy
, a
, ARRAY_SIZE(a
))) {
365 ds_put_cstr(ds
, "userspace(error)");
369 ds_put_format(ds
, "userspace(pid=%"PRIu32
,
370 nl_attr_get_u32(a
[OVS_USERSPACE_ATTR_PID
]));
372 userdata_attr
= a
[OVS_USERSPACE_ATTR_USERDATA
];
375 const uint8_t *userdata
= nl_attr_get(userdata_attr
);
376 size_t userdata_len
= nl_attr_get_size(userdata_attr
);
377 bool userdata_unspec
= true;
378 union user_action_cookie cookie
;
380 if (userdata_len
>= sizeof cookie
.type
381 && userdata_len
<= sizeof cookie
) {
383 memset(&cookie
, 0, sizeof cookie
);
384 memcpy(&cookie
, userdata
, userdata_len
);
386 userdata_unspec
= false;
388 if (userdata_len
== sizeof cookie
.sflow
389 && cookie
.type
== USER_ACTION_COOKIE_SFLOW
) {
390 ds_put_format(ds
, ",sFlow("
391 "vid=%"PRIu16
",pcp=%"PRIu8
",output=%"PRIu32
")",
392 vlan_tci_to_vid(cookie
.sflow
.vlan_tci
),
393 vlan_tci_to_pcp(cookie
.sflow
.vlan_tci
),
394 cookie
.sflow
.output
);
395 } else if (userdata_len
== sizeof cookie
.slow_path
396 && cookie
.type
== USER_ACTION_COOKIE_SLOW_PATH
) {
397 ds_put_cstr(ds
, ",slow_path(");
398 format_flags(ds
, slow_path_reason_to_string
,
399 cookie
.slow_path
.reason
, ',');
400 ds_put_format(ds
, ")");
401 } else if (userdata_len
== sizeof cookie
.flow_sample
402 && cookie
.type
== USER_ACTION_COOKIE_FLOW_SAMPLE
) {
403 ds_put_format(ds
, ",flow_sample(probability=%"PRIu16
404 ",collector_set_id=%"PRIu32
405 ",obs_domain_id=%"PRIu32
406 ",obs_point_id=%"PRIu32
")",
407 cookie
.flow_sample
.probability
,
408 cookie
.flow_sample
.collector_set_id
,
409 cookie
.flow_sample
.obs_domain_id
,
410 cookie
.flow_sample
.obs_point_id
);
411 } else if (userdata_len
>= sizeof cookie
.ipfix
412 && cookie
.type
== USER_ACTION_COOKIE_IPFIX
) {
413 ds_put_format(ds
, ",ipfix(output_port=%"PRIu32
")",
414 cookie
.ipfix
.output_odp_port
);
416 userdata_unspec
= true;
420 if (userdata_unspec
) {
422 ds_put_format(ds
, ",userdata(");
423 for (i
= 0; i
< userdata_len
; i
++) {
424 ds_put_format(ds
, "%02x", userdata
[i
]);
426 ds_put_char(ds
, ')');
430 tunnel_out_port_attr
= a
[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
];
431 if (tunnel_out_port_attr
) {
432 ds_put_format(ds
, ",tunnel_out_port=%"PRIu32
,
433 nl_attr_get_u32(tunnel_out_port_attr
));
436 ds_put_char(ds
, ')');
440 format_vlan_tci(struct ds
*ds
, ovs_be16 tci
, ovs_be16 mask
, bool verbose
)
442 if (verbose
|| vlan_tci_to_vid(tci
) || vlan_tci_to_vid(mask
)) {
443 ds_put_format(ds
, "vid=%"PRIu16
, vlan_tci_to_vid(tci
));
444 if (vlan_tci_to_vid(mask
) != VLAN_VID_MASK
) { /* Partially masked. */
445 ds_put_format(ds
, "/0x%"PRIx16
, vlan_tci_to_vid(mask
));
447 ds_put_char(ds
, ',');
449 if (verbose
|| vlan_tci_to_pcp(tci
) || vlan_tci_to_pcp(mask
)) {
450 ds_put_format(ds
, "pcp=%d", vlan_tci_to_pcp(tci
));
451 if (vlan_tci_to_pcp(mask
) != (VLAN_PCP_MASK
>> VLAN_PCP_SHIFT
)) {
452 ds_put_format(ds
, "/0x%x", vlan_tci_to_pcp(mask
));
454 ds_put_char(ds
, ',');
456 if (!(tci
& htons(VLAN_CFI
))) {
457 ds_put_cstr(ds
, "cfi=0");
458 ds_put_char(ds
, ',');
464 format_mpls_lse(struct ds
*ds
, ovs_be32 mpls_lse
)
466 ds_put_format(ds
, "label=%"PRIu32
",tc=%d,ttl=%d,bos=%d",
467 mpls_lse_to_label(mpls_lse
),
468 mpls_lse_to_tc(mpls_lse
),
469 mpls_lse_to_ttl(mpls_lse
),
470 mpls_lse_to_bos(mpls_lse
));
474 format_mpls(struct ds
*ds
, const struct ovs_key_mpls
*mpls_key
,
475 const struct ovs_key_mpls
*mpls_mask
, int n
)
478 ovs_be32 key
= mpls_key
->mpls_lse
;
480 if (mpls_mask
== NULL
) {
481 format_mpls_lse(ds
, key
);
483 ovs_be32 mask
= mpls_mask
->mpls_lse
;
485 ds_put_format(ds
, "label=%"PRIu32
"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
486 mpls_lse_to_label(key
), mpls_lse_to_label(mask
),
487 mpls_lse_to_tc(key
), mpls_lse_to_tc(mask
),
488 mpls_lse_to_ttl(key
), mpls_lse_to_ttl(mask
),
489 mpls_lse_to_bos(key
), mpls_lse_to_bos(mask
));
494 for (i
= 0; i
< n
; i
++) {
495 ds_put_format(ds
, "lse%d=%#"PRIx32
,
496 i
, ntohl(mpls_key
[i
].mpls_lse
));
498 ds_put_format(ds
, "/%#"PRIx32
, ntohl(mpls_mask
[i
].mpls_lse
));
500 ds_put_char(ds
, ',');
507 format_odp_recirc_action(struct ds
*ds
, uint32_t recirc_id
)
509 ds_put_format(ds
, "recirc(%#"PRIx32
")", recirc_id
);
513 format_odp_hash_action(struct ds
*ds
, const struct ovs_action_hash
*hash_act
)
515 ds_put_format(ds
, "hash(");
517 if (hash_act
->hash_alg
== OVS_HASH_ALG_L4
) {
518 ds_put_format(ds
, "hash_l4(%"PRIu32
")", hash_act
->hash_basis
);
520 ds_put_format(ds
, "Unknown hash algorithm(%"PRIu32
")",
523 ds_put_format(ds
, ")");
527 format_udp_tnl_push_header(struct ds
*ds
, const struct ip_header
*ip
)
529 const struct udp_header
*udp
;
531 udp
= (const struct udp_header
*) (ip
+ 1);
532 ds_put_format(ds
, "udp(src=%"PRIu16
",dst=%"PRIu16
",csum=0x%"PRIx16
"),",
533 ntohs(udp
->udp_src
), ntohs(udp
->udp_dst
),
534 ntohs(udp
->udp_csum
));
540 format_odp_tnl_push_header(struct ds
*ds
, struct ovs_action_push_tnl
*data
)
542 const struct eth_header
*eth
;
543 const struct ip_header
*ip
;
546 eth
= (const struct eth_header
*)data
->header
;
549 ip
= (const struct ip_header
*)l3
;
552 ds_put_format(ds
, "header(size=%"PRIu8
",type=%"PRIu8
",eth(dst=",
553 data
->header_len
, data
->tnl_type
);
554 ds_put_format(ds
, ETH_ADDR_FMT
, ETH_ADDR_ARGS(eth
->eth_dst
));
555 ds_put_format(ds
, ",src=");
556 ds_put_format(ds
, ETH_ADDR_FMT
, ETH_ADDR_ARGS(eth
->eth_src
));
557 ds_put_format(ds
, ",dl_type=0x%04"PRIx16
"),", ntohs(eth
->eth_type
));
560 ds_put_format(ds
, "ipv4(src="IP_FMT
",dst="IP_FMT
",proto=%"PRIu8
561 ",tos=%#"PRIx8
",ttl=%"PRIu8
",frag=0x%"PRIx16
"),",
562 IP_ARGS(get_16aligned_be32(&ip
->ip_src
)),
563 IP_ARGS(get_16aligned_be32(&ip
->ip_dst
)),
564 ip
->ip_proto
, ip
->ip_tos
,
568 if (data
->tnl_type
== OVS_VPORT_TYPE_VXLAN
) {
569 const struct vxlanhdr
*vxh
;
571 vxh
= format_udp_tnl_push_header(ds
, ip
);
573 ds_put_format(ds
, "vxlan(flags=0x%"PRIx32
",vni=0x%"PRIx32
")",
574 ntohl(get_16aligned_be32(&vxh
->vx_flags
)),
575 ntohl(get_16aligned_be32(&vxh
->vx_vni
)) >> 8);
576 } else if (data
->tnl_type
== OVS_VPORT_TYPE_GENEVE
) {
577 const struct genevehdr
*gnh
;
579 gnh
= format_udp_tnl_push_header(ds
, ip
);
581 ds_put_format(ds
, "geneve(%svni=0x%"PRIx32
")",
582 gnh
->oam
? "oam," : "",
583 ntohl(get_16aligned_be32(&gnh
->vni
)) >> 8);
584 } else if (data
->tnl_type
== OVS_VPORT_TYPE_GRE
) {
585 const struct gre_base_hdr
*greh
;
586 ovs_16aligned_be32
*options
;
589 l4
= ((uint8_t *)l3
+ sizeof(struct ip_header
));
590 greh
= (const struct gre_base_hdr
*) l4
;
592 ds_put_format(ds
, "gre((flags=0x%"PRIx16
",proto=0x%"PRIx16
")",
593 ntohs(greh
->flags
), ntohs(greh
->protocol
));
594 options
= (ovs_16aligned_be32
*)(greh
+ 1);
595 if (greh
->flags
& htons(GRE_CSUM
)) {
596 ds_put_format(ds
, ",csum=0x%"PRIx16
, ntohs(*((ovs_be16
*)options
)));
599 if (greh
->flags
& htons(GRE_KEY
)) {
600 ds_put_format(ds
, ",key=0x%"PRIx32
, ntohl(get_16aligned_be32(options
)));
603 if (greh
->flags
& htons(GRE_SEQ
)) {
604 ds_put_format(ds
, ",seq=0x%"PRIx32
, ntohl(get_16aligned_be32(options
)));
607 ds_put_format(ds
, ")");
609 ds_put_format(ds
, ")");
613 format_odp_tnl_push_action(struct ds
*ds
, const struct nlattr
*attr
)
615 struct ovs_action_push_tnl
*data
;
617 data
= (struct ovs_action_push_tnl
*) nl_attr_get(attr
);
619 ds_put_format(ds
, "tnl_push(tnl_port(%"PRIu32
"),", data
->tnl_port
);
620 format_odp_tnl_push_header(ds
, data
);
621 ds_put_format(ds
, ",out_port(%"PRIu32
"))", data
->out_port
);
625 format_odp_action(struct ds
*ds
, const struct nlattr
*a
)
628 enum ovs_action_attr type
= nl_attr_type(a
);
629 const struct ovs_action_push_vlan
*vlan
;
632 expected_len
= odp_action_len(nl_attr_type(a
));
633 if (expected_len
!= ATTR_LEN_VARIABLE
&&
634 nl_attr_get_size(a
) != expected_len
) {
635 ds_put_format(ds
, "bad length %"PRIuSIZE
", expected %d for: ",
636 nl_attr_get_size(a
), expected_len
);
637 format_generic_odp_action(ds
, a
);
642 case OVS_ACTION_ATTR_OUTPUT
:
643 ds_put_format(ds
, "%"PRIu32
, nl_attr_get_u32(a
));
645 case OVS_ACTION_ATTR_TUNNEL_POP
:
646 ds_put_format(ds
, "tnl_pop(%"PRIu32
")", nl_attr_get_u32(a
));
648 case OVS_ACTION_ATTR_TUNNEL_PUSH
:
649 format_odp_tnl_push_action(ds
, a
);
651 case OVS_ACTION_ATTR_USERSPACE
:
652 format_odp_userspace_action(ds
, a
);
654 case OVS_ACTION_ATTR_RECIRC
:
655 format_odp_recirc_action(ds
, nl_attr_get_u32(a
));
657 case OVS_ACTION_ATTR_HASH
:
658 format_odp_hash_action(ds
, nl_attr_get(a
));
660 case OVS_ACTION_ATTR_SET_MASKED
:
662 size
= nl_attr_get_size(a
) / 2;
663 ds_put_cstr(ds
, "set(");
665 /* Masked set action not supported for tunnel key, which is bigger. */
666 if (size
<= sizeof(struct ovs_key_ipv6
)) {
667 struct nlattr attr
[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6
),
668 sizeof(struct nlattr
))];
669 struct nlattr mask
[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6
),
670 sizeof(struct nlattr
))];
672 mask
->nla_type
= attr
->nla_type
= nl_attr_type(a
);
673 mask
->nla_len
= attr
->nla_len
= NLA_HDRLEN
+ size
;
674 memcpy(attr
+ 1, (char *)(a
+ 1), size
);
675 memcpy(mask
+ 1, (char *)(a
+ 1) + size
, size
);
676 format_odp_key_attr(attr
, mask
, NULL
, ds
, false);
678 format_odp_key_attr(a
, NULL
, NULL
, ds
, false);
680 ds_put_cstr(ds
, ")");
682 case OVS_ACTION_ATTR_SET
:
683 ds_put_cstr(ds
, "set(");
684 format_odp_key_attr(nl_attr_get(a
), NULL
, NULL
, ds
, true);
685 ds_put_cstr(ds
, ")");
687 case OVS_ACTION_ATTR_PUSH_VLAN
:
688 vlan
= nl_attr_get(a
);
689 ds_put_cstr(ds
, "push_vlan(");
690 if (vlan
->vlan_tpid
!= htons(ETH_TYPE_VLAN
)) {
691 ds_put_format(ds
, "tpid=0x%04"PRIx16
",", ntohs(vlan
->vlan_tpid
));
693 format_vlan_tci(ds
, vlan
->vlan_tci
, OVS_BE16_MAX
, false);
694 ds_put_char(ds
, ')');
696 case OVS_ACTION_ATTR_POP_VLAN
:
697 ds_put_cstr(ds
, "pop_vlan");
699 case OVS_ACTION_ATTR_PUSH_MPLS
: {
700 const struct ovs_action_push_mpls
*mpls
= nl_attr_get(a
);
701 ds_put_cstr(ds
, "push_mpls(");
702 format_mpls_lse(ds
, mpls
->mpls_lse
);
703 ds_put_format(ds
, ",eth_type=0x%"PRIx16
")", ntohs(mpls
->mpls_ethertype
));
706 case OVS_ACTION_ATTR_POP_MPLS
: {
707 ovs_be16 ethertype
= nl_attr_get_be16(a
);
708 ds_put_format(ds
, "pop_mpls(eth_type=0x%"PRIx16
")", ntohs(ethertype
));
711 case OVS_ACTION_ATTR_SAMPLE
:
712 format_odp_sample_action(ds
, a
);
714 case OVS_ACTION_ATTR_UNSPEC
:
715 case __OVS_ACTION_ATTR_MAX
:
717 format_generic_odp_action(ds
, a
);
723 format_odp_actions(struct ds
*ds
, const struct nlattr
*actions
,
727 const struct nlattr
*a
;
730 NL_ATTR_FOR_EACH (a
, left
, actions
, actions_len
) {
732 ds_put_char(ds
, ',');
734 format_odp_action(ds
, a
);
739 if (left
== actions_len
) {
740 ds_put_cstr(ds
, "<empty>");
742 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
743 for (i
= 0; i
< left
; i
++) {
744 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
746 ds_put_char(ds
, ')');
749 ds_put_cstr(ds
, "drop");
753 /* Separate out parse_odp_userspace_action() function. */
755 parse_odp_userspace_action(const char *s
, struct ofpbuf
*actions
)
758 union user_action_cookie cookie
;
760 odp_port_t tunnel_out_port
;
762 void *user_data
= NULL
;
763 size_t user_data_size
= 0;
765 if (!ovs_scan(s
, "userspace(pid=%"SCNi32
"%n", &pid
, &n
)) {
771 uint32_t probability
;
772 uint32_t collector_set_id
;
773 uint32_t obs_domain_id
;
774 uint32_t obs_point_id
;
777 if (ovs_scan(&s
[n
], ",sFlow(vid=%i,"
778 "pcp=%i,output=%"SCNi32
")%n",
779 &vid
, &pcp
, &output
, &n1
)) {
783 tci
= vid
| (pcp
<< VLAN_PCP_SHIFT
);
788 cookie
.type
= USER_ACTION_COOKIE_SFLOW
;
789 cookie
.sflow
.vlan_tci
= htons(tci
);
790 cookie
.sflow
.output
= output
;
792 user_data_size
= sizeof cookie
.sflow
;
793 } else if (ovs_scan(&s
[n
], ",slow_path(%n",
798 cookie
.type
= USER_ACTION_COOKIE_SLOW_PATH
;
799 cookie
.slow_path
.unused
= 0;
800 cookie
.slow_path
.reason
= 0;
802 res
= parse_flags(&s
[n
], slow_path_reason_to_string
,
803 &cookie
.slow_path
.reason
,
804 SLOW_PATH_REASON_MASK
, NULL
);
805 if (res
< 0 || s
[n
+ res
] != ')') {
811 user_data_size
= sizeof cookie
.slow_path
;
812 } else if (ovs_scan(&s
[n
], ",flow_sample(probability=%"SCNi32
","
813 "collector_set_id=%"SCNi32
","
814 "obs_domain_id=%"SCNi32
","
815 "obs_point_id=%"SCNi32
")%n",
816 &probability
, &collector_set_id
,
817 &obs_domain_id
, &obs_point_id
, &n1
)) {
820 cookie
.type
= USER_ACTION_COOKIE_FLOW_SAMPLE
;
821 cookie
.flow_sample
.probability
= probability
;
822 cookie
.flow_sample
.collector_set_id
= collector_set_id
;
823 cookie
.flow_sample
.obs_domain_id
= obs_domain_id
;
824 cookie
.flow_sample
.obs_point_id
= obs_point_id
;
826 user_data_size
= sizeof cookie
.flow_sample
;
827 } else if (ovs_scan(&s
[n
], ",ipfix(output_port=%"SCNi32
")%n",
830 cookie
.type
= USER_ACTION_COOKIE_IPFIX
;
831 cookie
.ipfix
.output_odp_port
= u32_to_odp(output
);
833 user_data_size
= sizeof cookie
.ipfix
;
834 } else if (ovs_scan(&s
[n
], ",userdata(%n",
839 ofpbuf_init(&buf
, 16);
840 end
= ofpbuf_put_hex(&buf
, &s
[n
], NULL
);
844 user_data
= buf
.data
;
845 user_data_size
= buf
.size
;
852 if (ovs_scan(&s
[n
], ",tunnel_out_port=%"SCNi32
")%n",
853 &tunnel_out_port
, &n1
)) {
854 odp_put_userspace_action(pid
, user_data
, user_data_size
, tunnel_out_port
, actions
);
856 } else if (s
[n
] == ')') {
857 odp_put_userspace_action(pid
, user_data
, user_data_size
, ODPP_NONE
, actions
);
866 ovs_parse_tnl_push(const char *s
, struct ovs_action_push_tnl
*data
)
868 struct eth_header
*eth
;
869 struct ip_header
*ip
;
870 struct udp_header
*udp
;
871 struct gre_base_hdr
*greh
;
872 uint16_t gre_proto
, gre_flags
, dl_type
, udp_src
, udp_dst
, csum
;
874 uint32_t tnl_type
= 0, header_len
= 0;
878 if (!ovs_scan_len(s
, &n
, "tnl_push(tnl_port(%"SCNi32
"),", &data
->tnl_port
)) {
881 eth
= (struct eth_header
*) data
->header
;
882 l3
= (data
->header
+ sizeof *eth
);
883 l4
= ((uint8_t *) l3
+ sizeof (struct ip_header
));
884 ip
= (struct ip_header
*) l3
;
885 if (!ovs_scan_len(s
, &n
, "header(size=%"SCNi32
",type=%"SCNi32
","
886 "eth(dst="ETH_ADDR_SCAN_FMT
",",
889 ETH_ADDR_SCAN_ARGS(eth
->eth_dst
))) {
893 if (!ovs_scan_len(s
, &n
, "src="ETH_ADDR_SCAN_FMT
",",
894 ETH_ADDR_SCAN_ARGS(eth
->eth_src
))) {
897 if (!ovs_scan_len(s
, &n
, "dl_type=0x%"SCNx16
"),", &dl_type
)) {
900 eth
->eth_type
= htons(dl_type
);
903 if (!ovs_scan_len(s
, &n
, "ipv4(src="IP_SCAN_FMT
",dst="IP_SCAN_FMT
",proto=%"SCNi8
904 ",tos=%"SCNi8
",ttl=%"SCNi8
",frag=0x%"SCNx16
"),",
907 &ip
->ip_proto
, &ip
->ip_tos
,
908 &ip
->ip_ttl
, &ip
->ip_frag_off
)) {
911 put_16aligned_be32(&ip
->ip_src
, sip
);
912 put_16aligned_be32(&ip
->ip_dst
, dip
);
915 udp
= (struct udp_header
*) l4
;
916 greh
= (struct gre_base_hdr
*) l4
;
917 if (ovs_scan_len(s
, &n
, "udp(src=%"SCNi16
",dst=%"SCNi16
",csum=0x%"SCNx16
"),",
918 &udp_src
, &udp_dst
, &csum
)) {
919 uint32_t vx_flags
, vni
;
921 udp
->udp_src
= htons(udp_src
);
922 udp
->udp_dst
= htons(udp_dst
);
924 udp
->udp_csum
= htons(csum
);
926 if (ovs_scan_len(s
, &n
, "vxlan(flags=0x%"SCNx32
",vni=0x%"SCNx32
"))",
928 struct vxlanhdr
*vxh
= (struct vxlanhdr
*) (udp
+ 1);
930 put_16aligned_be32(&vxh
->vx_flags
, htonl(vx_flags
));
931 put_16aligned_be32(&vxh
->vx_vni
, htonl(vni
<< 8));
932 tnl_type
= OVS_VPORT_TYPE_VXLAN
;
933 header_len
= sizeof *eth
+ sizeof *ip
+
934 sizeof *udp
+ sizeof *vxh
;
935 } else if (ovs_scan_len(s
, &n
, "geneve(")) {
936 struct genevehdr
*gnh
= (struct genevehdr
*) (udp
+ 1);
938 memset(gnh
, 0, sizeof *gnh
);
939 if (ovs_scan_len(s
, &n
, "oam,")) {
942 if (!ovs_scan_len(s
, &n
, "vni=0x%"SCNx32
"))", &vni
)) {
945 gnh
->proto_type
= htons(ETH_TYPE_TEB
);
946 put_16aligned_be32(&gnh
->vni
, htonl(vni
<< 8));
947 tnl_type
= OVS_VPORT_TYPE_GENEVE
;
948 header_len
= sizeof *eth
+ sizeof *ip
+
949 sizeof *udp
+ sizeof *gnh
;
953 } else if (ovs_scan_len(s
, &n
, "gre((flags=0x%"SCNx16
",proto=0x%"SCNx16
")",
954 &gre_flags
, &gre_proto
)){
956 tnl_type
= OVS_VPORT_TYPE_GRE
;
957 greh
->flags
= htons(gre_flags
);
958 greh
->protocol
= htons(gre_proto
);
959 ovs_16aligned_be32
*options
= (ovs_16aligned_be32
*) (greh
+ 1);
961 if (greh
->flags
& htons(GRE_CSUM
)) {
962 if (!ovs_scan_len(s
, &n
, ",csum=0x%"SCNx16
, &csum
)) {
966 memset(options
, 0, sizeof *options
);
967 *((ovs_be16
*)options
) = htons(csum
);
970 if (greh
->flags
& htons(GRE_KEY
)) {
973 if (!ovs_scan_len(s
, &n
, ",key=0x%"SCNx32
, &key
)) {
977 put_16aligned_be32(options
, htonl(key
));
980 if (greh
->flags
& htons(GRE_SEQ
)) {
983 if (!ovs_scan_len(s
, &n
, ",seq=0x%"SCNx32
, &seq
)) {
986 put_16aligned_be32(options
, htonl(seq
));
990 if (!ovs_scan_len(s
, &n
, "))")) {
994 header_len
= sizeof *eth
+ sizeof *ip
+
995 ((uint8_t *) options
- (uint8_t *) greh
);
1000 /* check tunnel meta data. */
1001 if (data
->tnl_type
!= tnl_type
) {
1004 if (data
->header_len
!= header_len
) {
1009 if (!ovs_scan_len(s
, &n
, ",out_port(%"SCNi32
"))", &data
->out_port
)) {
1017 parse_odp_action(const char *s
, const struct simap
*port_names
,
1018 struct ofpbuf
*actions
)
1024 if (ovs_scan(s
, "%"SCNi32
"%n", &port
, &n
)) {
1025 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, port
);
1031 int len
= strcspn(s
, delimiters
);
1032 struct simap_node
*node
;
1034 node
= simap_find_len(port_names
, s
, len
);
1036 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, node
->data
);
1045 if (ovs_scan(s
, "recirc(%"PRIu32
")%n", &recirc_id
, &n
)) {
1046 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_RECIRC
, recirc_id
);
1051 if (!strncmp(s
, "userspace(", 10)) {
1052 return parse_odp_userspace_action(s
, actions
);
1055 if (!strncmp(s
, "set(", 4)) {
1058 struct nlattr mask
[128 / sizeof(struct nlattr
)];
1059 struct ofpbuf maskbuf
;
1060 struct nlattr
*nested
, *key
;
1063 /* 'mask' is big enough to hold any key. */
1064 ofpbuf_use_stack(&maskbuf
, mask
, sizeof mask
);
1066 start_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SET
);
1067 retval
= parse_odp_key_mask_attr(s
+ 4, port_names
, actions
, &maskbuf
);
1071 if (s
[retval
+ 4] != ')') {
1075 nested
= ofpbuf_at_assert(actions
, start_ofs
, sizeof *nested
);
1078 size
= nl_attr_get_size(mask
);
1079 if (size
== nl_attr_get_size(key
)) {
1080 /* Change to masked set action if not fully masked. */
1081 if (!is_all_ones(mask
+ 1, size
)) {
1082 key
->nla_len
+= size
;
1083 ofpbuf_put(actions
, mask
+ 1, size
);
1084 /* 'actions' may have been reallocated by ofpbuf_put(). */
1085 nested
= ofpbuf_at_assert(actions
, start_ofs
, sizeof *nested
);
1086 nested
->nla_type
= OVS_ACTION_ATTR_SET_MASKED
;
1090 nl_msg_end_nested(actions
, start_ofs
);
1095 struct ovs_action_push_vlan push
;
1096 int tpid
= ETH_TYPE_VLAN
;
1101 if (ovs_scan(s
, "push_vlan(vid=%i,pcp=%i)%n", &vid
, &pcp
, &n
)
1102 || ovs_scan(s
, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
1103 &vid
, &pcp
, &cfi
, &n
)
1104 || ovs_scan(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
1105 &tpid
, &vid
, &pcp
, &n
)
1106 || ovs_scan(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
1107 &tpid
, &vid
, &pcp
, &cfi
, &n
)) {
1108 push
.vlan_tpid
= htons(tpid
);
1109 push
.vlan_tci
= htons((vid
<< VLAN_VID_SHIFT
)
1110 | (pcp
<< VLAN_PCP_SHIFT
)
1111 | (cfi
? VLAN_CFI
: 0));
1112 nl_msg_put_unspec(actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
1113 &push
, sizeof push
);
1119 if (!strncmp(s
, "pop_vlan", 8)) {
1120 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_VLAN
);
1128 if (ovs_scan(s
, "sample(sample=%lf%%,actions(%n", &percentage
, &n
)
1129 && percentage
>= 0. && percentage
<= 100.0) {
1130 size_t sample_ofs
, actions_ofs
;
1133 probability
= floor(UINT32_MAX
* (percentage
/ 100.0) + .5);
1134 sample_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SAMPLE
);
1135 nl_msg_put_u32(actions
, OVS_SAMPLE_ATTR_PROBABILITY
,
1136 (probability
<= 0 ? 0
1137 : probability
>= UINT32_MAX
? UINT32_MAX
1140 actions_ofs
= nl_msg_start_nested(actions
,
1141 OVS_SAMPLE_ATTR_ACTIONS
);
1145 n
+= strspn(s
+ n
, delimiters
);
1150 retval
= parse_odp_action(s
+ n
, port_names
, actions
);
1156 nl_msg_end_nested(actions
, actions_ofs
);
1157 nl_msg_end_nested(actions
, sample_ofs
);
1159 return s
[n
+ 1] == ')' ? n
+ 2 : -EINVAL
;
1167 if (ovs_scan(s
, "tnl_pop(%"SCNi32
")%n", &port
, &n
)) {
1168 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_TUNNEL_POP
, port
);
1174 struct ovs_action_push_tnl data
;
1177 n
= ovs_parse_tnl_push(s
, &data
);
1179 odp_put_tnl_push_action(actions
, &data
);
1188 /* Parses the string representation of datapath actions, in the format output
1189 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
1190 * value. On success, the ODP actions are appended to 'actions' as a series of
1191 * Netlink attributes. On failure, no data is appended to 'actions'. Either
1192 * way, 'actions''s data might be reallocated. */
1194 odp_actions_from_string(const char *s
, const struct simap
*port_names
,
1195 struct ofpbuf
*actions
)
1199 if (!strcasecmp(s
, "drop")) {
1203 old_size
= actions
->size
;
1207 s
+= strspn(s
, delimiters
);
1212 retval
= parse_odp_action(s
, port_names
, actions
);
1213 if (retval
< 0 || !strchr(delimiters
, s
[retval
])) {
1214 actions
->size
= old_size
;
1223 static const struct attr_len_tbl ovs_vxlan_ext_attr_lens
[OVS_VXLAN_EXT_MAX
+ 1] = {
1224 [OVS_VXLAN_EXT_GBP
] = { .len
= 4 },
1227 static const struct attr_len_tbl ovs_tun_key_attr_lens
[OVS_TUNNEL_KEY_ATTR_MAX
+ 1] = {
1228 [OVS_TUNNEL_KEY_ATTR_ID
] = { .len
= 8 },
1229 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC
] = { .len
= 4 },
1230 [OVS_TUNNEL_KEY_ATTR_IPV4_DST
] = { .len
= 4 },
1231 [OVS_TUNNEL_KEY_ATTR_TOS
] = { .len
= 1 },
1232 [OVS_TUNNEL_KEY_ATTR_TTL
] = { .len
= 1 },
1233 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
] = { .len
= 0 },
1234 [OVS_TUNNEL_KEY_ATTR_CSUM
] = { .len
= 0 },
1235 [OVS_TUNNEL_KEY_ATTR_TP_SRC
] = { .len
= 2 },
1236 [OVS_TUNNEL_KEY_ATTR_TP_DST
] = { .len
= 2 },
1237 [OVS_TUNNEL_KEY_ATTR_OAM
] = { .len
= 0 },
1238 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
] = { .len
= ATTR_LEN_VARIABLE
},
1239 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
] = { .len
= ATTR_LEN_NESTED
,
1240 .next
= ovs_vxlan_ext_attr_lens
,
1241 .next_max
= OVS_VXLAN_EXT_MAX
},
1244 static const struct attr_len_tbl ovs_flow_key_attr_lens
[OVS_KEY_ATTR_MAX
+ 1] = {
1245 [OVS_KEY_ATTR_ENCAP
] = { .len
= ATTR_LEN_NESTED
},
1246 [OVS_KEY_ATTR_PRIORITY
] = { .len
= 4 },
1247 [OVS_KEY_ATTR_SKB_MARK
] = { .len
= 4 },
1248 [OVS_KEY_ATTR_DP_HASH
] = { .len
= 4 },
1249 [OVS_KEY_ATTR_RECIRC_ID
] = { .len
= 4 },
1250 [OVS_KEY_ATTR_TUNNEL
] = { .len
= ATTR_LEN_NESTED
,
1251 .next
= ovs_tun_key_attr_lens
,
1252 .next_max
= OVS_TUNNEL_KEY_ATTR_MAX
},
1253 [OVS_KEY_ATTR_IN_PORT
] = { .len
= 4 },
1254 [OVS_KEY_ATTR_ETHERNET
] = { .len
= sizeof(struct ovs_key_ethernet
) },
1255 [OVS_KEY_ATTR_VLAN
] = { .len
= 2 },
1256 [OVS_KEY_ATTR_ETHERTYPE
] = { .len
= 2 },
1257 [OVS_KEY_ATTR_MPLS
] = { .len
= ATTR_LEN_VARIABLE
},
1258 [OVS_KEY_ATTR_IPV4
] = { .len
= sizeof(struct ovs_key_ipv4
) },
1259 [OVS_KEY_ATTR_IPV6
] = { .len
= sizeof(struct ovs_key_ipv6
) },
1260 [OVS_KEY_ATTR_TCP
] = { .len
= sizeof(struct ovs_key_tcp
) },
1261 [OVS_KEY_ATTR_TCP_FLAGS
] = { .len
= 2 },
1262 [OVS_KEY_ATTR_UDP
] = { .len
= sizeof(struct ovs_key_udp
) },
1263 [OVS_KEY_ATTR_SCTP
] = { .len
= sizeof(struct ovs_key_sctp
) },
1264 [OVS_KEY_ATTR_ICMP
] = { .len
= sizeof(struct ovs_key_icmp
) },
1265 [OVS_KEY_ATTR_ICMPV6
] = { .len
= sizeof(struct ovs_key_icmpv6
) },
1266 [OVS_KEY_ATTR_ARP
] = { .len
= sizeof(struct ovs_key_arp
) },
1267 [OVS_KEY_ATTR_ND
] = { .len
= sizeof(struct ovs_key_nd
) },
1270 /* Returns the correct length of the payload for a flow key attribute of the
1271 * specified 'type', ATTR_LEN_INVALID if 'type' is unknown, ATTR_LEN_VARIABLE
1272 * if the attribute's payload is variable length, or ATTR_LEN_NESTED if the
1273 * payload is a nested type. */
1275 odp_key_attr_len(const struct attr_len_tbl tbl
[], int max_len
, uint16_t type
)
1277 if (type
> max_len
) {
1278 return ATTR_LEN_INVALID
;
1281 return tbl
[type
].len
;
1285 format_generic_odp_key(const struct nlattr
*a
, struct ds
*ds
)
1287 size_t len
= nl_attr_get_size(a
);
1289 const uint8_t *unspec
;
1292 unspec
= nl_attr_get(a
);
1293 for (i
= 0; i
< len
; i
++) {
1295 ds_put_char(ds
, ' ');
1297 ds_put_format(ds
, "%02x", unspec
[i
]);
1303 ovs_frag_type_to_string(enum ovs_frag_type type
)
1306 case OVS_FRAG_TYPE_NONE
:
1308 case OVS_FRAG_TYPE_FIRST
:
1310 case OVS_FRAG_TYPE_LATER
:
1312 case __OVS_FRAG_TYPE_MAX
:
1318 #define GENEVE_OPT(class, type) ((OVS_FORCE uint32_t)(class) << 8 | (type))
1320 parse_geneve_opts(const struct nlattr
*attr
)
1322 int opts_len
= nl_attr_get_size(attr
);
1323 const struct geneve_opt
*opt
= nl_attr_get(attr
);
1325 while (opts_len
> 0) {
1328 if (opts_len
< sizeof(*opt
)) {
1332 len
= sizeof(*opt
) + opt
->length
* 4;
1333 if (len
> opts_len
) {
1337 switch (GENEVE_OPT(opt
->opt_class
, opt
->type
)) {
1339 if (opt
->type
& GENEVE_CRIT_OPT_TYPE
) {
1344 opt
= opt
+ len
/ sizeof(*opt
);
1351 enum odp_key_fitness
1352 odp_tun_key_from_attr(const struct nlattr
*attr
, struct flow_tnl
*tun
)
1355 const struct nlattr
*a
;
1357 bool unknown
= false;
1359 NL_NESTED_FOR_EACH(a
, left
, attr
) {
1360 uint16_t type
= nl_attr_type(a
);
1361 size_t len
= nl_attr_get_size(a
);
1362 int expected_len
= odp_key_attr_len(ovs_tun_key_attr_lens
,
1363 OVS_TUNNEL_ATTR_MAX
, type
);
1365 if (len
!= expected_len
&& expected_len
>= 0) {
1366 return ODP_FIT_ERROR
;
1370 case OVS_TUNNEL_KEY_ATTR_ID
:
1371 tun
->tun_id
= nl_attr_get_be64(a
);
1372 tun
->flags
|= FLOW_TNL_F_KEY
;
1374 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
1375 tun
->ip_src
= nl_attr_get_be32(a
);
1377 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
1378 tun
->ip_dst
= nl_attr_get_be32(a
);
1380 case OVS_TUNNEL_KEY_ATTR_TOS
:
1381 tun
->ip_tos
= nl_attr_get_u8(a
);
1383 case OVS_TUNNEL_KEY_ATTR_TTL
:
1384 tun
->ip_ttl
= nl_attr_get_u8(a
);
1387 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
1388 tun
->flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
1390 case OVS_TUNNEL_KEY_ATTR_CSUM
:
1391 tun
->flags
|= FLOW_TNL_F_CSUM
;
1393 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
1394 tun
->tp_src
= nl_attr_get_be16(a
);
1396 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
1397 tun
->tp_dst
= nl_attr_get_be16(a
);
1399 case OVS_TUNNEL_KEY_ATTR_OAM
:
1400 tun
->flags
|= FLOW_TNL_F_OAM
;
1402 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
: {
1403 static const struct nl_policy vxlan_opts_policy
[] = {
1404 [OVS_VXLAN_EXT_GBP
] = { .type
= NL_A_U32
},
1406 struct nlattr
*ext
[ARRAY_SIZE(vxlan_opts_policy
)];
1408 if (!nl_parse_nested(a
, vxlan_opts_policy
, ext
, ARRAY_SIZE(ext
))) {
1409 return ODP_FIT_ERROR
;
1412 if (ext
[OVS_VXLAN_EXT_GBP
]) {
1413 uint32_t gbp
= nl_attr_get_u32(ext
[OVS_VXLAN_EXT_GBP
]);
1415 tun
->gbp_id
= htons(gbp
& 0xFFFF);
1416 tun
->gbp_flags
= (gbp
>> 16) & 0xFF;
1421 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
: {
1422 if (parse_geneve_opts(a
)) {
1423 return ODP_FIT_ERROR
;
1425 /* It is necessary to reproduce options exactly (including order)
1426 * so it's easiest to just echo them back. */
1431 /* Allow this to show up as unexpected, if there are unknown
1432 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
1439 return ODP_FIT_ERROR
;
1442 return ODP_FIT_TOO_MUCH
;
1444 return ODP_FIT_PERFECT
;
1448 tun_key_to_attr(struct ofpbuf
*a
, const struct flow_tnl
*tun_key
)
1452 tun_key_ofs
= nl_msg_start_nested(a
, OVS_KEY_ATTR_TUNNEL
);
1454 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
1455 if (tun_key
->tun_id
|| tun_key
->flags
& FLOW_TNL_F_KEY
) {
1456 nl_msg_put_be64(a
, OVS_TUNNEL_KEY_ATTR_ID
, tun_key
->tun_id
);
1458 if (tun_key
->ip_src
) {
1459 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
, tun_key
->ip_src
);
1461 if (tun_key
->ip_dst
) {
1462 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
, tun_key
->ip_dst
);
1464 if (tun_key
->ip_tos
) {
1465 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TOS
, tun_key
->ip_tos
);
1467 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TTL
, tun_key
->ip_ttl
);
1468 if (tun_key
->flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
1469 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
1471 if (tun_key
->flags
& FLOW_TNL_F_CSUM
) {
1472 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
1474 if (tun_key
->tp_src
) {
1475 nl_msg_put_be16(a
, OVS_TUNNEL_KEY_ATTR_TP_SRC
, tun_key
->tp_src
);
1477 if (tun_key
->tp_dst
) {
1478 nl_msg_put_be16(a
, OVS_TUNNEL_KEY_ATTR_TP_DST
, tun_key
->tp_dst
);
1480 if (tun_key
->flags
& FLOW_TNL_F_OAM
) {
1481 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_OAM
);
1483 if (tun_key
->gbp_flags
|| tun_key
->gbp_id
) {
1484 size_t vxlan_opts_ofs
;
1486 vxlan_opts_ofs
= nl_msg_start_nested(a
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
1487 nl_msg_put_u32(a
, OVS_VXLAN_EXT_GBP
,
1488 (tun_key
->gbp_flags
<< 16) | ntohs(tun_key
->gbp_id
));
1489 nl_msg_end_nested(a
, vxlan_opts_ofs
);
1492 nl_msg_end_nested(a
, tun_key_ofs
);
1496 odp_mask_attr_is_wildcard(const struct nlattr
*ma
)
1498 return is_all_zeros(nl_attr_get(ma
), nl_attr_get_size(ma
));
1502 odp_mask_is_exact(enum ovs_key_attr attr
, const void *mask
, size_t size
)
1504 if (attr
== OVS_KEY_ATTR_TCP_FLAGS
) {
1505 return TCP_FLAGS(*(ovs_be16
*)mask
) == TCP_FLAGS(OVS_BE16_MAX
);
1507 if (attr
== OVS_KEY_ATTR_IPV6
) {
1508 const struct ovs_key_ipv6
*ipv6_mask
= mask
;
1511 ((ipv6_mask
->ipv6_label
& htonl(IPV6_LABEL_MASK
))
1512 == htonl(IPV6_LABEL_MASK
))
1513 && ipv6_mask
->ipv6_proto
== UINT8_MAX
1514 && ipv6_mask
->ipv6_tclass
== UINT8_MAX
1515 && ipv6_mask
->ipv6_hlimit
== UINT8_MAX
1516 && ipv6_mask
->ipv6_frag
== UINT8_MAX
1517 && ipv6_mask_is_exact((const struct in6_addr
*)ipv6_mask
->ipv6_src
)
1518 && ipv6_mask_is_exact((const struct in6_addr
*)ipv6_mask
->ipv6_dst
);
1520 if (attr
== OVS_KEY_ATTR_TUNNEL
) {
1521 const struct flow_tnl
*tun_mask
= mask
;
1523 return tun_mask
->flags
== FLOW_TNL_F_MASK
1524 && tun_mask
->tun_id
== OVS_BE64_MAX
1525 && tun_mask
->ip_src
== OVS_BE32_MAX
1526 && tun_mask
->ip_dst
== OVS_BE32_MAX
1527 && tun_mask
->ip_tos
== UINT8_MAX
1528 && tun_mask
->ip_ttl
== UINT8_MAX
1529 && tun_mask
->tp_src
== OVS_BE16_MAX
1530 && tun_mask
->tp_dst
== OVS_BE16_MAX
1531 && tun_mask
->gbp_id
== OVS_BE16_MAX
1532 && tun_mask
->gbp_flags
== UINT8_MAX
;
1535 if (attr
== OVS_KEY_ATTR_ARP
) {
1536 /* ARP key has padding, ignore it. */
1537 BUILD_ASSERT_DECL(sizeof(struct ovs_key_arp
) == 24);
1538 BUILD_ASSERT_DECL(offsetof(struct ovs_key_arp
, arp_tha
) == 10 + 6);
1539 size
= offsetof(struct ovs_key_arp
, arp_tha
) + ETH_ADDR_LEN
;
1540 ovs_assert(((uint16_t *)mask
)[size
/2] == 0);
1543 return is_all_ones(mask
, size
);
1547 odp_mask_attr_is_exact(const struct nlattr
*ma
)
1549 struct flow_tnl tun_mask
;
1550 enum ovs_key_attr attr
= nl_attr_type(ma
);
1554 if (attr
== OVS_KEY_ATTR_TUNNEL
) {
1555 memset(&tun_mask
, 0, sizeof tun_mask
);
1556 odp_tun_key_from_attr(ma
, &tun_mask
);
1558 size
= sizeof tun_mask
;
1560 mask
= nl_attr_get(ma
);
1561 size
= nl_attr_get_size(ma
);
1564 return odp_mask_is_exact(attr
, mask
, size
);
1568 odp_portno_names_set(struct hmap
*portno_names
, odp_port_t port_no
,
1571 struct odp_portno_names
*odp_portno_names
;
1573 odp_portno_names
= xmalloc(sizeof *odp_portno_names
);
1574 odp_portno_names
->port_no
= port_no
;
1575 odp_portno_names
->name
= xstrdup(port_name
);
1576 hmap_insert(portno_names
, &odp_portno_names
->hmap_node
,
1577 hash_odp_port(port_no
));
1581 odp_portno_names_get(const struct hmap
*portno_names
, odp_port_t port_no
)
1583 struct odp_portno_names
*odp_portno_names
;
1585 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names
, hmap_node
,
1586 hash_odp_port(port_no
), portno_names
) {
1587 if (odp_portno_names
->port_no
== port_no
) {
1588 return odp_portno_names
->name
;
1595 odp_portno_names_destroy(struct hmap
*portno_names
)
1597 struct odp_portno_names
*odp_portno_names
, *odp_portno_names_next
;
1598 HMAP_FOR_EACH_SAFE (odp_portno_names
, odp_portno_names_next
,
1599 hmap_node
, portno_names
) {
1600 hmap_remove(portno_names
, &odp_portno_names
->hmap_node
);
1601 free(odp_portno_names
->name
);
1602 free(odp_portno_names
);
1606 /* Format helpers. */
1609 format_eth(struct ds
*ds
, const char *name
, const uint8_t key
[ETH_ADDR_LEN
],
1610 const uint8_t (*mask
)[ETH_ADDR_LEN
], bool verbose
)
1612 bool mask_empty
= mask
&& eth_addr_is_zero(*mask
);
1614 if (verbose
|| !mask_empty
) {
1615 bool mask_full
= !mask
|| eth_mask_is_exact(*mask
);
1618 ds_put_format(ds
, "%s="ETH_ADDR_FMT
",", name
, ETH_ADDR_ARGS(key
));
1620 ds_put_format(ds
, "%s=", name
);
1621 eth_format_masked(key
, *mask
, ds
);
1622 ds_put_char(ds
, ',');
1628 format_be64(struct ds
*ds
, const char *name
, ovs_be64 key
,
1629 const ovs_be64
*mask
, bool verbose
)
1631 bool mask_empty
= mask
&& !*mask
;
1633 if (verbose
|| !mask_empty
) {
1634 bool mask_full
= !mask
|| *mask
== OVS_BE64_MAX
;
1636 ds_put_format(ds
, "%s=0x%"PRIx64
, name
, ntohll(key
));
1637 if (!mask_full
) { /* Partially masked. */
1638 ds_put_format(ds
, "/%#"PRIx64
, ntohll(*mask
));
1640 ds_put_char(ds
, ',');
1645 format_ipv4(struct ds
*ds
, const char *name
, ovs_be32 key
,
1646 const ovs_be32
*mask
, bool verbose
)
1648 bool mask_empty
= mask
&& !*mask
;
1650 if (verbose
|| !mask_empty
) {
1651 bool mask_full
= !mask
|| *mask
== OVS_BE32_MAX
;
1653 ds_put_format(ds
, "%s="IP_FMT
, name
, IP_ARGS(key
));
1654 if (!mask_full
) { /* Partially masked. */
1655 ds_put_format(ds
, "/"IP_FMT
, IP_ARGS(*mask
));
1657 ds_put_char(ds
, ',');
1662 format_ipv6(struct ds
*ds
, const char *name
, const ovs_be32 key_
[4],
1663 const ovs_be32 (*mask_
)[4], bool verbose
)
1665 char buf
[INET6_ADDRSTRLEN
];
1666 const struct in6_addr
*key
= (const struct in6_addr
*)key_
;
1667 const struct in6_addr
*mask
= mask_
? (const struct in6_addr
*)*mask_
1669 bool mask_empty
= mask
&& ipv6_mask_is_any(mask
);
1671 if (verbose
|| !mask_empty
) {
1672 bool mask_full
= !mask
|| ipv6_mask_is_exact(mask
);
1674 inet_ntop(AF_INET6
, key
, buf
, sizeof buf
);
1675 ds_put_format(ds
, "%s=%s", name
, buf
);
1676 if (!mask_full
) { /* Partially masked. */
1677 inet_ntop(AF_INET6
, mask
, buf
, sizeof buf
);
1678 ds_put_format(ds
, "/%s", buf
);
1680 ds_put_char(ds
, ',');
1685 format_ipv6_label(struct ds
*ds
, const char *name
, ovs_be32 key
,
1686 const ovs_be32
*mask
, bool verbose
)
1688 bool mask_empty
= mask
&& !*mask
;
1690 if (verbose
|| !mask_empty
) {
1691 bool mask_full
= !mask
1692 || (*mask
& htonl(IPV6_LABEL_MASK
)) == htonl(IPV6_LABEL_MASK
);
1694 ds_put_format(ds
, "%s=%#"PRIx32
, name
, ntohl(key
));
1695 if (!mask_full
) { /* Partially masked. */
1696 ds_put_format(ds
, "/%#"PRIx32
, ntohl(*mask
));
1698 ds_put_char(ds
, ',');
1703 format_u8x(struct ds
*ds
, const char *name
, uint8_t key
,
1704 const uint8_t *mask
, bool verbose
)
1706 bool mask_empty
= mask
&& !*mask
;
1708 if (verbose
|| !mask_empty
) {
1709 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
1711 ds_put_format(ds
, "%s=%#"PRIx8
, name
, key
);
1712 if (!mask_full
) { /* Partially masked. */
1713 ds_put_format(ds
, "/%#"PRIx8
, *mask
);
1715 ds_put_char(ds
, ',');
1720 format_u8u(struct ds
*ds
, const char *name
, uint8_t key
,
1721 const uint8_t *mask
, bool verbose
)
1723 bool mask_empty
= mask
&& !*mask
;
1725 if (verbose
|| !mask_empty
) {
1726 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
1728 ds_put_format(ds
, "%s=%"PRIu8
, name
, key
);
1729 if (!mask_full
) { /* Partially masked. */
1730 ds_put_format(ds
, "/%#"PRIx8
, *mask
);
1732 ds_put_char(ds
, ',');
1737 format_be16(struct ds
*ds
, const char *name
, ovs_be16 key
,
1738 const ovs_be16
*mask
, bool verbose
)
1740 bool mask_empty
= mask
&& !*mask
;
1742 if (verbose
|| !mask_empty
) {
1743 bool mask_full
= !mask
|| *mask
== OVS_BE16_MAX
;
1745 ds_put_format(ds
, "%s=%"PRIu16
, name
, ntohs(key
));
1746 if (!mask_full
) { /* Partially masked. */
1747 ds_put_format(ds
, "/%#"PRIx16
, ntohs(*mask
));
1749 ds_put_char(ds
, ',');
1754 format_tun_flags(struct ds
*ds
, const char *name
, uint16_t key
,
1755 const uint16_t *mask
, bool verbose
)
1757 bool mask_empty
= mask
&& !*mask
;
1759 if (verbose
|| !mask_empty
) {
1760 bool mask_full
= !mask
|| (*mask
& FLOW_TNL_F_MASK
) == FLOW_TNL_F_MASK
;
1762 ds_put_cstr(ds
, name
);
1763 ds_put_char(ds
, '(');
1764 if (!mask_full
) { /* Partially masked. */
1765 format_flags_masked(ds
, NULL
, flow_tun_flag_to_string
, key
, *mask
);
1766 } else { /* Fully masked. */
1767 format_flags(ds
, flow_tun_flag_to_string
, key
, ',');
1769 ds_put_cstr(ds
, "),");
1774 format_frag(struct ds
*ds
, const char *name
, uint8_t key
,
1775 const uint8_t *mask
, bool verbose
)
1777 bool mask_empty
= mask
&& !*mask
;
1779 /* ODP frag is an enumeration field; partial masks are not meaningful. */
1780 if (verbose
|| !mask_empty
) {
1781 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
1783 if (!mask_full
) { /* Partially masked. */
1784 ds_put_format(ds
, "error: partial mask not supported for frag (%#"
1787 ds_put_format(ds
, "%s=%s,", name
, ovs_frag_type_to_string(key
));
1792 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
1795 format_odp_key_attr(const struct nlattr
*a
, const struct nlattr
*ma
,
1796 const struct hmap
*portno_names
, struct ds
*ds
,
1799 enum ovs_key_attr attr
= nl_attr_type(a
);
1800 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
1804 is_exact
= ma
? odp_mask_attr_is_exact(ma
) : true;
1806 ds_put_cstr(ds
, ovs_key_attr_to_string(attr
, namebuf
, sizeof namebuf
));
1809 expected_len
= odp_key_attr_len(ovs_flow_key_attr_lens
,
1810 OVS_KEY_ATTR_MAX
, nl_attr_type(a
));
1811 if (expected_len
!= ATTR_LEN_VARIABLE
&&
1812 expected_len
!= ATTR_LEN_NESTED
) {
1813 bool bad_key_len
= nl_attr_get_size(a
) != expected_len
;
1814 bool bad_mask_len
= ma
&& nl_attr_get_size(ma
) != expected_len
;
1816 if (bad_key_len
|| bad_mask_len
) {
1818 ds_put_format(ds
, "(bad key length %"PRIuSIZE
", expected %d)(",
1819 nl_attr_get_size(a
), expected_len
);
1821 format_generic_odp_key(a
, ds
);
1823 ds_put_char(ds
, '/');
1825 ds_put_format(ds
, "(bad mask length %"PRIuSIZE
", expected %d)(",
1826 nl_attr_get_size(ma
), expected_len
);
1828 format_generic_odp_key(ma
, ds
);
1830 ds_put_char(ds
, ')');
1836 ds_put_char(ds
, '(');
1838 case OVS_KEY_ATTR_ENCAP
:
1839 if (ma
&& nl_attr_get_size(ma
) && nl_attr_get_size(a
)) {
1840 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
),
1841 nl_attr_get(ma
), nl_attr_get_size(ma
), NULL
, ds
,
1843 } else if (nl_attr_get_size(a
)) {
1844 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
), NULL
, 0, NULL
,
1849 case OVS_KEY_ATTR_PRIORITY
:
1850 case OVS_KEY_ATTR_SKB_MARK
:
1851 case OVS_KEY_ATTR_DP_HASH
:
1852 case OVS_KEY_ATTR_RECIRC_ID
:
1853 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
1855 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
1859 case OVS_KEY_ATTR_TUNNEL
: {
1860 struct flow_tnl key
, mask_
;
1861 struct flow_tnl
*mask
= ma
? &mask_
: NULL
;
1864 memset(mask
, 0, sizeof *mask
);
1865 odp_tun_key_from_attr(ma
, mask
);
1867 memset(&key
, 0, sizeof key
);
1868 if (odp_tun_key_from_attr(a
, &key
) == ODP_FIT_ERROR
) {
1869 ds_put_format(ds
, "error");
1872 format_be64(ds
, "tun_id", key
.tun_id
, MASK(mask
, tun_id
), verbose
);
1873 format_ipv4(ds
, "src", key
.ip_src
, MASK(mask
, ip_src
), verbose
);
1874 format_ipv4(ds
, "dst", key
.ip_dst
, MASK(mask
, ip_dst
), verbose
);
1875 format_u8x(ds
, "tos", key
.ip_tos
, MASK(mask
, ip_tos
), verbose
);
1876 format_u8u(ds
, "ttl", key
.ip_ttl
, MASK(mask
, ip_ttl
), verbose
);
1877 format_be16(ds
, "tp_src", key
.tp_src
, MASK(mask
, tp_src
), verbose
);
1878 format_be16(ds
, "tp_dst", key
.tp_dst
, MASK(mask
, tp_dst
), verbose
);
1879 format_be16(ds
, "gbp_id", key
.gbp_id
, MASK(mask
, gbp_id
), verbose
);
1880 format_u8x(ds
, "gbp_flags", key
.gbp_flags
, MASK(mask
, gbp_flags
), verbose
);
1881 format_tun_flags(ds
, "flags", key
.flags
, MASK(mask
, flags
), verbose
);
1885 case OVS_KEY_ATTR_IN_PORT
:
1886 if (portno_names
&& verbose
&& is_exact
) {
1887 char *name
= odp_portno_names_get(portno_names
,
1888 u32_to_odp(nl_attr_get_u32(a
)));
1890 ds_put_format(ds
, "%s", name
);
1892 ds_put_format(ds
, "%"PRIu32
, nl_attr_get_u32(a
));
1895 ds_put_format(ds
, "%"PRIu32
, nl_attr_get_u32(a
));
1897 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
1902 case OVS_KEY_ATTR_ETHERNET
: {
1903 const struct ovs_key_ethernet
*mask
= ma
? nl_attr_get(ma
) : NULL
;
1904 const struct ovs_key_ethernet
*key
= nl_attr_get(a
);
1906 format_eth(ds
, "src", key
->eth_src
, MASK(mask
, eth_src
), verbose
);
1907 format_eth(ds
, "dst", key
->eth_dst
, MASK(mask
, eth_dst
), verbose
);
1911 case OVS_KEY_ATTR_VLAN
:
1912 format_vlan_tci(ds
, nl_attr_get_be16(a
),
1913 ma
? nl_attr_get_be16(ma
) : OVS_BE16_MAX
, verbose
);
1916 case OVS_KEY_ATTR_MPLS
: {
1917 const struct ovs_key_mpls
*mpls_key
= nl_attr_get(a
);
1918 const struct ovs_key_mpls
*mpls_mask
= NULL
;
1919 size_t size
= nl_attr_get_size(a
);
1921 if (!size
|| size
% sizeof *mpls_key
) {
1922 ds_put_format(ds
, "(bad key length %"PRIuSIZE
")", size
);
1926 mpls_mask
= nl_attr_get(ma
);
1927 if (size
!= nl_attr_get_size(ma
)) {
1928 ds_put_format(ds
, "(key length %"PRIuSIZE
" != "
1929 "mask length %"PRIuSIZE
")",
1930 size
, nl_attr_get_size(ma
));
1934 format_mpls(ds
, mpls_key
, mpls_mask
, size
/ sizeof *mpls_key
);
1937 case OVS_KEY_ATTR_ETHERTYPE
:
1938 ds_put_format(ds
, "0x%04"PRIx16
, ntohs(nl_attr_get_be16(a
)));
1940 ds_put_format(ds
, "/0x%04"PRIx16
, ntohs(nl_attr_get_be16(ma
)));
1944 case OVS_KEY_ATTR_IPV4
: {
1945 const struct ovs_key_ipv4
*key
= nl_attr_get(a
);
1946 const struct ovs_key_ipv4
*mask
= ma
? nl_attr_get(ma
) : NULL
;
1948 format_ipv4(ds
, "src", key
->ipv4_src
, MASK(mask
, ipv4_src
), verbose
);
1949 format_ipv4(ds
, "dst", key
->ipv4_dst
, MASK(mask
, ipv4_dst
), verbose
);
1950 format_u8u(ds
, "proto", key
->ipv4_proto
, MASK(mask
, ipv4_proto
),
1952 format_u8x(ds
, "tos", key
->ipv4_tos
, MASK(mask
, ipv4_tos
), verbose
);
1953 format_u8u(ds
, "ttl", key
->ipv4_ttl
, MASK(mask
, ipv4_ttl
), verbose
);
1954 format_frag(ds
, "frag", key
->ipv4_frag
, MASK(mask
, ipv4_frag
),
1959 case OVS_KEY_ATTR_IPV6
: {
1960 const struct ovs_key_ipv6
*key
= nl_attr_get(a
);
1961 const struct ovs_key_ipv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
1963 format_ipv6(ds
, "src", key
->ipv6_src
, MASK(mask
, ipv6_src
), verbose
);
1964 format_ipv6(ds
, "dst", key
->ipv6_dst
, MASK(mask
, ipv6_dst
), verbose
);
1965 format_ipv6_label(ds
, "label", key
->ipv6_label
, MASK(mask
, ipv6_label
),
1967 format_u8u(ds
, "proto", key
->ipv6_proto
, MASK(mask
, ipv6_proto
),
1969 format_u8x(ds
, "tclass", key
->ipv6_tclass
, MASK(mask
, ipv6_tclass
),
1971 format_u8u(ds
, "hlimit", key
->ipv6_hlimit
, MASK(mask
, ipv6_hlimit
),
1973 format_frag(ds
, "frag", key
->ipv6_frag
, MASK(mask
, ipv6_frag
),
1978 /* These have the same structure and format. */
1979 case OVS_KEY_ATTR_TCP
:
1980 case OVS_KEY_ATTR_UDP
:
1981 case OVS_KEY_ATTR_SCTP
: {
1982 const struct ovs_key_tcp
*key
= nl_attr_get(a
);
1983 const struct ovs_key_tcp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
1985 format_be16(ds
, "src", key
->tcp_src
, MASK(mask
, tcp_src
), verbose
);
1986 format_be16(ds
, "dst", key
->tcp_dst
, MASK(mask
, tcp_dst
), verbose
);
1990 case OVS_KEY_ATTR_TCP_FLAGS
:
1992 format_flags_masked(ds
, NULL
, packet_tcp_flag_to_string
,
1993 ntohs(nl_attr_get_be16(a
)),
1994 ntohs(nl_attr_get_be16(ma
)));
1996 format_flags(ds
, packet_tcp_flag_to_string
,
1997 ntohs(nl_attr_get_be16(a
)), ',');
2001 case OVS_KEY_ATTR_ICMP
: {
2002 const struct ovs_key_icmp
*key
= nl_attr_get(a
);
2003 const struct ovs_key_icmp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2005 format_u8u(ds
, "type", key
->icmp_type
, MASK(mask
, icmp_type
), verbose
);
2006 format_u8u(ds
, "code", key
->icmp_code
, MASK(mask
, icmp_code
), verbose
);
2010 case OVS_KEY_ATTR_ICMPV6
: {
2011 const struct ovs_key_icmpv6
*key
= nl_attr_get(a
);
2012 const struct ovs_key_icmpv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2014 format_u8u(ds
, "type", key
->icmpv6_type
, MASK(mask
, icmpv6_type
),
2016 format_u8u(ds
, "code", key
->icmpv6_code
, MASK(mask
, icmpv6_code
),
2021 case OVS_KEY_ATTR_ARP
: {
2022 const struct ovs_key_arp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2023 const struct ovs_key_arp
*key
= nl_attr_get(a
);
2025 format_ipv4(ds
, "sip", key
->arp_sip
, MASK(mask
, arp_sip
), verbose
);
2026 format_ipv4(ds
, "tip", key
->arp_tip
, MASK(mask
, arp_tip
), verbose
);
2027 format_be16(ds
, "op", key
->arp_op
, MASK(mask
, arp_op
), verbose
);
2028 format_eth(ds
, "sha", key
->arp_sha
, MASK(mask
, arp_sha
), verbose
);
2029 format_eth(ds
, "tha", key
->arp_tha
, MASK(mask
, arp_tha
), verbose
);
2033 case OVS_KEY_ATTR_ND
: {
2034 const struct ovs_key_nd
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2035 const struct ovs_key_nd
*key
= nl_attr_get(a
);
2037 format_ipv6(ds
, "target", key
->nd_target
, MASK(mask
, nd_target
),
2039 format_eth(ds
, "sll", key
->nd_sll
, MASK(mask
, nd_sll
), verbose
);
2040 format_eth(ds
, "tll", key
->nd_tll
, MASK(mask
, nd_tll
), verbose
);
2045 case OVS_KEY_ATTR_UNSPEC
:
2046 case __OVS_KEY_ATTR_MAX
:
2048 format_generic_odp_key(a
, ds
);
2050 ds_put_char(ds
, '/');
2051 format_generic_odp_key(ma
, ds
);
2055 ds_put_char(ds
, ')');
2058 static struct nlattr
*
2059 generate_all_wildcard_mask(const struct attr_len_tbl tbl
[], int max
,
2060 struct ofpbuf
*ofp
, const struct nlattr
*key
)
2062 const struct nlattr
*a
;
2064 int type
= nl_attr_type(key
);
2065 int size
= nl_attr_get_size(key
);
2067 if (odp_key_attr_len(tbl
, max
, type
) != ATTR_LEN_NESTED
) {
2068 nl_msg_put_unspec_zero(ofp
, type
, size
);
2072 if (tbl
[type
].next
) {
2073 tbl
= tbl
[type
].next
;
2074 max
= tbl
[type
].next_max
;
2077 nested_mask
= nl_msg_start_nested(ofp
, type
);
2078 NL_ATTR_FOR_EACH(a
, left
, key
, nl_attr_get_size(key
)) {
2079 generate_all_wildcard_mask(tbl
, max
, ofp
, nl_attr_get(a
));
2081 nl_msg_end_nested(ofp
, nested_mask
);
2088 odp_ufid_from_string(const char *s_
, ovs_u128
*ufid
)
2092 if (ovs_scan(s
, "ufid:")) {
2096 if (ovs_scan(s
, "0x")) {
2100 n
= strspn(s
, hex_chars
);
2105 if (!ovs_scan(s
, "%16"SCNx64
"%16"SCNx64
, &ufid
->u64
.hi
,
2110 s
+= strspn(s
, delimiters
);
2119 odp_format_ufid(const ovs_u128
*ufid
, struct ds
*ds
)
2121 ds_put_format(ds
, "ufid:%016"PRIx64
"%016"PRIx64
, ufid
->u64
.hi
,
2125 /* Appends to 'ds' a string representation of the 'key_len' bytes of
2126 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
2127 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
2128 * non-null and 'verbose' is true, translates odp port number to its name. */
2130 odp_flow_format(const struct nlattr
*key
, size_t key_len
,
2131 const struct nlattr
*mask
, size_t mask_len
,
2132 const struct hmap
*portno_names
, struct ds
*ds
, bool verbose
)
2135 const struct nlattr
*a
;
2137 bool has_ethtype_key
= false;
2138 const struct nlattr
*ma
= NULL
;
2140 bool first_field
= true;
2142 ofpbuf_init(&ofp
, 100);
2143 NL_ATTR_FOR_EACH (a
, left
, key
, key_len
) {
2144 bool is_nested_attr
;
2145 bool is_wildcard
= false;
2146 int attr_type
= nl_attr_type(a
);
2148 if (attr_type
== OVS_KEY_ATTR_ETHERTYPE
) {
2149 has_ethtype_key
= true;
2152 is_nested_attr
= odp_key_attr_len(ovs_flow_key_attr_lens
,
2153 OVS_KEY_ATTR_MAX
, attr_type
) ==
2156 if (mask
&& mask_len
) {
2157 ma
= nl_attr_find__(mask
, mask_len
, nl_attr_type(a
));
2158 is_wildcard
= ma
? odp_mask_attr_is_wildcard(ma
) : true;
2161 if (verbose
|| !is_wildcard
|| is_nested_attr
) {
2162 if (is_wildcard
&& !ma
) {
2163 ma
= generate_all_wildcard_mask(ovs_flow_key_attr_lens
,
2168 ds_put_char(ds
, ',');
2170 format_odp_key_attr(a
, ma
, portno_names
, ds
, verbose
);
2171 first_field
= false;
2175 ofpbuf_uninit(&ofp
);
2180 if (left
== key_len
) {
2181 ds_put_cstr(ds
, "<empty>");
2183 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
2184 for (i
= 0; i
< left
; i
++) {
2185 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
2187 ds_put_char(ds
, ')');
2189 if (!has_ethtype_key
) {
2190 ma
= nl_attr_find__(mask
, mask_len
, OVS_KEY_ATTR_ETHERTYPE
);
2192 ds_put_format(ds
, ",eth_type(0/0x%04"PRIx16
")",
2193 ntohs(nl_attr_get_be16(ma
)));
2197 ds_put_cstr(ds
, "<empty>");
2201 /* Appends to 'ds' a string representation of the 'key_len' bytes of
2202 * OVS_KEY_ATTR_* attributes in 'key'. */
2204 odp_flow_key_format(const struct nlattr
*key
,
2205 size_t key_len
, struct ds
*ds
)
2207 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, ds
, true);
2211 ovs_frag_type_from_string(const char *s
, enum ovs_frag_type
*type
)
2213 if (!strcasecmp(s
, "no")) {
2214 *type
= OVS_FRAG_TYPE_NONE
;
2215 } else if (!strcasecmp(s
, "first")) {
2216 *type
= OVS_FRAG_TYPE_FIRST
;
2217 } else if (!strcasecmp(s
, "later")) {
2218 *type
= OVS_FRAG_TYPE_LATER
;
2228 scan_eth(const char *s
, uint8_t (*key
)[ETH_ADDR_LEN
],
2229 uint8_t (*mask
)[ETH_ADDR_LEN
])
2233 if (ovs_scan(s
, ETH_ADDR_SCAN_FMT
"%n", ETH_ADDR_SCAN_ARGS(*key
), &n
)) {
2237 if (ovs_scan(s
+ len
, "/"ETH_ADDR_SCAN_FMT
"%n",
2238 ETH_ADDR_SCAN_ARGS(*mask
), &n
)) {
2241 memset(mask
, 0xff, sizeof *mask
);
2250 scan_ipv4(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
2254 if (ovs_scan(s
, IP_SCAN_FMT
"%n", IP_SCAN_ARGS(key
), &n
)) {
2258 if (ovs_scan(s
+ len
, "/"IP_SCAN_FMT
"%n",
2259 IP_SCAN_ARGS(mask
), &n
)) {
2262 *mask
= OVS_BE32_MAX
;
2271 scan_ipv6(const char *s
, ovs_be32 (*key
)[4], ovs_be32 (*mask
)[4])
2274 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
2276 if (ovs_scan(s
, IPV6_SCAN_FMT
"%n", ipv6_s
, &n
)
2277 && inet_pton(AF_INET6
, ipv6_s
, key
) == 1) {
2281 if (ovs_scan(s
+ len
, "/"IPV6_SCAN_FMT
"%n", ipv6_s
, &n
)
2282 && inet_pton(AF_INET6
, ipv6_s
, mask
) == 1) {
2285 memset(mask
, 0xff, sizeof *mask
);
2294 scan_ipv6_label(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
2299 if (ovs_scan(s
, "%i%n", &key_
, &n
)
2300 && (key_
& ~IPV6_LABEL_MASK
) == 0) {
2305 if (ovs_scan(s
+ len
, "/%i%n", &mask_
, &n
)
2306 && (mask_
& ~IPV6_LABEL_MASK
) == 0) {
2308 *mask
= htonl(mask_
);
2310 *mask
= htonl(IPV6_LABEL_MASK
);
2319 scan_u8(const char *s
, uint8_t *key
, uint8_t *mask
)
2323 if (ovs_scan(s
, "%"SCNi8
"%n", key
, &n
)) {
2327 if (ovs_scan(s
+ len
, "/%"SCNi8
"%n", mask
, &n
)) {
2339 scan_u32(const char *s
, uint32_t *key
, uint32_t *mask
)
2343 if (ovs_scan(s
, "%"SCNi32
"%n", key
, &n
)) {
2347 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", mask
, &n
)) {
2359 scan_be16(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
2361 uint16_t key_
, mask_
;
2364 if (ovs_scan(s
, "%"SCNi16
"%n", &key_
, &n
)) {
2369 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", &mask_
, &n
)) {
2371 *mask
= htons(mask_
);
2373 *mask
= OVS_BE16_MAX
;
2382 scan_be64(const char *s
, ovs_be64
*key
, ovs_be64
*mask
)
2384 uint64_t key_
, mask_
;
2387 if (ovs_scan(s
, "%"SCNi64
"%n", &key_
, &n
)) {
2390 *key
= htonll(key_
);
2392 if (ovs_scan(s
+ len
, "/%"SCNi64
"%n", &mask_
, &n
)) {
2394 *mask
= htonll(mask_
);
2396 *mask
= OVS_BE64_MAX
;
2405 scan_tun_flags(const char *s
, uint16_t *key
, uint16_t *mask
)
2407 uint32_t flags
, fmask
;
2410 n
= parse_flags(s
, flow_tun_flag_to_string
, &flags
,
2411 FLOW_TNL_F_MASK
, mask
? &fmask
: NULL
);
2412 if (n
>= 0 && s
[n
] == ')') {
2423 scan_tcp_flags(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
2425 uint32_t flags
, fmask
;
2428 n
= parse_flags(s
, packet_tcp_flag_to_string
, &flags
,
2429 TCP_FLAGS(OVS_BE16_MAX
), mask
? &fmask
: NULL
);
2431 *key
= htons(flags
);
2433 *mask
= htons(fmask
);
2441 scan_frag(const char *s
, uint8_t *key
, uint8_t *mask
)
2445 enum ovs_frag_type frag_type
;
2447 if (ovs_scan(s
, "%7[a-z]%n", frag
, &n
)
2448 && ovs_frag_type_from_string(frag
, &frag_type
)) {
2461 scan_port(const char *s
, uint32_t *key
, uint32_t *mask
,
2462 const struct simap
*port_names
)
2466 if (ovs_scan(s
, "%"SCNi32
"%n", key
, &n
)) {
2470 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", mask
, &n
)) {
2477 } else if (port_names
) {
2478 const struct simap_node
*node
;
2481 len
= strcspn(s
, ")");
2482 node
= simap_find_len(port_names
, s
, len
);
2495 /* Helper for vlan parsing. */
2496 struct ovs_key_vlan__
{
2501 set_be16_bf(ovs_be16
*bf
, uint8_t bits
, uint8_t offset
, uint16_t value
)
2503 const uint16_t mask
= ((1U << bits
) - 1) << offset
;
2505 if (value
>> bits
) {
2509 *bf
= htons((ntohs(*bf
) & ~mask
) | (value
<< offset
));
2514 scan_be16_bf(const char *s
, ovs_be16
*key
, ovs_be16
*mask
, uint8_t bits
,
2517 uint16_t key_
, mask_
;
2520 if (ovs_scan(s
, "%"SCNi16
"%n", &key_
, &n
)) {
2523 if (set_be16_bf(key
, bits
, offset
, key_
)) {
2525 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", &mask_
, &n
)) {
2528 if (!set_be16_bf(mask
, bits
, offset
, mask_
)) {
2532 *mask
|= htons(((1U << bits
) - 1) << offset
);
2542 scan_vid(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
2544 return scan_be16_bf(s
, key
, mask
, 12, VLAN_VID_SHIFT
);
2548 scan_pcp(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
2550 return scan_be16_bf(s
, key
, mask
, 3, VLAN_PCP_SHIFT
);
2554 scan_cfi(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
2556 return scan_be16_bf(s
, key
, mask
, 1, VLAN_CFI_SHIFT
);
2561 set_be32_bf(ovs_be32
*bf
, uint8_t bits
, uint8_t offset
, uint32_t value
)
2563 const uint32_t mask
= ((1U << bits
) - 1) << offset
;
2565 if (value
>> bits
) {
2569 *bf
= htonl((ntohl(*bf
) & ~mask
) | (value
<< offset
));
2574 scan_be32_bf(const char *s
, ovs_be32
*key
, ovs_be32
*mask
, uint8_t bits
,
2577 uint32_t key_
, mask_
;
2580 if (ovs_scan(s
, "%"SCNi32
"%n", &key_
, &n
)) {
2583 if (set_be32_bf(key
, bits
, offset
, key_
)) {
2585 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", &mask_
, &n
)) {
2588 if (!set_be32_bf(mask
, bits
, offset
, mask_
)) {
2592 *mask
|= htonl(((1U << bits
) - 1) << offset
);
2602 scan_mpls_label(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
2604 return scan_be32_bf(s
, key
, mask
, 20, MPLS_LABEL_SHIFT
);
2608 scan_mpls_tc(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
2610 return scan_be32_bf(s
, key
, mask
, 3, MPLS_TC_SHIFT
);
2614 scan_mpls_ttl(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
2616 return scan_be32_bf(s
, key
, mask
, 8, MPLS_TTL_SHIFT
);
2620 scan_mpls_bos(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
2622 return scan_be32_bf(s
, key
, mask
, 1, MPLS_BOS_SHIFT
);
2625 /* ATTR is compile-time constant, so only the case with correct data type
2626 * will be used. However, the compiler complains about the data type for
2627 * the other cases, so we must cast to make the compiler silent. */
2628 #define SCAN_PUT_ATTR(BUF, ATTR, DATA) \
2629 if ((ATTR) == OVS_KEY_ATTR_TUNNEL) { \
2630 tun_key_to_attr(BUF, (const struct flow_tnl *)(void *)&(DATA)); \
2632 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
2635 #define SCAN_IF(NAME) \
2636 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
2637 const char *start = s; \
2642 /* Usually no special initialization is needed. */
2643 #define SCAN_BEGIN(NAME, TYPE) \
2646 memset(&skey, 0, sizeof skey); \
2647 memset(&smask, 0, sizeof smask); \
2651 /* Init as fully-masked as mask will not be scanned. */
2652 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
2655 memset(&skey, 0, sizeof skey); \
2656 memset(&smask, 0xff, sizeof smask); \
2660 /* VLAN needs special initialization. */
2661 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
2663 TYPE skey = KEY_INIT; \
2664 TYPE smask = MASK_INIT; \
2668 /* Scan unnamed entry as 'TYPE' */
2669 #define SCAN_TYPE(TYPE, KEY, MASK) \
2670 len = scan_##TYPE(s, KEY, MASK); \
2676 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
2677 #define SCAN_FIELD(NAME, TYPE, FIELD) \
2678 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
2679 s += strlen(NAME); \
2680 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
2684 #define SCAN_FINISH() \
2685 } while (*s++ == ',' && len != 0); \
2686 if (s[-1] != ')') { \
2690 #define SCAN_FINISH_SINGLE() \
2692 if (*s++ != ')') { \
2696 #define SCAN_PUT(ATTR) \
2697 if (!mask || !is_all_zeros(&smask, sizeof smask)) { \
2698 SCAN_PUT_ATTR(key, ATTR, skey); \
2700 SCAN_PUT_ATTR(mask, ATTR, smask); \
2704 #define SCAN_END(ATTR) \
2710 #define SCAN_END_SINGLE(ATTR) \
2711 SCAN_FINISH_SINGLE(); \
2716 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
2717 SCAN_BEGIN(NAME, TYPE) { \
2718 SCAN_TYPE(SCAN_AS, &skey, &smask); \
2719 } SCAN_END_SINGLE(ATTR)
2721 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
2722 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
2723 SCAN_TYPE(SCAN_AS, &skey, NULL); \
2724 } SCAN_END_SINGLE(ATTR)
2726 /* scan_port needs one extra argument. */
2727 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
2728 SCAN_BEGIN(NAME, TYPE) { \
2729 len = scan_port(s, &skey, &smask, port_names); \
2734 } SCAN_END_SINGLE(ATTR)
2737 parse_odp_key_mask_attr(const char *s
, const struct simap
*port_names
,
2738 struct ofpbuf
*key
, struct ofpbuf
*mask
)
2740 if (!strncmp(s
, "ufid:", 5)) {
2741 const char *start
= s
;
2745 s
+= strspn(s
, hex_chars
);
2746 s
+= strspn(s
, delimiters
);
2751 SCAN_SINGLE("skb_priority(", uint32_t, u32
, OVS_KEY_ATTR_PRIORITY
);
2752 SCAN_SINGLE("skb_mark(", uint32_t, u32
, OVS_KEY_ATTR_SKB_MARK
);
2753 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32
,
2754 OVS_KEY_ATTR_RECIRC_ID
);
2755 SCAN_SINGLE("dp_hash(", uint32_t, u32
, OVS_KEY_ATTR_DP_HASH
);
2757 SCAN_BEGIN("tunnel(", struct flow_tnl
) {
2758 SCAN_FIELD("tun_id=", be64
, tun_id
);
2759 SCAN_FIELD("src=", ipv4
, ip_src
);
2760 SCAN_FIELD("dst=", ipv4
, ip_dst
);
2761 SCAN_FIELD("tos=", u8
, ip_tos
);
2762 SCAN_FIELD("ttl=", u8
, ip_ttl
);
2763 SCAN_FIELD("tp_src=", be16
, tp_src
);
2764 SCAN_FIELD("tp_dst=", be16
, tp_dst
);
2765 SCAN_FIELD("gbp_id=", be16
, gbp_id
);
2766 SCAN_FIELD("gbp_flags=", u8
, gbp_flags
);
2767 SCAN_FIELD("flags(", tun_flags
, flags
);
2768 } SCAN_END(OVS_KEY_ATTR_TUNNEL
);
2770 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT
);
2772 SCAN_BEGIN("eth(", struct ovs_key_ethernet
) {
2773 SCAN_FIELD("src=", eth
, eth_src
);
2774 SCAN_FIELD("dst=", eth
, eth_dst
);
2775 } SCAN_END(OVS_KEY_ATTR_ETHERNET
);
2777 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__
,
2778 { htons(VLAN_CFI
) }, { htons(VLAN_CFI
) }) {
2779 SCAN_FIELD("vid=", vid
, tci
);
2780 SCAN_FIELD("pcp=", pcp
, tci
);
2781 SCAN_FIELD("cfi=", cfi
, tci
);
2782 } SCAN_END(OVS_KEY_ATTR_VLAN
);
2784 SCAN_SINGLE("eth_type(", ovs_be16
, be16
, OVS_KEY_ATTR_ETHERTYPE
);
2786 SCAN_BEGIN("mpls(", struct ovs_key_mpls
) {
2787 SCAN_FIELD("label=", mpls_label
, mpls_lse
);
2788 SCAN_FIELD("tc=", mpls_tc
, mpls_lse
);
2789 SCAN_FIELD("ttl=", mpls_ttl
, mpls_lse
);
2790 SCAN_FIELD("bos=", mpls_bos
, mpls_lse
);
2791 } SCAN_END(OVS_KEY_ATTR_MPLS
);
2793 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4
) {
2794 SCAN_FIELD("src=", ipv4
, ipv4_src
);
2795 SCAN_FIELD("dst=", ipv4
, ipv4_dst
);
2796 SCAN_FIELD("proto=", u8
, ipv4_proto
);
2797 SCAN_FIELD("tos=", u8
, ipv4_tos
);
2798 SCAN_FIELD("ttl=", u8
, ipv4_ttl
);
2799 SCAN_FIELD("frag=", frag
, ipv4_frag
);
2800 } SCAN_END(OVS_KEY_ATTR_IPV4
);
2802 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6
) {
2803 SCAN_FIELD("src=", ipv6
, ipv6_src
);
2804 SCAN_FIELD("dst=", ipv6
, ipv6_dst
);
2805 SCAN_FIELD("label=", ipv6_label
, ipv6_label
);
2806 SCAN_FIELD("proto=", u8
, ipv6_proto
);
2807 SCAN_FIELD("tclass=", u8
, ipv6_tclass
);
2808 SCAN_FIELD("hlimit=", u8
, ipv6_hlimit
);
2809 SCAN_FIELD("frag=", frag
, ipv6_frag
);
2810 } SCAN_END(OVS_KEY_ATTR_IPV6
);
2812 SCAN_BEGIN("tcp(", struct ovs_key_tcp
) {
2813 SCAN_FIELD("src=", be16
, tcp_src
);
2814 SCAN_FIELD("dst=", be16
, tcp_dst
);
2815 } SCAN_END(OVS_KEY_ATTR_TCP
);
2817 SCAN_SINGLE("tcp_flags(", ovs_be16
, tcp_flags
, OVS_KEY_ATTR_TCP_FLAGS
);
2819 SCAN_BEGIN("udp(", struct ovs_key_udp
) {
2820 SCAN_FIELD("src=", be16
, udp_src
);
2821 SCAN_FIELD("dst=", be16
, udp_dst
);
2822 } SCAN_END(OVS_KEY_ATTR_UDP
);
2824 SCAN_BEGIN("sctp(", struct ovs_key_sctp
) {
2825 SCAN_FIELD("src=", be16
, sctp_src
);
2826 SCAN_FIELD("dst=", be16
, sctp_dst
);
2827 } SCAN_END(OVS_KEY_ATTR_SCTP
);
2829 SCAN_BEGIN("icmp(", struct ovs_key_icmp
) {
2830 SCAN_FIELD("type=", u8
, icmp_type
);
2831 SCAN_FIELD("code=", u8
, icmp_code
);
2832 } SCAN_END(OVS_KEY_ATTR_ICMP
);
2834 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6
) {
2835 SCAN_FIELD("type=", u8
, icmpv6_type
);
2836 SCAN_FIELD("code=", u8
, icmpv6_code
);
2837 } SCAN_END(OVS_KEY_ATTR_ICMPV6
);
2839 SCAN_BEGIN("arp(", struct ovs_key_arp
) {
2840 SCAN_FIELD("sip=", ipv4
, arp_sip
);
2841 SCAN_FIELD("tip=", ipv4
, arp_tip
);
2842 SCAN_FIELD("op=", be16
, arp_op
);
2843 SCAN_FIELD("sha=", eth
, arp_sha
);
2844 SCAN_FIELD("tha=", eth
, arp_tha
);
2845 } SCAN_END(OVS_KEY_ATTR_ARP
);
2847 SCAN_BEGIN("nd(", struct ovs_key_nd
) {
2848 SCAN_FIELD("target=", ipv6
, nd_target
);
2849 SCAN_FIELD("sll=", eth
, nd_sll
);
2850 SCAN_FIELD("tll=", eth
, nd_tll
);
2851 } SCAN_END(OVS_KEY_ATTR_ND
);
2853 /* Encap open-coded. */
2854 if (!strncmp(s
, "encap(", 6)) {
2855 const char *start
= s
;
2856 size_t encap
, encap_mask
= 0;
2858 encap
= nl_msg_start_nested(key
, OVS_KEY_ATTR_ENCAP
);
2860 encap_mask
= nl_msg_start_nested(mask
, OVS_KEY_ATTR_ENCAP
);
2867 s
+= strspn(s
, delimiters
);
2870 } else if (*s
== ')') {
2874 retval
= parse_odp_key_mask_attr(s
, port_names
, key
, mask
);
2882 nl_msg_end_nested(key
, encap
);
2884 nl_msg_end_nested(mask
, encap_mask
);
2893 /* Parses the string representation of a datapath flow key, in the
2894 * format output by odp_flow_key_format(). Returns 0 if successful,
2895 * otherwise a positive errno value. On success, the flow key is
2896 * appended to 'key' as a series of Netlink attributes. On failure, no
2897 * data is appended to 'key'. Either way, 'key''s data might be
2900 * If 'port_names' is nonnull, it points to an simap that maps from a port name
2901 * to a port number. (Port names may be used instead of port numbers in
2904 * On success, the attributes appended to 'key' are individually syntactically
2905 * valid, but they may not be valid as a sequence. 'key' might, for example,
2906 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
2908 odp_flow_from_string(const char *s
, const struct simap
*port_names
,
2909 struct ofpbuf
*key
, struct ofpbuf
*mask
)
2911 const size_t old_size
= key
->size
;
2915 s
+= strspn(s
, delimiters
);
2920 retval
= parse_odp_key_mask_attr(s
, port_names
, key
, mask
);
2922 key
->size
= old_size
;
2932 ovs_to_odp_frag(uint8_t nw_frag
, bool is_mask
)
2935 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
2936 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
2937 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
2938 * must use a zero mask for the netlink frag field, and all ones mask
2940 return (nw_frag
& FLOW_NW_FRAG_ANY
) ? UINT8_MAX
: 0;
2942 return !(nw_frag
& FLOW_NW_FRAG_ANY
) ? OVS_FRAG_TYPE_NONE
2943 : nw_frag
& FLOW_NW_FRAG_LATER
? OVS_FRAG_TYPE_LATER
2944 : OVS_FRAG_TYPE_FIRST
;
2947 static void get_ethernet_key(const struct flow
*, struct ovs_key_ethernet
*);
2948 static void put_ethernet_key(const struct ovs_key_ethernet
*, struct flow
*);
2949 static void get_ipv4_key(const struct flow
*, struct ovs_key_ipv4
*,
2951 static void put_ipv4_key(const struct ovs_key_ipv4
*, struct flow
*,
2953 static void get_ipv6_key(const struct flow
*, struct ovs_key_ipv6
*,
2955 static void put_ipv6_key(const struct ovs_key_ipv6
*, struct flow
*,
2957 static void get_arp_key(const struct flow
*, struct ovs_key_arp
*);
2958 static void put_arp_key(const struct ovs_key_arp
*, struct flow
*);
2959 static void get_nd_key(const struct flow
*, struct ovs_key_nd
*);
2960 static void put_nd_key(const struct ovs_key_nd
*, struct flow
*);
2962 /* These share the same layout. */
2964 struct ovs_key_tcp tcp
;
2965 struct ovs_key_udp udp
;
2966 struct ovs_key_sctp sctp
;
2969 static void get_tp_key(const struct flow
*, union ovs_key_tp
*);
2970 static void put_tp_key(const union ovs_key_tp
*, struct flow
*);
2973 odp_flow_key_from_flow__(struct ofpbuf
*buf
, const struct flow
*flow
,
2974 const struct flow
*mask
, odp_port_t odp_in_port
,
2975 size_t max_mpls_depth
, bool recirc
, bool export_mask
)
2977 struct ovs_key_ethernet
*eth_key
;
2979 const struct flow
*data
= export_mask
? mask
: flow
;
2981 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, data
->skb_priority
);
2983 if (flow
->tunnel
.ip_dst
|| export_mask
) {
2984 tun_key_to_attr(buf
, &data
->tunnel
);
2987 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, data
->pkt_mark
);
2990 nl_msg_put_u32(buf
, OVS_KEY_ATTR_RECIRC_ID
, data
->recirc_id
);
2991 nl_msg_put_u32(buf
, OVS_KEY_ATTR_DP_HASH
, data
->dp_hash
);
2994 /* Add an ingress port attribute if this is a mask or 'odp_in_port'
2995 * is not the magical value "ODPP_NONE". */
2996 if (export_mask
|| odp_in_port
!= ODPP_NONE
) {
2997 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, odp_in_port
);
3000 eth_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ETHERNET
,
3002 get_ethernet_key(data
, eth_key
);
3004 if (flow
->vlan_tci
!= htons(0) || flow
->dl_type
== htons(ETH_TYPE_VLAN
)) {
3006 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, OVS_BE16_MAX
);
3008 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, htons(ETH_TYPE_VLAN
));
3010 nl_msg_put_be16(buf
, OVS_KEY_ATTR_VLAN
, data
->vlan_tci
);
3011 encap
= nl_msg_start_nested(buf
, OVS_KEY_ATTR_ENCAP
);
3012 if (flow
->vlan_tci
== htons(0)) {
3019 if (ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
3020 /* For backwards compatibility with kernels that don't support
3021 * wildcarding, the following convention is used to encode the
3022 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
3025 * -------- -------- -------
3026 * >0x5ff 0xffff Specified Ethernet II Ethertype.
3027 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
3028 * <none> 0xffff Any non-Ethernet II frame (except valid
3029 * 802.3 SNAP packet with valid eth_type).
3032 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, OVS_BE16_MAX
);
3037 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, data
->dl_type
);
3039 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
3040 struct ovs_key_ipv4
*ipv4_key
;
3042 ipv4_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV4
,
3044 get_ipv4_key(data
, ipv4_key
, export_mask
);
3045 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
3046 struct ovs_key_ipv6
*ipv6_key
;
3048 ipv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV6
,
3050 get_ipv6_key(data
, ipv6_key
, export_mask
);
3051 } else if (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
3052 flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
3053 struct ovs_key_arp
*arp_key
;
3055 arp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ARP
,
3057 get_arp_key(data
, arp_key
);
3058 } else if (eth_type_mpls(flow
->dl_type
)) {
3059 struct ovs_key_mpls
*mpls_key
;
3062 n
= flow_count_mpls_labels(flow
, NULL
);
3063 n
= MIN(n
, max_mpls_depth
);
3064 mpls_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_MPLS
,
3065 n
* sizeof *mpls_key
);
3066 for (i
= 0; i
< n
; i
++) {
3067 mpls_key
[i
].mpls_lse
= data
->mpls_lse
[i
];
3071 if (is_ip_any(flow
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
3072 if (flow
->nw_proto
== IPPROTO_TCP
) {
3073 union ovs_key_tp
*tcp_key
;
3075 tcp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_TCP
,
3077 get_tp_key(data
, tcp_key
);
3078 if (data
->tcp_flags
) {
3079 nl_msg_put_be16(buf
, OVS_KEY_ATTR_TCP_FLAGS
, data
->tcp_flags
);
3081 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
3082 union ovs_key_tp
*udp_key
;
3084 udp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_UDP
,
3086 get_tp_key(data
, udp_key
);
3087 } else if (flow
->nw_proto
== IPPROTO_SCTP
) {
3088 union ovs_key_tp
*sctp_key
;
3090 sctp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_SCTP
,
3092 get_tp_key(data
, sctp_key
);
3093 } else if (flow
->dl_type
== htons(ETH_TYPE_IP
)
3094 && flow
->nw_proto
== IPPROTO_ICMP
) {
3095 struct ovs_key_icmp
*icmp_key
;
3097 icmp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMP
,
3099 icmp_key
->icmp_type
= ntohs(data
->tp_src
);
3100 icmp_key
->icmp_code
= ntohs(data
->tp_dst
);
3101 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)
3102 && flow
->nw_proto
== IPPROTO_ICMPV6
) {
3103 struct ovs_key_icmpv6
*icmpv6_key
;
3105 icmpv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMPV6
,
3106 sizeof *icmpv6_key
);
3107 icmpv6_key
->icmpv6_type
= ntohs(data
->tp_src
);
3108 icmpv6_key
->icmpv6_code
= ntohs(data
->tp_dst
);
3110 if (flow
->tp_dst
== htons(0)
3111 && (flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
)
3112 || flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
))
3113 && (!export_mask
|| (data
->tp_src
== htons(0xffff)
3114 && data
->tp_dst
== htons(0xffff)))) {
3116 struct ovs_key_nd
*nd_key
;
3118 nd_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ND
,
3120 memcpy(nd_key
->nd_target
, &data
->nd_target
,
3121 sizeof nd_key
->nd_target
);
3122 memcpy(nd_key
->nd_sll
, data
->arp_sha
, ETH_ADDR_LEN
);
3123 memcpy(nd_key
->nd_tll
, data
->arp_tha
, ETH_ADDR_LEN
);
3130 nl_msg_end_nested(buf
, encap
);
3134 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
3135 * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
3136 * number rather than a datapath port number). Instead, if 'odp_in_port'
3137 * is anything other than ODPP_NONE, it is included in 'buf' as the input
3140 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
3141 * capable of being expanded to allow for that much space.
3143 * 'recirc' indicates support for recirculation fields. If this is true, then
3144 * these fields will always be serialised. */
3146 odp_flow_key_from_flow(struct ofpbuf
*buf
, const struct flow
*flow
,
3147 const struct flow
*mask
, odp_port_t odp_in_port
,
3150 odp_flow_key_from_flow__(buf
, flow
, mask
, odp_in_port
, SIZE_MAX
, recirc
,
3154 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
3155 * 'buf'. 'flow' is used as a template to determine how to interpret
3156 * 'mask'. For example, the 'dl_type' of 'mask' describes the mask, but
3157 * it doesn't indicate whether the other fields should be interpreted as
3158 * ARP, IPv4, IPv6, etc.
3160 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
3161 * capable of being expanded to allow for that much space.
3163 * 'recirc' indicates support for recirculation fields. If this is true, then
3164 * these fields will always be serialised. */
3166 odp_flow_key_from_mask(struct ofpbuf
*buf
, const struct flow
*mask
,
3167 const struct flow
*flow
, uint32_t odp_in_port_mask
,
3168 size_t max_mpls_depth
, bool recirc
)
3170 odp_flow_key_from_flow__(buf
, flow
, mask
, u32_to_odp(odp_in_port_mask
),
3171 max_mpls_depth
, recirc
, true);
3174 /* Generate ODP flow key from the given packet metadata */
3176 odp_key_from_pkt_metadata(struct ofpbuf
*buf
, const struct pkt_metadata
*md
)
3178 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, md
->skb_priority
);
3180 if (md
->tunnel
.ip_dst
) {
3181 tun_key_to_attr(buf
, &md
->tunnel
);
3184 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, md
->pkt_mark
);
3186 /* Add an ingress port attribute if 'odp_in_port' is not the magical
3187 * value "ODPP_NONE". */
3188 if (md
->in_port
.odp_port
!= ODPP_NONE
) {
3189 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, md
->in_port
.odp_port
);
3193 /* Generate packet metadata from the given ODP flow key. */
3195 odp_key_to_pkt_metadata(const struct nlattr
*key
, size_t key_len
,
3196 struct pkt_metadata
*md
)
3198 const struct nlattr
*nla
;
3200 uint32_t wanted_attrs
= 1u << OVS_KEY_ATTR_PRIORITY
|
3201 1u << OVS_KEY_ATTR_SKB_MARK
| 1u << OVS_KEY_ATTR_TUNNEL
|
3202 1u << OVS_KEY_ATTR_IN_PORT
;
3204 *md
= PKT_METADATA_INITIALIZER(ODPP_NONE
);
3206 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
3207 uint16_t type
= nl_attr_type(nla
);
3208 size_t len
= nl_attr_get_size(nla
);
3209 int expected_len
= odp_key_attr_len(ovs_flow_key_attr_lens
,
3210 OVS_KEY_ATTR_MAX
, type
);
3212 if (len
!= expected_len
&& expected_len
>= 0) {
3217 case OVS_KEY_ATTR_RECIRC_ID
:
3218 md
->recirc_id
= nl_attr_get_u32(nla
);
3219 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_RECIRC_ID
);
3221 case OVS_KEY_ATTR_DP_HASH
:
3222 md
->dp_hash
= nl_attr_get_u32(nla
);
3223 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_DP_HASH
);
3225 case OVS_KEY_ATTR_PRIORITY
:
3226 md
->skb_priority
= nl_attr_get_u32(nla
);
3227 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_PRIORITY
);
3229 case OVS_KEY_ATTR_SKB_MARK
:
3230 md
->pkt_mark
= nl_attr_get_u32(nla
);
3231 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_SKB_MARK
);
3233 case OVS_KEY_ATTR_TUNNEL
: {
3234 enum odp_key_fitness res
;
3236 res
= odp_tun_key_from_attr(nla
, &md
->tunnel
);
3237 if (res
== ODP_FIT_ERROR
) {
3238 memset(&md
->tunnel
, 0, sizeof md
->tunnel
);
3239 } else if (res
== ODP_FIT_PERFECT
) {
3240 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_TUNNEL
);
3244 case OVS_KEY_ATTR_IN_PORT
:
3245 md
->in_port
.odp_port
= nl_attr_get_odp_port(nla
);
3246 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_IN_PORT
);
3252 if (!wanted_attrs
) {
3253 return; /* Have everything. */
3259 odp_flow_key_hash(const struct nlattr
*key
, size_t key_len
)
3261 BUILD_ASSERT_DECL(!(NLA_ALIGNTO
% sizeof(uint32_t)));
3262 return hash_words(ALIGNED_CAST(const uint32_t *, key
),
3263 key_len
/ sizeof(uint32_t), 0);
3267 log_odp_key_attributes(struct vlog_rate_limit
*rl
, const char *title
,
3268 uint64_t attrs
, int out_of_range_attr
,
3269 const struct nlattr
*key
, size_t key_len
)
3274 if (VLOG_DROP_DBG(rl
)) {
3279 for (i
= 0; i
< 64; i
++) {
3280 if (attrs
& (UINT64_C(1) << i
)) {
3281 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
3283 ds_put_format(&s
, " %s",
3284 ovs_key_attr_to_string(i
, namebuf
, sizeof namebuf
));
3287 if (out_of_range_attr
) {
3288 ds_put_format(&s
, " %d (and possibly others)", out_of_range_attr
);
3291 ds_put_cstr(&s
, ": ");
3292 odp_flow_key_format(key
, key_len
, &s
);
3294 VLOG_DBG("%s:%s", title
, ds_cstr(&s
));
3299 odp_to_ovs_frag(uint8_t odp_frag
, bool is_mask
)
3301 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3304 return odp_frag
? FLOW_NW_FRAG_MASK
: 0;
3307 if (odp_frag
> OVS_FRAG_TYPE_LATER
) {
3308 VLOG_ERR_RL(&rl
, "invalid frag %"PRIu8
" in flow key", odp_frag
);
3309 return 0xff; /* Error. */
3312 return (odp_frag
== OVS_FRAG_TYPE_NONE
) ? 0
3313 : (odp_frag
== OVS_FRAG_TYPE_FIRST
) ? FLOW_NW_FRAG_ANY
3314 : FLOW_NW_FRAG_ANY
| FLOW_NW_FRAG_LATER
;
3318 parse_flow_nlattrs(const struct nlattr
*key
, size_t key_len
,
3319 const struct nlattr
*attrs
[], uint64_t *present_attrsp
,
3320 int *out_of_range_attrp
)
3322 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
3323 const struct nlattr
*nla
;
3324 uint64_t present_attrs
;
3327 BUILD_ASSERT(OVS_KEY_ATTR_MAX
< CHAR_BIT
* sizeof present_attrs
);
3329 *out_of_range_attrp
= 0;
3330 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
3331 uint16_t type
= nl_attr_type(nla
);
3332 size_t len
= nl_attr_get_size(nla
);
3333 int expected_len
= odp_key_attr_len(ovs_flow_key_attr_lens
,
3334 OVS_KEY_ATTR_MAX
, type
);
3336 if (len
!= expected_len
&& expected_len
>= 0) {
3337 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
3339 VLOG_ERR_RL(&rl
, "attribute %s has length %"PRIuSIZE
" but should have "
3340 "length %d", ovs_key_attr_to_string(type
, namebuf
,
3346 if (type
> OVS_KEY_ATTR_MAX
) {
3347 *out_of_range_attrp
= type
;
3349 if (present_attrs
& (UINT64_C(1) << type
)) {
3350 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
3352 VLOG_ERR_RL(&rl
, "duplicate %s attribute in flow key",
3353 ovs_key_attr_to_string(type
,
3354 namebuf
, sizeof namebuf
));
3358 present_attrs
|= UINT64_C(1) << type
;
3363 VLOG_ERR_RL(&rl
, "trailing garbage in flow key");
3367 *present_attrsp
= present_attrs
;
3371 static enum odp_key_fitness
3372 check_expectations(uint64_t present_attrs
, int out_of_range_attr
,
3373 uint64_t expected_attrs
,
3374 const struct nlattr
*key
, size_t key_len
)
3376 uint64_t missing_attrs
;
3377 uint64_t extra_attrs
;
3379 missing_attrs
= expected_attrs
& ~present_attrs
;
3380 if (missing_attrs
) {
3381 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
3382 log_odp_key_attributes(&rl
, "expected but not present",
3383 missing_attrs
, 0, key
, key_len
);
3384 return ODP_FIT_TOO_LITTLE
;
3387 extra_attrs
= present_attrs
& ~expected_attrs
;
3388 if (extra_attrs
|| out_of_range_attr
) {
3389 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
3390 log_odp_key_attributes(&rl
, "present but not expected",
3391 extra_attrs
, out_of_range_attr
, key
, key_len
);
3392 return ODP_FIT_TOO_MUCH
;
3395 return ODP_FIT_PERFECT
;
3399 parse_ethertype(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
3400 uint64_t present_attrs
, uint64_t *expected_attrs
,
3401 struct flow
*flow
, const struct flow
*src_flow
)
3403 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3404 bool is_mask
= flow
!= src_flow
;
3406 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
)) {
3407 flow
->dl_type
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_ETHERTYPE
]);
3408 if (!is_mask
&& ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
3409 VLOG_ERR_RL(&rl
, "invalid Ethertype %"PRIu16
" in flow key",
3410 ntohs(flow
->dl_type
));
3413 if (is_mask
&& ntohs(src_flow
->dl_type
) < ETH_TYPE_MIN
&&
3414 flow
->dl_type
!= htons(0xffff)) {
3417 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
;
3420 flow
->dl_type
= htons(FLOW_DL_TYPE_NONE
);
3421 } else if (ntohs(src_flow
->dl_type
) < ETH_TYPE_MIN
) {
3422 /* See comments in odp_flow_key_from_flow__(). */
3423 VLOG_ERR_RL(&rl
, "mask expected for non-Ethernet II frame");
3430 static enum odp_key_fitness
3431 parse_l2_5_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
3432 uint64_t present_attrs
, int out_of_range_attr
,
3433 uint64_t expected_attrs
, struct flow
*flow
,
3434 const struct nlattr
*key
, size_t key_len
,
3435 const struct flow
*src_flow
)
3437 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3438 bool is_mask
= src_flow
!= flow
;
3439 const void *check_start
= NULL
;
3440 size_t check_len
= 0;
3441 enum ovs_key_attr expected_bit
= 0xff;
3443 if (eth_type_mpls(src_flow
->dl_type
)) {
3444 if (!is_mask
|| present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
3445 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_MPLS
);
3447 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
3448 size_t size
= nl_attr_get_size(attrs
[OVS_KEY_ATTR_MPLS
]);
3449 const ovs_be32
*mpls_lse
= nl_attr_get(attrs
[OVS_KEY_ATTR_MPLS
]);
3450 int n
= size
/ sizeof(ovs_be32
);
3453 if (!size
|| size
% sizeof(ovs_be32
)) {
3454 return ODP_FIT_ERROR
;
3456 if (flow
->mpls_lse
[0] && flow
->dl_type
!= htons(0xffff)) {
3457 return ODP_FIT_ERROR
;
3460 for (i
= 0; i
< n
&& i
< FLOW_MAX_MPLS_LABELS
; i
++) {
3461 flow
->mpls_lse
[i
] = mpls_lse
[i
];
3463 if (n
> FLOW_MAX_MPLS_LABELS
) {
3464 return ODP_FIT_TOO_MUCH
;
3468 /* BOS may be set only in the innermost label. */
3469 for (i
= 0; i
< n
- 1; i
++) {
3470 if (flow
->mpls_lse
[i
] & htonl(MPLS_BOS_MASK
)) {
3471 return ODP_FIT_ERROR
;
3475 /* BOS must be set in the innermost label. */
3476 if (n
< FLOW_MAX_MPLS_LABELS
3477 && !(flow
->mpls_lse
[n
- 1] & htonl(MPLS_BOS_MASK
))) {
3478 return ODP_FIT_TOO_LITTLE
;
3484 } else if (src_flow
->dl_type
== htons(ETH_TYPE_IP
)) {
3486 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV4
;
3488 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
3489 const struct ovs_key_ipv4
*ipv4_key
;
3491 ipv4_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV4
]);
3492 put_ipv4_key(ipv4_key
, flow
, is_mask
);
3493 if (flow
->nw_frag
> FLOW_NW_FRAG_MASK
) {
3494 return ODP_FIT_ERROR
;
3497 check_start
= ipv4_key
;
3498 check_len
= sizeof *ipv4_key
;
3499 expected_bit
= OVS_KEY_ATTR_IPV4
;
3502 } else if (src_flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
3504 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV6
;
3506 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
3507 const struct ovs_key_ipv6
*ipv6_key
;
3509 ipv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV6
]);
3510 put_ipv6_key(ipv6_key
, flow
, is_mask
);
3511 if (flow
->nw_frag
> FLOW_NW_FRAG_MASK
) {
3512 return ODP_FIT_ERROR
;
3515 check_start
= ipv6_key
;
3516 check_len
= sizeof *ipv6_key
;
3517 expected_bit
= OVS_KEY_ATTR_IPV6
;
3520 } else if (src_flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
3521 src_flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
3523 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ARP
;
3525 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ARP
)) {
3526 const struct ovs_key_arp
*arp_key
;
3528 arp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ARP
]);
3529 if (!is_mask
&& (arp_key
->arp_op
& htons(0xff00))) {
3530 VLOG_ERR_RL(&rl
, "unsupported ARP opcode %"PRIu16
" in flow "
3531 "key", ntohs(arp_key
->arp_op
));
3532 return ODP_FIT_ERROR
;
3534 put_arp_key(arp_key
, flow
);
3536 check_start
= arp_key
;
3537 check_len
= sizeof *arp_key
;
3538 expected_bit
= OVS_KEY_ATTR_ARP
;
3544 if (check_len
> 0) { /* Happens only when 'is_mask'. */
3545 if (!is_all_zeros(check_start
, check_len
) &&
3546 flow
->dl_type
!= htons(0xffff)) {
3547 return ODP_FIT_ERROR
;
3549 expected_attrs
|= UINT64_C(1) << expected_bit
;
3553 expected_bit
= OVS_KEY_ATTR_UNSPEC
;
3554 if (src_flow
->nw_proto
== IPPROTO_TCP
3555 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
3556 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
3557 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
3559 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP
;
3561 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP
)) {
3562 const union ovs_key_tp
*tcp_key
;
3564 tcp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_TCP
]);
3565 put_tp_key(tcp_key
, flow
);
3566 expected_bit
= OVS_KEY_ATTR_TCP
;
3568 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS
)) {
3569 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS
;
3570 flow
->tcp_flags
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_TCP_FLAGS
]);
3572 } else if (src_flow
->nw_proto
== IPPROTO_UDP
3573 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
3574 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
3575 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
3577 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_UDP
;
3579 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_UDP
)) {
3580 const union ovs_key_tp
*udp_key
;
3582 udp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_UDP
]);
3583 put_tp_key(udp_key
, flow
);
3584 expected_bit
= OVS_KEY_ATTR_UDP
;
3586 } else if (src_flow
->nw_proto
== IPPROTO_SCTP
3587 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
3588 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
3589 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
3591 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SCTP
;
3593 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SCTP
)) {
3594 const union ovs_key_tp
*sctp_key
;
3596 sctp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_SCTP
]);
3597 put_tp_key(sctp_key
, flow
);
3598 expected_bit
= OVS_KEY_ATTR_SCTP
;
3600 } else if (src_flow
->nw_proto
== IPPROTO_ICMP
3601 && src_flow
->dl_type
== htons(ETH_TYPE_IP
)
3602 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
3604 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMP
;
3606 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMP
)) {
3607 const struct ovs_key_icmp
*icmp_key
;
3609 icmp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMP
]);
3610 flow
->tp_src
= htons(icmp_key
->icmp_type
);
3611 flow
->tp_dst
= htons(icmp_key
->icmp_code
);
3612 expected_bit
= OVS_KEY_ATTR_ICMP
;
3614 } else if (src_flow
->nw_proto
== IPPROTO_ICMPV6
3615 && src_flow
->dl_type
== htons(ETH_TYPE_IPV6
)
3616 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
3618 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
;
3620 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
)) {
3621 const struct ovs_key_icmpv6
*icmpv6_key
;
3623 icmpv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMPV6
]);
3624 flow
->tp_src
= htons(icmpv6_key
->icmpv6_type
);
3625 flow
->tp_dst
= htons(icmpv6_key
->icmpv6_code
);
3626 expected_bit
= OVS_KEY_ATTR_ICMPV6
;
3627 if (src_flow
->tp_dst
== htons(0) &&
3628 (src_flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
) ||
3629 src_flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
))) {
3631 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
3633 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ND
)) {
3634 const struct ovs_key_nd
*nd_key
;
3636 nd_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ND
]);
3637 memcpy(&flow
->nd_target
, nd_key
->nd_target
,
3638 sizeof flow
->nd_target
);
3639 memcpy(flow
->arp_sha
, nd_key
->nd_sll
, ETH_ADDR_LEN
);
3640 memcpy(flow
->arp_tha
, nd_key
->nd_tll
, ETH_ADDR_LEN
);
3642 if (!is_all_zeros(nd_key
, sizeof *nd_key
) &&
3643 (flow
->tp_src
!= htons(0xffff) ||
3644 flow
->tp_dst
!= htons(0xffff))) {
3645 return ODP_FIT_ERROR
;
3647 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
3654 if (is_mask
&& expected_bit
!= OVS_KEY_ATTR_UNSPEC
) {
3655 if ((flow
->tp_src
|| flow
->tp_dst
) && flow
->nw_proto
!= 0xff) {
3656 return ODP_FIT_ERROR
;
3658 expected_attrs
|= UINT64_C(1) << expected_bit
;
3663 return check_expectations(present_attrs
, out_of_range_attr
, expected_attrs
,
3667 /* Parse 802.1Q header then encapsulated L3 attributes. */
3668 static enum odp_key_fitness
3669 parse_8021q_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
3670 uint64_t present_attrs
, int out_of_range_attr
,
3671 uint64_t expected_attrs
, struct flow
*flow
,
3672 const struct nlattr
*key
, size_t key_len
,
3673 const struct flow
*src_flow
)
3675 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3676 bool is_mask
= src_flow
!= flow
;
3678 const struct nlattr
*encap
3679 = (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)
3680 ? attrs
[OVS_KEY_ATTR_ENCAP
] : NULL
);
3681 enum odp_key_fitness encap_fitness
;
3682 enum odp_key_fitness fitness
;
3684 /* Calculate fitness of outer attributes. */
3686 expected_attrs
|= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN
) |
3687 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
));
3689 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) {
3690 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_VLAN
);
3692 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)) {
3693 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
);
3696 fitness
= check_expectations(present_attrs
, out_of_range_attr
,
3697 expected_attrs
, key
, key_len
);
3700 * Remove the TPID from dl_type since it's not the real Ethertype. */
3701 flow
->dl_type
= htons(0);
3702 flow
->vlan_tci
= (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)
3703 ? nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
])
3706 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
))) {
3707 return ODP_FIT_TOO_LITTLE
;
3708 } else if (flow
->vlan_tci
== htons(0)) {
3709 /* Corner case for a truncated 802.1Q header. */
3710 if (fitness
== ODP_FIT_PERFECT
&& nl_attr_get_size(encap
)) {
3711 return ODP_FIT_TOO_MUCH
;
3714 } else if (!(flow
->vlan_tci
& htons(VLAN_CFI
))) {
3715 VLOG_ERR_RL(&rl
, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16
" is nonzero "
3716 "but CFI bit is not set", ntohs(flow
->vlan_tci
));
3717 return ODP_FIT_ERROR
;
3720 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
))) {
3725 /* Now parse the encapsulated attributes. */
3726 if (!parse_flow_nlattrs(nl_attr_get(encap
), nl_attr_get_size(encap
),
3727 attrs
, &present_attrs
, &out_of_range_attr
)) {
3728 return ODP_FIT_ERROR
;
3732 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
, src_flow
)) {
3733 return ODP_FIT_ERROR
;
3735 encap_fitness
= parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
3736 expected_attrs
, flow
, key
, key_len
,
3739 /* The overall fitness is the worse of the outer and inner attributes. */
3740 return MAX(fitness
, encap_fitness
);
3743 static enum odp_key_fitness
3744 odp_flow_key_to_flow__(const struct nlattr
*key
, size_t key_len
,
3745 struct flow
*flow
, const struct flow
*src_flow
)
3747 const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1];
3748 uint64_t expected_attrs
;
3749 uint64_t present_attrs
;
3750 int out_of_range_attr
;
3751 bool is_mask
= src_flow
!= flow
;
3753 memset(flow
, 0, sizeof *flow
);
3755 /* Parse attributes. */
3756 if (!parse_flow_nlattrs(key
, key_len
, attrs
, &present_attrs
,
3757 &out_of_range_attr
)) {
3758 return ODP_FIT_ERROR
;
3763 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID
)) {
3764 flow
->recirc_id
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_RECIRC_ID
]);
3765 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID
;
3766 } else if (is_mask
) {
3767 /* Always exact match recirc_id if it is not specified. */
3768 flow
->recirc_id
= UINT32_MAX
;
3771 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH
)) {
3772 flow
->dp_hash
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_DP_HASH
]);
3773 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH
;
3775 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
)) {
3776 flow
->skb_priority
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_PRIORITY
]);
3777 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
;
3780 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
)) {
3781 flow
->pkt_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_SKB_MARK
]);
3782 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
;
3785 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
)) {
3786 enum odp_key_fitness res
;
3788 res
= odp_tun_key_from_attr(attrs
[OVS_KEY_ATTR_TUNNEL
], &flow
->tunnel
);
3789 if (res
== ODP_FIT_ERROR
) {
3790 return ODP_FIT_ERROR
;
3791 } else if (res
== ODP_FIT_PERFECT
) {
3792 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
;
3796 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
)) {
3797 flow
->in_port
.odp_port
3798 = nl_attr_get_odp_port(attrs
[OVS_KEY_ATTR_IN_PORT
]);
3799 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
;
3800 } else if (!is_mask
) {
3801 flow
->in_port
.odp_port
= ODPP_NONE
;
3804 /* Ethernet header. */
3805 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
)) {
3806 const struct ovs_key_ethernet
*eth_key
;
3808 eth_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ETHERNET
]);
3809 put_ethernet_key(eth_key
, flow
);
3811 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
;
3815 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
;
3818 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
3819 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
,
3821 return ODP_FIT_ERROR
;
3825 ? (src_flow
->vlan_tci
& htons(VLAN_CFI
)) != 0
3826 : src_flow
->dl_type
== htons(ETH_TYPE_VLAN
)) {
3827 return parse_8021q_onward(attrs
, present_attrs
, out_of_range_attr
,
3828 expected_attrs
, flow
, key
, key_len
, src_flow
);
3831 flow
->vlan_tci
= htons(0xffff);
3832 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) {
3833 flow
->vlan_tci
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
]);
3834 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_VLAN
);
3837 return parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
3838 expected_attrs
, flow
, key
, key_len
, src_flow
);
3841 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
3842 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
3843 * 'key' fits our expectations for what a flow key should contain.
3845 * The 'in_port' will be the datapath's understanding of the port. The
3846 * caller will need to translate with odp_port_to_ofp_port() if the
3847 * OpenFlow port is needed.
3849 * This function doesn't take the packet itself as an argument because none of
3850 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
3851 * it is always possible to infer which additional attribute(s) should appear
3852 * by looking at the attributes for lower-level protocols, e.g. if the network
3853 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
3854 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
3855 * must be absent. */
3856 enum odp_key_fitness
3857 odp_flow_key_to_flow(const struct nlattr
*key
, size_t key_len
,
3860 return odp_flow_key_to_flow__(key
, key_len
, flow
, flow
);
3863 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a mask
3864 * structure in 'mask'. 'flow' must be a previously translated flow
3865 * corresponding to 'mask'. Returns an ODP_FIT_* value that indicates how well
3866 * 'key' fits our expectations for what a flow key should contain. */
3867 enum odp_key_fitness
3868 odp_flow_key_to_mask(const struct nlattr
*key
, size_t key_len
,
3869 struct flow
*mask
, const struct flow
*flow
)
3871 return odp_flow_key_to_flow__(key
, key_len
, mask
, flow
);
3874 /* Returns 'fitness' as a string, for use in debug messages. */
3876 odp_key_fitness_to_string(enum odp_key_fitness fitness
)
3879 case ODP_FIT_PERFECT
:
3881 case ODP_FIT_TOO_MUCH
:
3883 case ODP_FIT_TOO_LITTLE
:
3884 return "too_little";
3892 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
3893 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
3894 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
3895 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
3896 * null, then the return value is not meaningful.) */
3898 odp_put_userspace_action(uint32_t pid
,
3899 const void *userdata
, size_t userdata_size
,
3900 odp_port_t tunnel_out_port
,
3901 struct ofpbuf
*odp_actions
)
3903 size_t userdata_ofs
;
3906 offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_USERSPACE
);
3907 nl_msg_put_u32(odp_actions
, OVS_USERSPACE_ATTR_PID
, pid
);
3909 userdata_ofs
= odp_actions
->size
+ NLA_HDRLEN
;
3911 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
3912 * module before Linux 3.10 required the userdata to be exactly 8 bytes
3915 * - The kernel rejected shorter userdata with -ERANGE.
3917 * - The kernel silently dropped userdata beyond the first 8 bytes.
3919 * Thus, for maximum compatibility, always put at least 8 bytes. (We
3920 * separately disable features that required more than 8 bytes.) */
3921 memcpy(nl_msg_put_unspec_zero(odp_actions
, OVS_USERSPACE_ATTR_USERDATA
,
3922 MAX(8, userdata_size
)),
3923 userdata
, userdata_size
);
3927 if (tunnel_out_port
!= ODPP_NONE
) {
3928 nl_msg_put_odp_port(odp_actions
, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
,
3931 nl_msg_end_nested(odp_actions
, offset
);
3933 return userdata_ofs
;
3937 odp_put_tunnel_action(const struct flow_tnl
*tunnel
,
3938 struct ofpbuf
*odp_actions
)
3940 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
3941 tun_key_to_attr(odp_actions
, tunnel
);
3942 nl_msg_end_nested(odp_actions
, offset
);
3946 odp_put_tnl_push_action(struct ofpbuf
*odp_actions
,
3947 struct ovs_action_push_tnl
*data
)
3949 int size
= offsetof(struct ovs_action_push_tnl
, header
);
3951 size
+= data
->header_len
;
3952 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_TUNNEL_PUSH
, data
, size
);
3956 /* The commit_odp_actions() function and its helpers. */
3959 commit_set_action(struct ofpbuf
*odp_actions
, enum ovs_key_attr key_type
,
3960 const void *key
, size_t key_size
)
3962 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
3963 nl_msg_put_unspec(odp_actions
, key_type
, key
, key_size
);
3964 nl_msg_end_nested(odp_actions
, offset
);
3967 /* Masked set actions have a mask following the data within the netlink
3968 * attribute. The unmasked bits in the data will be cleared as the data
3969 * is copied to the action. */
3971 commit_masked_set_action(struct ofpbuf
*odp_actions
,
3972 enum ovs_key_attr key_type
,
3973 const void *key_
, const void *mask_
, size_t key_size
)
3975 size_t offset
= nl_msg_start_nested(odp_actions
,
3976 OVS_ACTION_ATTR_SET_MASKED
);
3977 char *data
= nl_msg_put_unspec_uninit(odp_actions
, key_type
, key_size
* 2);
3978 const char *key
= key_
, *mask
= mask_
;
3980 memcpy(data
+ key_size
, mask
, key_size
);
3981 /* Clear unmasked bits while copying. */
3982 while (key_size
--) {
3983 *data
++ = *key
++ & *mask
++;
3985 nl_msg_end_nested(odp_actions
, offset
);
3988 /* If any of the flow key data that ODP actions can modify are different in
3989 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
3990 * 'odp_actions' that change the flow tunneling information in key from
3991 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
3992 * same way. In other words, operates the same as commit_odp_actions(), but
3993 * only on tunneling information. */
3995 commit_odp_tunnel_action(const struct flow
*flow
, struct flow
*base
,
3996 struct ofpbuf
*odp_actions
)
3998 /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
3999 if (flow
->tunnel
.ip_dst
) {
4000 if (!memcmp(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
)) {
4003 memcpy(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
);
4004 odp_put_tunnel_action(&base
->tunnel
, odp_actions
);
4009 commit(enum ovs_key_attr attr
, bool use_masked_set
,
4010 const void *key
, void *base
, void *mask
, size_t size
,
4011 struct ofpbuf
*odp_actions
)
4013 if (memcmp(key
, base
, size
)) {
4014 bool fully_masked
= odp_mask_is_exact(attr
, mask
, size
);
4016 if (use_masked_set
&& !fully_masked
) {
4017 commit_masked_set_action(odp_actions
, attr
, key
, mask
, size
);
4019 if (!fully_masked
) {
4020 memset(mask
, 0xff, size
);
4022 commit_set_action(odp_actions
, attr
, key
, size
);
4024 memcpy(base
, key
, size
);
4027 /* Mask bits are set when we have either read or set the corresponding
4028 * values. Masked bits will be exact-matched, no need to set them
4029 * if the value did not actually change. */
4035 get_ethernet_key(const struct flow
*flow
, struct ovs_key_ethernet
*eth
)
4037 memcpy(eth
->eth_src
, flow
->dl_src
, ETH_ADDR_LEN
);
4038 memcpy(eth
->eth_dst
, flow
->dl_dst
, ETH_ADDR_LEN
);
4042 put_ethernet_key(const struct ovs_key_ethernet
*eth
, struct flow
*flow
)
4044 memcpy(flow
->dl_src
, eth
->eth_src
, ETH_ADDR_LEN
);
4045 memcpy(flow
->dl_dst
, eth
->eth_dst
, ETH_ADDR_LEN
);
4049 commit_set_ether_addr_action(const struct flow
*flow
, struct flow
*base_flow
,
4050 struct ofpbuf
*odp_actions
,
4051 struct flow_wildcards
*wc
,
4054 struct ovs_key_ethernet key
, base
, mask
;
4056 get_ethernet_key(flow
, &key
);
4057 get_ethernet_key(base_flow
, &base
);
4058 get_ethernet_key(&wc
->masks
, &mask
);
4060 if (commit(OVS_KEY_ATTR_ETHERNET
, use_masked
,
4061 &key
, &base
, &mask
, sizeof key
, odp_actions
)) {
4062 put_ethernet_key(&base
, base_flow
);
4063 put_ethernet_key(&mask
, &wc
->masks
);
4068 pop_vlan(struct flow
*base
,
4069 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
4071 memset(&wc
->masks
.vlan_tci
, 0xff, sizeof wc
->masks
.vlan_tci
);
4073 if (base
->vlan_tci
& htons(VLAN_CFI
)) {
4074 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_VLAN
);
4080 commit_vlan_action(ovs_be16 vlan_tci
, struct flow
*base
,
4081 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
4083 if (base
->vlan_tci
== vlan_tci
) {
4087 pop_vlan(base
, odp_actions
, wc
);
4088 if (vlan_tci
& htons(VLAN_CFI
)) {
4089 struct ovs_action_push_vlan vlan
;
4091 vlan
.vlan_tpid
= htons(ETH_TYPE_VLAN
);
4092 vlan
.vlan_tci
= vlan_tci
;
4093 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
4094 &vlan
, sizeof vlan
);
4096 base
->vlan_tci
= vlan_tci
;
4099 /* Wildcarding already done at action translation time. */
4101 commit_mpls_action(const struct flow
*flow
, struct flow
*base
,
4102 struct ofpbuf
*odp_actions
)
4104 int base_n
= flow_count_mpls_labels(base
, NULL
);
4105 int flow_n
= flow_count_mpls_labels(flow
, NULL
);
4106 int common_n
= flow_count_common_mpls_labels(flow
, flow_n
, base
, base_n
,
4109 while (base_n
> common_n
) {
4110 if (base_n
- 1 == common_n
&& flow_n
> common_n
) {
4111 /* If there is only one more LSE in base than there are common
4112 * between base and flow; and flow has at least one more LSE than
4113 * is common then the topmost LSE of base may be updated using
4115 struct ovs_key_mpls mpls_key
;
4117 mpls_key
.mpls_lse
= flow
->mpls_lse
[flow_n
- base_n
];
4118 commit_set_action(odp_actions
, OVS_KEY_ATTR_MPLS
,
4119 &mpls_key
, sizeof mpls_key
);
4120 flow_set_mpls_lse(base
, 0, mpls_key
.mpls_lse
);
4123 /* Otherwise, if there more LSEs in base than are common between
4124 * base and flow then pop the topmost one. */
4128 /* If all the LSEs are to be popped and this is not the outermost
4129 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
4130 * POP_MPLS action instead of flow->dl_type.
4132 * This is because the POP_MPLS action requires its ethertype
4133 * argument to be an MPLS ethernet type but in this case
4134 * flow->dl_type will be a non-MPLS ethernet type.
4136 * When the final POP_MPLS action occurs it use flow->dl_type and
4137 * the and the resulting packet will have the desired dl_type. */
4138 if ((!eth_type_mpls(flow
->dl_type
)) && base_n
> 1) {
4139 dl_type
= htons(ETH_TYPE_MPLS
);
4141 dl_type
= flow
->dl_type
;
4143 nl_msg_put_be16(odp_actions
, OVS_ACTION_ATTR_POP_MPLS
, dl_type
);
4144 popped
= flow_pop_mpls(base
, base_n
, flow
->dl_type
, NULL
);
4150 /* If, after the above popping and setting, there are more LSEs in flow
4151 * than base then some LSEs need to be pushed. */
4152 while (base_n
< flow_n
) {
4153 struct ovs_action_push_mpls
*mpls
;
4155 mpls
= nl_msg_put_unspec_zero(odp_actions
,
4156 OVS_ACTION_ATTR_PUSH_MPLS
,
4158 mpls
->mpls_ethertype
= flow
->dl_type
;
4159 mpls
->mpls_lse
= flow
->mpls_lse
[flow_n
- base_n
- 1];
4160 flow_push_mpls(base
, base_n
, mpls
->mpls_ethertype
, NULL
);
4161 flow_set_mpls_lse(base
, 0, mpls
->mpls_lse
);
4167 get_ipv4_key(const struct flow
*flow
, struct ovs_key_ipv4
*ipv4
, bool is_mask
)
4169 ipv4
->ipv4_src
= flow
->nw_src
;
4170 ipv4
->ipv4_dst
= flow
->nw_dst
;
4171 ipv4
->ipv4_proto
= flow
->nw_proto
;
4172 ipv4
->ipv4_tos
= flow
->nw_tos
;
4173 ipv4
->ipv4_ttl
= flow
->nw_ttl
;
4174 ipv4
->ipv4_frag
= ovs_to_odp_frag(flow
->nw_frag
, is_mask
);
4178 put_ipv4_key(const struct ovs_key_ipv4
*ipv4
, struct flow
*flow
, bool is_mask
)
4180 flow
->nw_src
= ipv4
->ipv4_src
;
4181 flow
->nw_dst
= ipv4
->ipv4_dst
;
4182 flow
->nw_proto
= ipv4
->ipv4_proto
;
4183 flow
->nw_tos
= ipv4
->ipv4_tos
;
4184 flow
->nw_ttl
= ipv4
->ipv4_ttl
;
4185 flow
->nw_frag
= odp_to_ovs_frag(ipv4
->ipv4_frag
, is_mask
);
4189 commit_set_ipv4_action(const struct flow
*flow
, struct flow
*base_flow
,
4190 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
4193 struct ovs_key_ipv4 key
, mask
, base
;
4195 /* Check that nw_proto and nw_frag remain unchanged. */
4196 ovs_assert(flow
->nw_proto
== base_flow
->nw_proto
&&
4197 flow
->nw_frag
== base_flow
->nw_frag
);
4199 get_ipv4_key(flow
, &key
, false);
4200 get_ipv4_key(base_flow
, &base
, false);
4201 get_ipv4_key(&wc
->masks
, &mask
, true);
4202 mask
.ipv4_proto
= 0; /* Not writeable. */
4203 mask
.ipv4_frag
= 0; /* Not writable. */
4205 if (commit(OVS_KEY_ATTR_IPV4
, use_masked
, &key
, &base
, &mask
, sizeof key
,
4207 put_ipv4_key(&base
, base_flow
, false);
4208 if (mask
.ipv4_proto
!= 0) { /* Mask was changed by commit(). */
4209 put_ipv4_key(&mask
, &wc
->masks
, true);
4215 get_ipv6_key(const struct flow
*flow
, struct ovs_key_ipv6
*ipv6
, bool is_mask
)
4217 memcpy(ipv6
->ipv6_src
, &flow
->ipv6_src
, sizeof ipv6
->ipv6_src
);
4218 memcpy(ipv6
->ipv6_dst
, &flow
->ipv6_dst
, sizeof ipv6
->ipv6_dst
);
4219 ipv6
->ipv6_label
= flow
->ipv6_label
;
4220 ipv6
->ipv6_proto
= flow
->nw_proto
;
4221 ipv6
->ipv6_tclass
= flow
->nw_tos
;
4222 ipv6
->ipv6_hlimit
= flow
->nw_ttl
;
4223 ipv6
->ipv6_frag
= ovs_to_odp_frag(flow
->nw_frag
, is_mask
);
4227 put_ipv6_key(const struct ovs_key_ipv6
*ipv6
, struct flow
*flow
, bool is_mask
)
4229 memcpy(&flow
->ipv6_src
, ipv6
->ipv6_src
, sizeof flow
->ipv6_src
);
4230 memcpy(&flow
->ipv6_dst
, ipv6
->ipv6_dst
, sizeof flow
->ipv6_dst
);
4231 flow
->ipv6_label
= ipv6
->ipv6_label
;
4232 flow
->nw_proto
= ipv6
->ipv6_proto
;
4233 flow
->nw_tos
= ipv6
->ipv6_tclass
;
4234 flow
->nw_ttl
= ipv6
->ipv6_hlimit
;
4235 flow
->nw_frag
= odp_to_ovs_frag(ipv6
->ipv6_frag
, is_mask
);
4239 commit_set_ipv6_action(const struct flow
*flow
, struct flow
*base_flow
,
4240 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
4243 struct ovs_key_ipv6 key
, mask
, base
;
4245 /* Check that nw_proto and nw_frag remain unchanged. */
4246 ovs_assert(flow
->nw_proto
== base_flow
->nw_proto
&&
4247 flow
->nw_frag
== base_flow
->nw_frag
);
4249 get_ipv6_key(flow
, &key
, false);
4250 get_ipv6_key(base_flow
, &base
, false);
4251 get_ipv6_key(&wc
->masks
, &mask
, true);
4252 mask
.ipv6_proto
= 0; /* Not writeable. */
4253 mask
.ipv6_frag
= 0; /* Not writable. */
4255 if (commit(OVS_KEY_ATTR_IPV6
, use_masked
, &key
, &base
, &mask
, sizeof key
,
4257 put_ipv6_key(&base
, base_flow
, false);
4258 if (mask
.ipv6_proto
!= 0) { /* Mask was changed by commit(). */
4259 put_ipv6_key(&mask
, &wc
->masks
, true);
4265 get_arp_key(const struct flow
*flow
, struct ovs_key_arp
*arp
)
4267 /* ARP key has padding, clear it. */
4268 memset(arp
, 0, sizeof *arp
);
4270 arp
->arp_sip
= flow
->nw_src
;
4271 arp
->arp_tip
= flow
->nw_dst
;
4272 arp
->arp_op
= htons(flow
->nw_proto
);
4273 memcpy(arp
->arp_sha
, flow
->arp_sha
, ETH_ADDR_LEN
);
4274 memcpy(arp
->arp_tha
, flow
->arp_tha
, ETH_ADDR_LEN
);
4278 put_arp_key(const struct ovs_key_arp
*arp
, struct flow
*flow
)
4280 flow
->nw_src
= arp
->arp_sip
;
4281 flow
->nw_dst
= arp
->arp_tip
;
4282 flow
->nw_proto
= ntohs(arp
->arp_op
);
4283 memcpy(flow
->arp_sha
, arp
->arp_sha
, ETH_ADDR_LEN
);
4284 memcpy(flow
->arp_tha
, arp
->arp_tha
, ETH_ADDR_LEN
);
4287 static enum slow_path_reason
4288 commit_set_arp_action(const struct flow
*flow
, struct flow
*base_flow
,
4289 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
4291 struct ovs_key_arp key
, mask
, base
;
4293 get_arp_key(flow
, &key
);
4294 get_arp_key(base_flow
, &base
);
4295 get_arp_key(&wc
->masks
, &mask
);
4297 if (commit(OVS_KEY_ATTR_ARP
, true, &key
, &base
, &mask
, sizeof key
,
4299 put_arp_key(&base
, base_flow
);
4300 put_arp_key(&mask
, &wc
->masks
);
4307 get_nd_key(const struct flow
*flow
, struct ovs_key_nd
*nd
)
4309 memcpy(nd
->nd_target
, &flow
->nd_target
, sizeof flow
->nd_target
);
4310 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
4311 memcpy(nd
->nd_sll
, flow
->arp_sha
, ETH_ADDR_LEN
);
4312 memcpy(nd
->nd_tll
, flow
->arp_tha
, ETH_ADDR_LEN
);
4316 put_nd_key(const struct ovs_key_nd
*nd
, struct flow
*flow
)
4318 memcpy(&flow
->nd_target
, &flow
->nd_target
, sizeof flow
->nd_target
);
4319 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
4320 memcpy(flow
->arp_sha
, nd
->nd_sll
, ETH_ADDR_LEN
);
4321 memcpy(flow
->arp_tha
, nd
->nd_tll
, ETH_ADDR_LEN
);
4324 static enum slow_path_reason
4325 commit_set_nd_action(const struct flow
*flow
, struct flow
*base_flow
,
4326 struct ofpbuf
*odp_actions
,
4327 struct flow_wildcards
*wc
, bool use_masked
)
4329 struct ovs_key_nd key
, mask
, base
;
4331 get_nd_key(flow
, &key
);
4332 get_nd_key(base_flow
, &base
);
4333 get_nd_key(&wc
->masks
, &mask
);
4335 if (commit(OVS_KEY_ATTR_ND
, use_masked
, &key
, &base
, &mask
, sizeof key
,
4337 put_nd_key(&base
, base_flow
);
4338 put_nd_key(&mask
, &wc
->masks
);
4345 static enum slow_path_reason
4346 commit_set_nw_action(const struct flow
*flow
, struct flow
*base
,
4347 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
4350 /* Check if 'flow' really has an L3 header. */
4351 if (!flow
->nw_proto
) {
4355 switch (ntohs(base
->dl_type
)) {
4357 commit_set_ipv4_action(flow
, base
, odp_actions
, wc
, use_masked
);
4361 commit_set_ipv6_action(flow
, base
, odp_actions
, wc
, use_masked
);
4362 return commit_set_nd_action(flow
, base
, odp_actions
, wc
, use_masked
);
4365 return commit_set_arp_action(flow
, base
, odp_actions
, wc
);
4371 /* TCP, UDP, and SCTP keys have the same layout. */
4372 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp
) == sizeof(struct ovs_key_udp
) &&
4373 sizeof(struct ovs_key_tcp
) == sizeof(struct ovs_key_sctp
));
4376 get_tp_key(const struct flow
*flow
, union ovs_key_tp
*tp
)
4378 tp
->tcp
.tcp_src
= flow
->tp_src
;
4379 tp
->tcp
.tcp_dst
= flow
->tp_dst
;
4383 put_tp_key(const union ovs_key_tp
*tp
, struct flow
*flow
)
4385 flow
->tp_src
= tp
->tcp
.tcp_src
;
4386 flow
->tp_dst
= tp
->tcp
.tcp_dst
;
4390 commit_set_port_action(const struct flow
*flow
, struct flow
*base_flow
,
4391 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
4394 enum ovs_key_attr key_type
;
4395 union ovs_key_tp key
, mask
, base
;
4397 /* Check if 'flow' really has an L3 header. */
4398 if (!flow
->nw_proto
) {
4402 if (!is_ip_any(base_flow
)) {
4406 if (flow
->nw_proto
== IPPROTO_TCP
) {
4407 key_type
= OVS_KEY_ATTR_TCP
;
4408 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
4409 key_type
= OVS_KEY_ATTR_UDP
;
4410 } else if (flow
->nw_proto
== IPPROTO_SCTP
) {
4411 key_type
= OVS_KEY_ATTR_SCTP
;
4416 get_tp_key(flow
, &key
);
4417 get_tp_key(base_flow
, &base
);
4418 get_tp_key(&wc
->masks
, &mask
);
4420 if (commit(key_type
, use_masked
, &key
, &base
, &mask
, sizeof key
,
4422 put_tp_key(&base
, base_flow
);
4423 put_tp_key(&mask
, &wc
->masks
);
4428 commit_set_priority_action(const struct flow
*flow
, struct flow
*base_flow
,
4429 struct ofpbuf
*odp_actions
,
4430 struct flow_wildcards
*wc
,
4433 uint32_t key
, mask
, base
;
4435 key
= flow
->skb_priority
;
4436 base
= base_flow
->skb_priority
;
4437 mask
= wc
->masks
.skb_priority
;
4439 if (commit(OVS_KEY_ATTR_PRIORITY
, use_masked
, &key
, &base
, &mask
,
4440 sizeof key
, odp_actions
)) {
4441 base_flow
->skb_priority
= base
;
4442 wc
->masks
.skb_priority
= mask
;
4447 commit_set_pkt_mark_action(const struct flow
*flow
, struct flow
*base_flow
,
4448 struct ofpbuf
*odp_actions
,
4449 struct flow_wildcards
*wc
,
4452 uint32_t key
, mask
, base
;
4454 key
= flow
->pkt_mark
;
4455 base
= base_flow
->pkt_mark
;
4456 mask
= wc
->masks
.pkt_mark
;
4458 if (commit(OVS_KEY_ATTR_SKB_MARK
, use_masked
, &key
, &base
, &mask
,
4459 sizeof key
, odp_actions
)) {
4460 base_flow
->pkt_mark
= base
;
4461 wc
->masks
.pkt_mark
= mask
;
4465 /* If any of the flow key data that ODP actions can modify are different in
4466 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
4467 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
4468 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
4469 * in addition to this function if needed. Sets fields in 'wc' that are
4470 * used as part of the action.
4472 * Returns a reason to force processing the flow's packets into the userspace
4473 * slow path, if there is one, otherwise 0. */
4474 enum slow_path_reason
4475 commit_odp_actions(const struct flow
*flow
, struct flow
*base
,
4476 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
4479 enum slow_path_reason slow
;
4481 commit_set_ether_addr_action(flow
, base
, odp_actions
, wc
, use_masked
);
4482 slow
= commit_set_nw_action(flow
, base
, odp_actions
, wc
, use_masked
);
4483 commit_set_port_action(flow
, base
, odp_actions
, wc
, use_masked
);
4484 commit_mpls_action(flow
, base
, odp_actions
);
4485 commit_vlan_action(flow
->vlan_tci
, base
, odp_actions
, wc
);
4486 commit_set_priority_action(flow
, base
, odp_actions
, wc
, use_masked
);
4487 commit_set_pkt_mark_action(flow
, base
, odp_actions
, wc
, use_masked
);