2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 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>
27 #include "byte-order.h"
29 #include "dynamic-string.h"
39 VLOG_DEFINE_THIS_MODULE(odp_util
);
41 /* The interface between userspace and kernel uses an "OVS_*" prefix.
42 * Since this is fairly non-specific for the OVS userspace components,
43 * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
44 * interactions with the datapath.
47 /* The set of characters that may separate one action or one key attribute
49 static const char *delimiters
= ", \t\r\n";
51 static int parse_odp_key_mask_attr(const char *, const struct simap
*port_names
,
52 struct ofpbuf
*, struct ofpbuf
*);
53 static void format_odp_key_attr(const struct nlattr
*a
,
54 const struct nlattr
*ma
, struct ds
*ds
);
56 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
59 * - For an action whose argument has a fixed length, returned that
60 * nonnegative length in bytes.
62 * - For an action with a variable-length argument, returns -2.
64 * - For an invalid 'type', returns -1. */
66 odp_action_len(uint16_t type
)
68 if (type
> OVS_ACTION_ATTR_MAX
) {
72 switch ((enum ovs_action_attr
) type
) {
73 case OVS_ACTION_ATTR_OUTPUT
: return sizeof(uint32_t);
74 case OVS_ACTION_ATTR_USERSPACE
: return -2;
75 case OVS_ACTION_ATTR_PUSH_VLAN
: return sizeof(struct ovs_action_push_vlan
);
76 case OVS_ACTION_ATTR_POP_VLAN
: return 0;
77 case OVS_ACTION_ATTR_PUSH_MPLS
: return sizeof(struct ovs_action_push_mpls
);
78 case OVS_ACTION_ATTR_POP_MPLS
: return sizeof(ovs_be16
);
79 case OVS_ACTION_ATTR_SET
: return -2;
80 case OVS_ACTION_ATTR_SAMPLE
: return -2;
82 case OVS_ACTION_ATTR_UNSPEC
:
83 case __OVS_ACTION_ATTR_MAX
:
90 /* Returns a string form of 'attr'. The return value is either a statically
91 * allocated constant string or the 'bufsize'-byte buffer 'namebuf'. 'bufsize'
92 * should be at least OVS_KEY_ATTR_BUFSIZE. */
93 enum { OVS_KEY_ATTR_BUFSIZE
= 3 + INT_STRLEN(unsigned int) + 1 };
95 ovs_key_attr_to_string(enum ovs_key_attr attr
, char *namebuf
, size_t bufsize
)
98 case OVS_KEY_ATTR_UNSPEC
: return "unspec";
99 case OVS_KEY_ATTR_ENCAP
: return "encap";
100 case OVS_KEY_ATTR_PRIORITY
: return "skb_priority";
101 case OVS_KEY_ATTR_SKB_MARK
: return "skb_mark";
102 case OVS_KEY_ATTR_TUNNEL
: return "tunnel";
103 case OVS_KEY_ATTR_IN_PORT
: return "in_port";
104 case OVS_KEY_ATTR_ETHERNET
: return "eth";
105 case OVS_KEY_ATTR_VLAN
: return "vlan";
106 case OVS_KEY_ATTR_ETHERTYPE
: return "eth_type";
107 case OVS_KEY_ATTR_IPV4
: return "ipv4";
108 case OVS_KEY_ATTR_IPV6
: return "ipv6";
109 case OVS_KEY_ATTR_TCP
: return "tcp";
110 case OVS_KEY_ATTR_UDP
: return "udp";
111 case OVS_KEY_ATTR_ICMP
: return "icmp";
112 case OVS_KEY_ATTR_ICMPV6
: return "icmpv6";
113 case OVS_KEY_ATTR_ARP
: return "arp";
114 case OVS_KEY_ATTR_ND
: return "nd";
115 case OVS_KEY_ATTR_MPLS
: return "mpls";
117 case __OVS_KEY_ATTR_MAX
:
119 snprintf(namebuf
, bufsize
, "key%u", (unsigned int) attr
);
125 format_generic_odp_action(struct ds
*ds
, const struct nlattr
*a
)
127 size_t len
= nl_attr_get_size(a
);
129 ds_put_format(ds
, "action%"PRId16
, nl_attr_type(a
));
131 const uint8_t *unspec
;
134 unspec
= nl_attr_get(a
);
135 for (i
= 0; i
< len
; i
++) {
136 ds_put_char(ds
, i
? ' ': '(');
137 ds_put_format(ds
, "%02x", unspec
[i
]);
139 ds_put_char(ds
, ')');
144 format_odp_sample_action(struct ds
*ds
, const struct nlattr
*attr
)
146 static const struct nl_policy ovs_sample_policy
[] = {
147 [OVS_SAMPLE_ATTR_PROBABILITY
] = { .type
= NL_A_U32
},
148 [OVS_SAMPLE_ATTR_ACTIONS
] = { .type
= NL_A_NESTED
}
150 struct nlattr
*a
[ARRAY_SIZE(ovs_sample_policy
)];
152 const struct nlattr
*nla_acts
;
155 ds_put_cstr(ds
, "sample");
157 if (!nl_parse_nested(attr
, ovs_sample_policy
, a
, ARRAY_SIZE(a
))) {
158 ds_put_cstr(ds
, "(error)");
162 percentage
= (100.0 * nl_attr_get_u32(a
[OVS_SAMPLE_ATTR_PROBABILITY
])) /
165 ds_put_format(ds
, "(sample=%.1f%%,", percentage
);
167 ds_put_cstr(ds
, "actions(");
168 nla_acts
= nl_attr_get(a
[OVS_SAMPLE_ATTR_ACTIONS
]);
169 len
= nl_attr_get_size(a
[OVS_SAMPLE_ATTR_ACTIONS
]);
170 format_odp_actions(ds
, nla_acts
, len
);
171 ds_put_format(ds
, "))");
175 slow_path_reason_to_string(enum slow_path_reason reason
)
186 case SLOW_CONTROLLER
:
194 static enum slow_path_reason
195 string_to_slow_path_reason(const char *string
)
197 enum slow_path_reason i
;
199 for (i
= 1; i
< __SLOW_MAX
; i
++) {
200 if (!strcmp(string
, slow_path_reason_to_string(i
))) {
209 parse_flags(const char *s
, const char *(*bit_to_string
)(uint32_t),
220 while (s
[n
] != ')') {
221 unsigned long long int flags
;
225 if (sscanf(&s
[n
], "%lli%n", &flags
, &n0
) > 0 && n0
> 0) {
226 n
+= n0
+ (s
[n
+ n0
] == ',');
231 for (bit
= 1; bit
; bit
<<= 1) {
232 const char *name
= bit_to_string(bit
);
240 if (!strncmp(s
+ n
, name
, len
) &&
241 (s
[n
+ len
] == ',' || s
[n
+ len
] == ')')) {
243 n
+= len
+ (s
[n
+ len
] == ',');
259 format_odp_userspace_action(struct ds
*ds
, const struct nlattr
*attr
)
261 static const struct nl_policy ovs_userspace_policy
[] = {
262 [OVS_USERSPACE_ATTR_PID
] = { .type
= NL_A_U32
},
263 [OVS_USERSPACE_ATTR_USERDATA
] = { .type
= NL_A_UNSPEC
,
266 struct nlattr
*a
[ARRAY_SIZE(ovs_userspace_policy
)];
267 const struct nlattr
*userdata_attr
;
269 if (!nl_parse_nested(attr
, ovs_userspace_policy
, a
, ARRAY_SIZE(a
))) {
270 ds_put_cstr(ds
, "userspace(error)");
274 ds_put_format(ds
, "userspace(pid=%"PRIu32
,
275 nl_attr_get_u32(a
[OVS_USERSPACE_ATTR_PID
]));
277 userdata_attr
= a
[OVS_USERSPACE_ATTR_USERDATA
];
280 const uint8_t *userdata
= nl_attr_get(userdata_attr
);
281 size_t userdata_len
= nl_attr_get_size(userdata_attr
);
282 bool userdata_unspec
= true;
283 union user_action_cookie cookie
;
285 if (userdata_len
>= sizeof cookie
.type
286 && userdata_len
<= sizeof cookie
) {
288 memset(&cookie
, 0, sizeof cookie
);
289 memcpy(&cookie
, userdata
, userdata_len
);
291 userdata_unspec
= false;
293 if (userdata_len
== sizeof cookie
.sflow
294 && cookie
.type
== USER_ACTION_COOKIE_SFLOW
) {
295 ds_put_format(ds
, ",sFlow("
296 "vid=%"PRIu16
",pcp=%"PRIu8
",output=%"PRIu32
")",
297 vlan_tci_to_vid(cookie
.sflow
.vlan_tci
),
298 vlan_tci_to_pcp(cookie
.sflow
.vlan_tci
),
299 cookie
.sflow
.output
);
300 } else if (userdata_len
== sizeof cookie
.slow_path
301 && cookie
.type
== USER_ACTION_COOKIE_SLOW_PATH
) {
303 reason
= slow_path_reason_to_string(cookie
.slow_path
.reason
);
304 reason
= reason
? reason
: "";
305 ds_put_format(ds
, ",slow_path(%s)", reason
);
306 } else if (userdata_len
== sizeof cookie
.flow_sample
307 && cookie
.type
== USER_ACTION_COOKIE_FLOW_SAMPLE
) {
308 ds_put_format(ds
, ",flow_sample(probability=%"PRIu16
309 ",collector_set_id=%"PRIu32
310 ",obs_domain_id=%"PRIu32
311 ",obs_point_id=%"PRIu32
")",
312 cookie
.flow_sample
.probability
,
313 cookie
.flow_sample
.collector_set_id
,
314 cookie
.flow_sample
.obs_domain_id
,
315 cookie
.flow_sample
.obs_point_id
);
316 } else if (userdata_len
== sizeof cookie
.ipfix
317 && cookie
.type
== USER_ACTION_COOKIE_IPFIX
) {
318 ds_put_format(ds
, ",ipfix");
320 userdata_unspec
= true;
324 if (userdata_unspec
) {
326 ds_put_format(ds
, ",userdata(");
327 for (i
= 0; i
< userdata_len
; i
++) {
328 ds_put_format(ds
, "%02x", userdata
[i
]);
330 ds_put_char(ds
, ')');
334 ds_put_char(ds
, ')');
338 format_vlan_tci(struct ds
*ds
, ovs_be16 vlan_tci
)
340 ds_put_format(ds
, "vid=%"PRIu16
",pcp=%d",
341 vlan_tci_to_vid(vlan_tci
),
342 vlan_tci_to_pcp(vlan_tci
));
343 if (!(vlan_tci
& htons(VLAN_CFI
))) {
344 ds_put_cstr(ds
, ",cfi=0");
349 format_mpls_lse(struct ds
*ds
, ovs_be32 mpls_lse
)
351 ds_put_format(ds
, "label=%"PRIu32
",tc=%d,ttl=%d,bos=%d",
352 mpls_lse_to_label(mpls_lse
),
353 mpls_lse_to_tc(mpls_lse
),
354 mpls_lse_to_ttl(mpls_lse
),
355 mpls_lse_to_bos(mpls_lse
));
359 format_mpls(struct ds
*ds
, const struct ovs_key_mpls
*mpls_key
,
360 const struct ovs_key_mpls
*mpls_mask
)
362 ovs_be32 key
= mpls_key
->mpls_lse
;
364 if (mpls_mask
== NULL
) {
365 format_mpls_lse(ds
, key
);
367 ovs_be32 mask
= mpls_mask
->mpls_lse
;
369 ds_put_format(ds
, "label=%"PRIu32
"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
370 mpls_lse_to_label(key
), mpls_lse_to_label(mask
),
371 mpls_lse_to_tc(key
), mpls_lse_to_tc(mask
),
372 mpls_lse_to_ttl(key
), mpls_lse_to_ttl(mask
),
373 mpls_lse_to_bos(key
), mpls_lse_to_bos(mask
));
378 format_odp_action(struct ds
*ds
, const struct nlattr
*a
)
381 enum ovs_action_attr type
= nl_attr_type(a
);
382 const struct ovs_action_push_vlan
*vlan
;
384 expected_len
= odp_action_len(nl_attr_type(a
));
385 if (expected_len
!= -2 && nl_attr_get_size(a
) != expected_len
) {
386 ds_put_format(ds
, "bad length %zu, expected %d for: ",
387 nl_attr_get_size(a
), expected_len
);
388 format_generic_odp_action(ds
, a
);
393 case OVS_ACTION_ATTR_OUTPUT
:
394 ds_put_format(ds
, "%"PRIu32
, nl_attr_get_u32(a
));
396 case OVS_ACTION_ATTR_USERSPACE
:
397 format_odp_userspace_action(ds
, a
);
399 case OVS_ACTION_ATTR_SET
:
400 ds_put_cstr(ds
, "set(");
401 format_odp_key_attr(nl_attr_get(a
), NULL
, ds
);
402 ds_put_cstr(ds
, ")");
404 case OVS_ACTION_ATTR_PUSH_VLAN
:
405 vlan
= nl_attr_get(a
);
406 ds_put_cstr(ds
, "push_vlan(");
407 if (vlan
->vlan_tpid
!= htons(ETH_TYPE_VLAN
)) {
408 ds_put_format(ds
, "tpid=0x%04"PRIx16
",", ntohs(vlan
->vlan_tpid
));
410 format_vlan_tci(ds
, vlan
->vlan_tci
);
411 ds_put_char(ds
, ')');
413 case OVS_ACTION_ATTR_POP_VLAN
:
414 ds_put_cstr(ds
, "pop_vlan");
416 case OVS_ACTION_ATTR_PUSH_MPLS
: {
417 const struct ovs_action_push_mpls
*mpls
= nl_attr_get(a
);
418 ds_put_cstr(ds
, "push_mpls(");
419 format_mpls_lse(ds
, mpls
->mpls_lse
);
420 ds_put_format(ds
, ",eth_type=0x%"PRIx16
")", ntohs(mpls
->mpls_ethertype
));
423 case OVS_ACTION_ATTR_POP_MPLS
: {
424 ovs_be16 ethertype
= nl_attr_get_be16(a
);
425 ds_put_format(ds
, "pop_mpls(eth_type=0x%"PRIx16
")", ntohs(ethertype
));
428 case OVS_ACTION_ATTR_SAMPLE
:
429 format_odp_sample_action(ds
, a
);
431 case OVS_ACTION_ATTR_UNSPEC
:
432 case __OVS_ACTION_ATTR_MAX
:
434 format_generic_odp_action(ds
, a
);
440 format_odp_actions(struct ds
*ds
, const struct nlattr
*actions
,
444 const struct nlattr
*a
;
447 NL_ATTR_FOR_EACH (a
, left
, actions
, actions_len
) {
449 ds_put_char(ds
, ',');
451 format_odp_action(ds
, a
);
456 if (left
== actions_len
) {
457 ds_put_cstr(ds
, "<empty>");
459 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
460 for (i
= 0; i
< left
; i
++) {
461 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
463 ds_put_char(ds
, ')');
466 ds_put_cstr(ds
, "drop");
471 parse_odp_action(const char *s
, const struct simap
*port_names
,
472 struct ofpbuf
*actions
)
474 /* Many of the sscanf calls in this function use oversized destination
475 * fields because some sscanf() implementations truncate the range of %i
476 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
477 * value of 0x7fff. The other alternatives are to allow only a single
478 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
481 * The tun_id parser has to use an alternative approach because there is no
482 * type larger than 64 bits. */
485 unsigned long long int port
;
488 if (sscanf(s
, "%lli%n", &port
, &n
) > 0 && n
> 0) {
489 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, port
);
495 int len
= strcspn(s
, delimiters
);
496 struct simap_node
*node
;
498 node
= simap_find_len(port_names
, s
, len
);
500 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, node
->data
);
506 unsigned long long int pid
;
507 unsigned long long int output
;
508 unsigned long long int probability
;
509 unsigned long long int collector_set_id
;
510 unsigned long long int obs_domain_id
;
511 unsigned long long int obs_point_id
;
515 if (sscanf(s
, "userspace(pid=%lli)%n", &pid
, &n
) > 0 && n
> 0) {
516 odp_put_userspace_action(pid
, NULL
, 0, actions
);
518 } else if (sscanf(s
, "userspace(pid=%lli,sFlow(vid=%i,"
519 "pcp=%i,output=%lli))%n",
520 &pid
, &vid
, &pcp
, &output
, &n
) > 0 && n
> 0) {
521 union user_action_cookie cookie
;
524 tci
= vid
| (pcp
<< VLAN_PCP_SHIFT
);
529 cookie
.type
= USER_ACTION_COOKIE_SFLOW
;
530 cookie
.sflow
.vlan_tci
= htons(tci
);
531 cookie
.sflow
.output
= output
;
532 odp_put_userspace_action(pid
, &cookie
, sizeof cookie
.sflow
,
535 } else if (sscanf(s
, "userspace(pid=%lli,slow_path(%n", &pid
, &n
) > 0
537 union user_action_cookie cookie
;
540 if (s
[n
] == ')' && s
[n
+ 1] == ')') {
543 } else if (sscanf(s
+ n
, "%31[^)]))", reason
) > 0) {
544 n
+= strlen(reason
) + 2;
549 cookie
.type
= USER_ACTION_COOKIE_SLOW_PATH
;
550 cookie
.slow_path
.unused
= 0;
551 cookie
.slow_path
.reason
= string_to_slow_path_reason(reason
);
553 if (reason
[0] && !cookie
.slow_path
.reason
) {
557 odp_put_userspace_action(pid
, &cookie
, sizeof cookie
.slow_path
,
560 } else if (sscanf(s
, "userspace(pid=%lli,flow_sample(probability=%lli,"
561 "collector_set_id=%lli,obs_domain_id=%lli,"
562 "obs_point_id=%lli))%n",
563 &pid
, &probability
, &collector_set_id
,
564 &obs_domain_id
, &obs_point_id
, &n
) > 0 && n
> 0) {
565 union user_action_cookie cookie
;
567 cookie
.type
= USER_ACTION_COOKIE_FLOW_SAMPLE
;
568 cookie
.flow_sample
.probability
= probability
;
569 cookie
.flow_sample
.collector_set_id
= collector_set_id
;
570 cookie
.flow_sample
.obs_domain_id
= obs_domain_id
;
571 cookie
.flow_sample
.obs_point_id
= obs_point_id
;
572 odp_put_userspace_action(pid
, &cookie
, sizeof cookie
.flow_sample
,
575 } else if (sscanf(s
, "userspace(pid=%lli,ipfix)%n", &pid
, &n
) > 0
577 union user_action_cookie cookie
;
579 cookie
.type
= USER_ACTION_COOKIE_IPFIX
;
580 odp_put_userspace_action(pid
, &cookie
, sizeof cookie
.ipfix
,
583 } else if (sscanf(s
, "userspace(pid=%lli,userdata(%n", &pid
, &n
) > 0
588 ofpbuf_init(&buf
, 16);
589 end
= ofpbuf_put_hex(&buf
, &s
[n
], NULL
);
590 if (end
[0] == ')' && end
[1] == ')') {
591 odp_put_userspace_action(pid
, buf
.data
, buf
.size
, actions
);
593 return (end
+ 2) - s
;
598 if (!strncmp(s
, "set(", 4)) {
602 start_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SET
);
603 retval
= parse_odp_key_mask_attr(s
+ 4, port_names
, actions
, NULL
);
607 if (s
[retval
+ 4] != ')') {
610 nl_msg_end_nested(actions
, start_ofs
);
615 struct ovs_action_push_vlan push
;
616 int tpid
= ETH_TYPE_VLAN
;
621 if ((sscanf(s
, "push_vlan(vid=%i,pcp=%i)%n", &vid
, &pcp
, &n
) > 0
623 || (sscanf(s
, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
624 &vid
, &pcp
, &cfi
, &n
) > 0 && n
> 0)
625 || (sscanf(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
626 &tpid
, &vid
, &pcp
, &n
) > 0 && n
> 0)
627 || (sscanf(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
628 &tpid
, &vid
, &pcp
, &cfi
, &n
) > 0 && n
> 0)) {
629 push
.vlan_tpid
= htons(tpid
);
630 push
.vlan_tci
= htons((vid
<< VLAN_VID_SHIFT
)
631 | (pcp
<< VLAN_PCP_SHIFT
)
632 | (cfi
? VLAN_CFI
: 0));
633 nl_msg_put_unspec(actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
640 if (!strncmp(s
, "pop_vlan", 8)) {
641 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_VLAN
);
649 if (sscanf(s
, "sample(sample=%lf%%,actions(%n", &percentage
, &n
) > 0
650 && percentage
>= 0. && percentage
<= 100.0
652 size_t sample_ofs
, actions_ofs
;
655 probability
= floor(UINT32_MAX
* (percentage
/ 100.0) + .5);
656 sample_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SAMPLE
);
657 nl_msg_put_u32(actions
, OVS_SAMPLE_ATTR_PROBABILITY
,
658 (probability
<= 0 ? 0
659 : probability
>= UINT32_MAX
? UINT32_MAX
662 actions_ofs
= nl_msg_start_nested(actions
,
663 OVS_SAMPLE_ATTR_ACTIONS
);
667 n
+= strspn(s
+ n
, delimiters
);
672 retval
= parse_odp_action(s
+ n
, port_names
, actions
);
678 nl_msg_end_nested(actions
, actions_ofs
);
679 nl_msg_end_nested(actions
, sample_ofs
);
681 return s
[n
+ 1] == ')' ? n
+ 2 : -EINVAL
;
688 /* Parses the string representation of datapath actions, in the format output
689 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
690 * value. On success, the ODP actions are appended to 'actions' as a series of
691 * Netlink attributes. On failure, no data is appended to 'actions'. Either
692 * way, 'actions''s data might be reallocated. */
694 odp_actions_from_string(const char *s
, const struct simap
*port_names
,
695 struct ofpbuf
*actions
)
699 if (!strcasecmp(s
, "drop")) {
703 old_size
= actions
->size
;
707 s
+= strspn(s
, delimiters
);
712 retval
= parse_odp_action(s
, port_names
, actions
);
713 if (retval
< 0 || !strchr(delimiters
, s
[retval
])) {
714 actions
->size
= old_size
;
723 /* Returns the correct length of the payload for a flow key attribute of the
724 * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
725 * is variable length. */
727 odp_flow_key_attr_len(uint16_t type
)
729 if (type
> OVS_KEY_ATTR_MAX
) {
733 switch ((enum ovs_key_attr
) type
) {
734 case OVS_KEY_ATTR_ENCAP
: return -2;
735 case OVS_KEY_ATTR_PRIORITY
: return 4;
736 case OVS_KEY_ATTR_SKB_MARK
: return 4;
737 case OVS_KEY_ATTR_TUNNEL
: return -2;
738 case OVS_KEY_ATTR_IN_PORT
: return 4;
739 case OVS_KEY_ATTR_ETHERNET
: return sizeof(struct ovs_key_ethernet
);
740 case OVS_KEY_ATTR_VLAN
: return sizeof(ovs_be16
);
741 case OVS_KEY_ATTR_ETHERTYPE
: return 2;
742 case OVS_KEY_ATTR_MPLS
: return sizeof(struct ovs_key_mpls
);
743 case OVS_KEY_ATTR_IPV4
: return sizeof(struct ovs_key_ipv4
);
744 case OVS_KEY_ATTR_IPV6
: return sizeof(struct ovs_key_ipv6
);
745 case OVS_KEY_ATTR_TCP
: return sizeof(struct ovs_key_tcp
);
746 case OVS_KEY_ATTR_UDP
: return sizeof(struct ovs_key_udp
);
747 case OVS_KEY_ATTR_ICMP
: return sizeof(struct ovs_key_icmp
);
748 case OVS_KEY_ATTR_ICMPV6
: return sizeof(struct ovs_key_icmpv6
);
749 case OVS_KEY_ATTR_ARP
: return sizeof(struct ovs_key_arp
);
750 case OVS_KEY_ATTR_ND
: return sizeof(struct ovs_key_nd
);
752 case OVS_KEY_ATTR_UNSPEC
:
753 case __OVS_KEY_ATTR_MAX
:
761 format_generic_odp_key(const struct nlattr
*a
, struct ds
*ds
)
763 size_t len
= nl_attr_get_size(a
);
765 const uint8_t *unspec
;
768 unspec
= nl_attr_get(a
);
769 for (i
= 0; i
< len
; i
++) {
771 ds_put_char(ds
, ' ');
773 ds_put_format(ds
, "%02x", unspec
[i
]);
779 ovs_frag_type_to_string(enum ovs_frag_type type
)
782 case OVS_FRAG_TYPE_NONE
:
784 case OVS_FRAG_TYPE_FIRST
:
786 case OVS_FRAG_TYPE_LATER
:
788 case __OVS_FRAG_TYPE_MAX
:
795 tunnel_key_attr_len(int type
)
798 case OVS_TUNNEL_KEY_ATTR_ID
: return 8;
799 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
: return 4;
800 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
: return 4;
801 case OVS_TUNNEL_KEY_ATTR_TOS
: return 1;
802 case OVS_TUNNEL_KEY_ATTR_TTL
: return 1;
803 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
: return 0;
804 case OVS_TUNNEL_KEY_ATTR_CSUM
: return 0;
805 case __OVS_TUNNEL_KEY_ATTR_MAX
:
812 odp_tun_key_from_attr(const struct nlattr
*attr
, struct flow_tnl
*tun
)
815 const struct nlattr
*a
;
817 bool unknown
= false;
819 NL_NESTED_FOR_EACH(a
, left
, attr
) {
820 uint16_t type
= nl_attr_type(a
);
821 size_t len
= nl_attr_get_size(a
);
822 int expected_len
= tunnel_key_attr_len(type
);
824 if (len
!= expected_len
&& expected_len
>= 0) {
825 return ODP_FIT_ERROR
;
829 case OVS_TUNNEL_KEY_ATTR_ID
:
830 tun
->tun_id
= nl_attr_get_be64(a
);
831 tun
->flags
|= FLOW_TNL_F_KEY
;
833 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
834 tun
->ip_src
= nl_attr_get_be32(a
);
836 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
837 tun
->ip_dst
= nl_attr_get_be32(a
);
839 case OVS_TUNNEL_KEY_ATTR_TOS
:
840 tun
->ip_tos
= nl_attr_get_u8(a
);
842 case OVS_TUNNEL_KEY_ATTR_TTL
:
843 tun
->ip_ttl
= nl_attr_get_u8(a
);
846 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
847 tun
->flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
849 case OVS_TUNNEL_KEY_ATTR_CSUM
:
850 tun
->flags
|= FLOW_TNL_F_CSUM
;
853 /* Allow this to show up as unexpected, if there are unknown
854 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
861 return ODP_FIT_ERROR
;
864 return ODP_FIT_TOO_MUCH
;
866 return ODP_FIT_PERFECT
;
870 tun_key_to_attr(struct ofpbuf
*a
, const struct flow_tnl
*tun_key
)
874 tun_key_ofs
= nl_msg_start_nested(a
, OVS_KEY_ATTR_TUNNEL
);
876 if (tun_key
->flags
& FLOW_TNL_F_KEY
) {
877 nl_msg_put_be64(a
, OVS_TUNNEL_KEY_ATTR_ID
, tun_key
->tun_id
);
879 if (tun_key
->ip_src
) {
880 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
, tun_key
->ip_src
);
882 if (tun_key
->ip_dst
) {
883 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
, tun_key
->ip_dst
);
885 if (tun_key
->ip_tos
) {
886 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TOS
, tun_key
->ip_tos
);
888 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TTL
, tun_key
->ip_ttl
);
889 if (tun_key
->flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
890 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
892 if (tun_key
->flags
& FLOW_TNL_F_CSUM
) {
893 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
896 nl_msg_end_nested(a
, tun_key_ofs
);
900 odp_mask_attr_is_exact(const struct nlattr
*ma
)
902 bool is_exact
= false;
903 enum ovs_key_attr attr
= nl_attr_type(ma
);
905 if (attr
== OVS_KEY_ATTR_TUNNEL
) {
906 /* XXX this is a hack for now. Should change
907 * the exact match dection to per field
908 * instead of per attribute.
910 struct flow_tnl tun_mask
;
911 memset(&tun_mask
, 0, sizeof tun_mask
);
912 odp_tun_key_from_attr(ma
, &tun_mask
);
913 if (tun_mask
.flags
== (FLOW_TNL_F_KEY
914 | FLOW_TNL_F_DONT_FRAGMENT
915 | FLOW_TNL_F_CSUM
)) {
916 /* The flags are exact match, check the remaining fields. */
917 tun_mask
.flags
= 0xffff;
918 is_exact
= is_all_ones((uint8_t *)&tun_mask
,
919 offsetof(struct flow_tnl
, ip_ttl
));
922 is_exact
= is_all_ones(nl_attr_get(ma
), nl_attr_get_size(ma
));
930 format_odp_key_attr(const struct nlattr
*a
, const struct nlattr
*ma
,
933 struct flow_tnl tun_key
;
934 enum ovs_key_attr attr
= nl_attr_type(a
);
935 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
939 is_exact
= ma
? odp_mask_attr_is_exact(ma
) : true;
941 ds_put_cstr(ds
, ovs_key_attr_to_string(attr
, namebuf
, sizeof namebuf
));
944 expected_len
= odp_flow_key_attr_len(nl_attr_type(a
));
945 if (expected_len
!= -2) {
946 bool bad_key_len
= nl_attr_get_size(a
) != expected_len
;
947 bool bad_mask_len
= ma
&& nl_attr_get_size(a
) != expected_len
;
949 if (bad_key_len
|| bad_mask_len
) {
951 ds_put_format(ds
, "(bad key length %zu, expected %d)(",
953 odp_flow_key_attr_len(nl_attr_type(a
)));
955 format_generic_odp_key(a
, ds
);
957 ds_put_char(ds
, '/');
958 ds_put_format(ds
, "(bad mask length %zu, expected %d)(",
959 nl_attr_get_size(ma
),
960 odp_flow_key_attr_len(nl_attr_type(ma
)));
962 format_generic_odp_key(ma
, ds
);
963 ds_put_char(ds
, ')');
969 ds_put_char(ds
, '(');
971 case OVS_KEY_ATTR_ENCAP
:
972 if (ma
&& nl_attr_get_size(ma
) && nl_attr_get_size(a
)) {
973 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
),
974 nl_attr_get(ma
), nl_attr_get_size(ma
), ds
);
975 } else if (nl_attr_get_size(a
)) {
976 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
), NULL
, 0, ds
);
980 case OVS_KEY_ATTR_PRIORITY
:
981 case OVS_KEY_ATTR_SKB_MARK
:
982 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
984 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
988 case OVS_KEY_ATTR_TUNNEL
:
989 memset(&tun_key
, 0, sizeof tun_key
);
990 if (odp_tun_key_from_attr(a
, &tun_key
) == ODP_FIT_ERROR
) {
991 ds_put_format(ds
, "error");
992 } else if (!is_exact
) {
993 struct flow_tnl tun_mask
;
995 memset(&tun_mask
, 0, sizeof tun_mask
);
996 odp_tun_key_from_attr(ma
, &tun_mask
);
997 ds_put_format(ds
, "tun_id=%#"PRIx64
"/%#"PRIx64
998 ",src="IP_FMT
"/"IP_FMT
",dst="IP_FMT
"/"IP_FMT
999 ",tos=%#"PRIx8
"/%#"PRIx8
",ttl=%"PRIu8
"/%#"PRIx8
1001 ntohll(tun_key
.tun_id
), ntohll(tun_mask
.tun_id
),
1002 IP_ARGS(tun_key
.ip_src
), IP_ARGS(tun_mask
.ip_src
),
1003 IP_ARGS(tun_key
.ip_dst
), IP_ARGS(tun_mask
.ip_dst
),
1004 tun_key
.ip_tos
, tun_mask
.ip_tos
,
1005 tun_key
.ip_ttl
, tun_mask
.ip_ttl
);
1007 format_flags(ds
, flow_tun_flag_to_string
, tun_key
.flags
, ',');
1009 /* XXX This code is correct, but enabling it would break the unit
1010 test. Disable it for now until the input parser is fixed.
1012 ds_put_char(ds, '/');
1013 format_flags(ds, flow_tun_flag_to_string, tun_mask.flags, ',');
1015 ds_put_char(ds
, ')');
1017 ds_put_format(ds
, "tun_id=0x%"PRIx64
",src="IP_FMT
",dst="IP_FMT
","
1018 "tos=0x%"PRIx8
",ttl=%"PRIu8
",flags(",
1019 ntohll(tun_key
.tun_id
),
1020 IP_ARGS(tun_key
.ip_src
),
1021 IP_ARGS(tun_key
.ip_dst
),
1022 tun_key
.ip_tos
, tun_key
.ip_ttl
);
1024 format_flags(ds
, flow_tun_flag_to_string
, tun_key
.flags
, ',');
1025 ds_put_char(ds
, ')');
1029 case OVS_KEY_ATTR_IN_PORT
:
1030 ds_put_format(ds
, "%"PRIu32
, nl_attr_get_u32(a
));
1032 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
1036 case OVS_KEY_ATTR_ETHERNET
:
1038 const struct ovs_key_ethernet
*eth_mask
= nl_attr_get(ma
);
1039 const struct ovs_key_ethernet
*eth_key
= nl_attr_get(a
);
1041 ds_put_format(ds
, "src="ETH_ADDR_FMT
"/"ETH_ADDR_FMT
1042 ",dst="ETH_ADDR_FMT
"/"ETH_ADDR_FMT
,
1043 ETH_ADDR_ARGS(eth_key
->eth_src
),
1044 ETH_ADDR_ARGS(eth_mask
->eth_src
),
1045 ETH_ADDR_ARGS(eth_key
->eth_dst
),
1046 ETH_ADDR_ARGS(eth_mask
->eth_dst
));
1048 const struct ovs_key_ethernet
*eth_key
= nl_attr_get(a
);
1050 ds_put_format(ds
, "src="ETH_ADDR_FMT
",dst="ETH_ADDR_FMT
,
1051 ETH_ADDR_ARGS(eth_key
->eth_src
),
1052 ETH_ADDR_ARGS(eth_key
->eth_dst
));
1056 case OVS_KEY_ATTR_VLAN
:
1058 ovs_be16 vlan_tci
= nl_attr_get_be16(a
);
1060 ovs_be16 mask
= nl_attr_get_be16(ma
);
1061 ds_put_format(ds
, "vid=%"PRIu16
"/%"PRIx16
",pcp=%d/0x%x,cfi=%d/%d",
1062 vlan_tci_to_vid(vlan_tci
),
1063 vlan_tci_to_vid(mask
),
1064 vlan_tci_to_pcp(vlan_tci
),
1065 vlan_tci_to_pcp(mask
),
1066 vlan_tci_to_cfi(vlan_tci
),
1067 vlan_tci_to_cfi(mask
));
1069 format_vlan_tci(ds
, vlan_tci
);
1074 case OVS_KEY_ATTR_MPLS
: {
1075 const struct ovs_key_mpls
*mpls_key
= nl_attr_get(a
);
1076 const struct ovs_key_mpls
*mpls_mask
= NULL
;
1078 mpls_mask
= nl_attr_get(ma
);
1080 format_mpls(ds
, mpls_key
, mpls_mask
);
1084 case OVS_KEY_ATTR_ETHERTYPE
:
1085 ds_put_format(ds
, "0x%04"PRIx16
, ntohs(nl_attr_get_be16(a
)));
1087 ds_put_format(ds
, "/0x%04"PRIx16
, ntohs(nl_attr_get_be16(ma
)));
1091 case OVS_KEY_ATTR_IPV4
:
1093 const struct ovs_key_ipv4
*ipv4_key
= nl_attr_get(a
);
1094 const struct ovs_key_ipv4
*ipv4_mask
= nl_attr_get(ma
);
1096 ds_put_format(ds
, "src="IP_FMT
"/"IP_FMT
",dst="IP_FMT
"/"IP_FMT
1097 ",proto=%"PRIu8
"/%#"PRIx8
",tos=%#"PRIx8
"/%#"PRIx8
1098 ",ttl=%"PRIu8
"/%#"PRIx8
",frag=%s/%#"PRIx8
,
1099 IP_ARGS(ipv4_key
->ipv4_src
),
1100 IP_ARGS(ipv4_mask
->ipv4_src
),
1101 IP_ARGS(ipv4_key
->ipv4_dst
),
1102 IP_ARGS(ipv4_mask
->ipv4_dst
),
1103 ipv4_key
->ipv4_proto
, ipv4_mask
->ipv4_proto
,
1104 ipv4_key
->ipv4_tos
, ipv4_mask
->ipv4_tos
,
1105 ipv4_key
->ipv4_ttl
, ipv4_mask
->ipv4_ttl
,
1106 ovs_frag_type_to_string(ipv4_key
->ipv4_frag
),
1107 ipv4_mask
->ipv4_frag
);
1109 const struct ovs_key_ipv4
*ipv4_key
= nl_attr_get(a
);
1111 ds_put_format(ds
, "src="IP_FMT
",dst="IP_FMT
",proto=%"PRIu8
1112 ",tos=%#"PRIx8
",ttl=%"PRIu8
",frag=%s",
1113 IP_ARGS(ipv4_key
->ipv4_src
),
1114 IP_ARGS(ipv4_key
->ipv4_dst
),
1115 ipv4_key
->ipv4_proto
, ipv4_key
->ipv4_tos
,
1117 ovs_frag_type_to_string(ipv4_key
->ipv4_frag
));
1121 case OVS_KEY_ATTR_IPV6
:
1123 const struct ovs_key_ipv6
*ipv6_key
, *ipv6_mask
;
1124 char src_str
[INET6_ADDRSTRLEN
];
1125 char dst_str
[INET6_ADDRSTRLEN
];
1126 char src_mask
[INET6_ADDRSTRLEN
];
1127 char dst_mask
[INET6_ADDRSTRLEN
];
1129 ipv6_key
= nl_attr_get(a
);
1130 inet_ntop(AF_INET6
, ipv6_key
->ipv6_src
, src_str
, sizeof src_str
);
1131 inet_ntop(AF_INET6
, ipv6_key
->ipv6_dst
, dst_str
, sizeof dst_str
);
1133 ipv6_mask
= nl_attr_get(ma
);
1134 inet_ntop(AF_INET6
, ipv6_mask
->ipv6_src
, src_mask
, sizeof src_mask
);
1135 inet_ntop(AF_INET6
, ipv6_mask
->ipv6_dst
, dst_mask
, sizeof dst_mask
);
1137 ds_put_format(ds
, "src=%s/%s,dst=%s/%s,label=%#"PRIx32
"/%#"PRIx32
1138 ",proto=%"PRIu8
"/%#"PRIx8
",tclass=%#"PRIx8
"/%#"PRIx8
1139 ",hlimit=%"PRIu8
"/%#"PRIx8
",frag=%s/%#"PRIx8
,
1140 src_str
, src_mask
, dst_str
, dst_mask
,
1141 ntohl(ipv6_key
->ipv6_label
),
1142 ntohl(ipv6_mask
->ipv6_label
),
1143 ipv6_key
->ipv6_proto
, ipv6_mask
->ipv6_proto
,
1144 ipv6_key
->ipv6_tclass
, ipv6_mask
->ipv6_tclass
,
1145 ipv6_key
->ipv6_hlimit
, ipv6_mask
->ipv6_hlimit
,
1146 ovs_frag_type_to_string(ipv6_key
->ipv6_frag
),
1147 ipv6_mask
->ipv6_frag
);
1149 const struct ovs_key_ipv6
*ipv6_key
;
1150 char src_str
[INET6_ADDRSTRLEN
];
1151 char dst_str
[INET6_ADDRSTRLEN
];
1153 ipv6_key
= nl_attr_get(a
);
1154 inet_ntop(AF_INET6
, ipv6_key
->ipv6_src
, src_str
, sizeof src_str
);
1155 inet_ntop(AF_INET6
, ipv6_key
->ipv6_dst
, dst_str
, sizeof dst_str
);
1157 ds_put_format(ds
, "src=%s,dst=%s,label=%#"PRIx32
",proto=%"PRIu8
1158 ",tclass=%#"PRIx8
",hlimit=%"PRIu8
",frag=%s",
1159 src_str
, dst_str
, ntohl(ipv6_key
->ipv6_label
),
1160 ipv6_key
->ipv6_proto
, ipv6_key
->ipv6_tclass
,
1161 ipv6_key
->ipv6_hlimit
,
1162 ovs_frag_type_to_string(ipv6_key
->ipv6_frag
));
1166 case OVS_KEY_ATTR_TCP
:
1168 const struct ovs_key_tcp
*tcp_mask
= nl_attr_get(ma
);
1169 const struct ovs_key_tcp
*tcp_key
= nl_attr_get(a
);
1171 ds_put_format(ds
, "src=%"PRIu16
"/%#"PRIx16
1172 ",dst=%"PRIu16
"/%#"PRIx16
,
1173 ntohs(tcp_key
->tcp_src
), ntohs(tcp_mask
->tcp_src
),
1174 ntohs(tcp_key
->tcp_dst
), ntohs(tcp_mask
->tcp_dst
));
1176 const struct ovs_key_tcp
*tcp_key
= nl_attr_get(a
);
1178 ds_put_format(ds
, "src=%"PRIu16
",dst=%"PRIu16
,
1179 ntohs(tcp_key
->tcp_src
), ntohs(tcp_key
->tcp_dst
));
1183 case OVS_KEY_ATTR_UDP
:
1185 const struct ovs_key_udp
*udp_mask
= nl_attr_get(ma
);
1186 const struct ovs_key_udp
*udp_key
= nl_attr_get(a
);
1188 ds_put_format(ds
, "src=%"PRIu16
"/%#"PRIx16
1189 ",dst=%"PRIu16
"/%#"PRIx16
,
1190 ntohs(udp_key
->udp_src
), ntohs(udp_mask
->udp_src
),
1191 ntohs(udp_key
->udp_dst
), ntohs(udp_mask
->udp_dst
));
1193 const struct ovs_key_udp
*udp_key
= nl_attr_get(a
);
1195 ds_put_format(ds
, "src=%"PRIu16
",dst=%"PRIu16
,
1196 ntohs(udp_key
->udp_src
), ntohs(udp_key
->udp_dst
));
1200 case OVS_KEY_ATTR_ICMP
:
1202 const struct ovs_key_icmp
*icmp_mask
= nl_attr_get(ma
);
1203 const struct ovs_key_icmp
*icmp_key
= nl_attr_get(a
);
1205 ds_put_format(ds
, "type=%"PRIu8
"/%#"PRIx8
",code=%"PRIu8
"/%#"PRIx8
,
1206 icmp_key
->icmp_type
, icmp_mask
->icmp_type
,
1207 icmp_key
->icmp_code
, icmp_mask
->icmp_code
);
1209 const struct ovs_key_icmp
*icmp_key
= nl_attr_get(a
);
1211 ds_put_format(ds
, "type=%"PRIu8
",code=%"PRIu8
,
1212 icmp_key
->icmp_type
, icmp_key
->icmp_code
);
1216 case OVS_KEY_ATTR_ICMPV6
:
1218 const struct ovs_key_icmpv6
*icmpv6_mask
= nl_attr_get(ma
);
1219 const struct ovs_key_icmpv6
*icmpv6_key
= nl_attr_get(a
);
1221 ds_put_format(ds
, "type=%"PRIu8
"/%#"PRIx8
",code=%"PRIu8
"/%#"PRIx8
,
1222 icmpv6_key
->icmpv6_type
, icmpv6_mask
->icmpv6_type
,
1223 icmpv6_key
->icmpv6_code
, icmpv6_mask
->icmpv6_code
);
1225 const struct ovs_key_icmpv6
*icmpv6_key
= nl_attr_get(a
);
1227 ds_put_format(ds
, "type=%"PRIu8
",code=%"PRIu8
,
1228 icmpv6_key
->icmpv6_type
, icmpv6_key
->icmpv6_code
);
1232 case OVS_KEY_ATTR_ARP
:
1234 const struct ovs_key_arp
*arp_mask
= nl_attr_get(ma
);
1235 const struct ovs_key_arp
*arp_key
= nl_attr_get(a
);
1237 ds_put_format(ds
, "sip="IP_FMT
"/"IP_FMT
",tip="IP_FMT
"/"IP_FMT
1238 ",op=%"PRIu16
"/%#"PRIx16
1239 ",sha="ETH_ADDR_FMT
"/"ETH_ADDR_FMT
1240 ",tha="ETH_ADDR_FMT
"/"ETH_ADDR_FMT
,
1241 IP_ARGS(arp_key
->arp_sip
),
1242 IP_ARGS(arp_mask
->arp_sip
),
1243 IP_ARGS(arp_key
->arp_tip
),
1244 IP_ARGS(arp_mask
->arp_tip
),
1245 ntohs(arp_key
->arp_op
), ntohs(arp_mask
->arp_op
),
1246 ETH_ADDR_ARGS(arp_key
->arp_sha
),
1247 ETH_ADDR_ARGS(arp_mask
->arp_sha
),
1248 ETH_ADDR_ARGS(arp_key
->arp_tha
),
1249 ETH_ADDR_ARGS(arp_mask
->arp_tha
));
1251 const struct ovs_key_arp
*arp_key
= nl_attr_get(a
);
1253 ds_put_format(ds
, "sip="IP_FMT
",tip="IP_FMT
",op=%"PRIu16
","
1254 "sha="ETH_ADDR_FMT
",tha="ETH_ADDR_FMT
,
1255 IP_ARGS(arp_key
->arp_sip
), IP_ARGS(arp_key
->arp_tip
),
1256 ntohs(arp_key
->arp_op
),
1257 ETH_ADDR_ARGS(arp_key
->arp_sha
),
1258 ETH_ADDR_ARGS(arp_key
->arp_tha
));
1262 case OVS_KEY_ATTR_ND
: {
1263 const struct ovs_key_nd
*nd_key
, *nd_mask
= NULL
;
1264 char target
[INET6_ADDRSTRLEN
];
1266 nd_key
= nl_attr_get(a
);
1268 nd_mask
= nl_attr_get(ma
);
1271 inet_ntop(AF_INET6
, nd_key
->nd_target
, target
, sizeof target
);
1272 ds_put_format(ds
, "target=%s", target
);
1274 inet_ntop(AF_INET6
, nd_mask
->nd_target
, target
, sizeof target
);
1275 ds_put_format(ds
, "/%s", target
);
1278 if (!eth_addr_is_zero(nd_key
->nd_sll
)) {
1279 ds_put_format(ds
, ",sll="ETH_ADDR_FMT
,
1280 ETH_ADDR_ARGS(nd_key
->nd_sll
));
1282 ds_put_format(ds
, "/"ETH_ADDR_FMT
,
1283 ETH_ADDR_ARGS(nd_mask
->nd_sll
));
1286 if (!eth_addr_is_zero(nd_key
->nd_tll
)) {
1287 ds_put_format(ds
, ",tll="ETH_ADDR_FMT
,
1288 ETH_ADDR_ARGS(nd_key
->nd_tll
));
1290 ds_put_format(ds
, "/"ETH_ADDR_FMT
,
1291 ETH_ADDR_ARGS(nd_mask
->nd_tll
));
1297 case OVS_KEY_ATTR_UNSPEC
:
1298 case __OVS_KEY_ATTR_MAX
:
1300 format_generic_odp_key(a
, ds
);
1302 ds_put_char(ds
, '/');
1303 format_generic_odp_key(ma
, ds
);
1307 ds_put_char(ds
, ')');
1310 static struct nlattr
*
1311 generate_all_wildcard_mask(struct ofpbuf
*ofp
, const struct nlattr
*key
)
1313 const struct nlattr
*a
;
1315 int type
= nl_attr_type(key
);
1316 int size
= nl_attr_get_size(key
);
1318 if (odp_flow_key_attr_len(type
) >=0) {
1319 memset(nl_msg_put_unspec_uninit(ofp
, type
, size
), 0, size
);
1323 nested_mask
= nl_msg_start_nested(ofp
, type
);
1324 NL_ATTR_FOR_EACH(a
, left
, key
, nl_attr_get_size(key
)) {
1325 generate_all_wildcard_mask(ofp
, nl_attr_get(a
));
1327 nl_msg_end_nested(ofp
, nested_mask
);
1333 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1334 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
1335 * 'mask_len' bytes of 'mask' which apply to 'key'. */
1337 odp_flow_format(const struct nlattr
*key
, size_t key_len
,
1338 const struct nlattr
*mask
, size_t mask_len
,
1342 const struct nlattr
*a
;
1344 bool has_ethtype_key
= false;
1345 const struct nlattr
*ma
= NULL
;
1348 ofpbuf_init(&ofp
, 100);
1349 NL_ATTR_FOR_EACH (a
, left
, key
, key_len
) {
1351 ds_put_char(ds
, ',');
1353 if (nl_attr_type(a
) == OVS_KEY_ATTR_ETHERTYPE
) {
1354 has_ethtype_key
= true;
1356 if (mask
&& mask_len
) {
1357 ma
= nl_attr_find__(mask
, mask_len
, nl_attr_type(a
));
1359 ma
= generate_all_wildcard_mask(&ofp
, a
);
1362 format_odp_key_attr(a
, ma
, ds
);
1365 ofpbuf_uninit(&ofp
);
1370 if (left
== key_len
) {
1371 ds_put_cstr(ds
, "<empty>");
1373 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
1374 for (i
= 0; i
< left
; i
++) {
1375 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
1377 ds_put_char(ds
, ')');
1379 if (!has_ethtype_key
) {
1380 ma
= nl_attr_find__(mask
, mask_len
, OVS_KEY_ATTR_ETHERTYPE
);
1382 ds_put_format(ds
, ",eth_type(0/0x%04"PRIx16
")",
1383 ntohs(nl_attr_get_be16(ma
)));
1387 ds_put_cstr(ds
, "<empty>");
1391 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1392 * OVS_KEY_ATTR_* attributes in 'key'. */
1394 odp_flow_key_format(const struct nlattr
*key
,
1395 size_t key_len
, struct ds
*ds
)
1397 odp_flow_format(key
, key_len
, NULL
, 0, ds
);
1401 put_nd(struct ovs_key_nd
* nd_key
, const uint8_t *nd_sll
,
1402 const uint8_t *nd_tll
, struct ofpbuf
*key
)
1405 memcpy(nd_key
->nd_sll
, nd_sll
, ETH_ADDR_LEN
);
1409 memcpy(nd_key
->nd_tll
, nd_tll
, ETH_ADDR_LEN
);
1412 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ND
, nd_key
, sizeof *nd_key
);
1416 put_nd_key(int n
, const char *nd_target_s
, const uint8_t *nd_sll
,
1417 const uint8_t *nd_tll
, struct ofpbuf
*key
)
1419 struct ovs_key_nd nd_key
;
1421 memset(&nd_key
, 0, sizeof nd_key
);
1423 if (inet_pton(AF_INET6
, nd_target_s
, nd_key
.nd_target
) != 1) {
1427 put_nd(&nd_key
, nd_sll
, nd_tll
, key
);
1432 put_nd_mask(int n
, const char *nd_target_s
,
1433 const uint8_t *nd_sll
, const uint8_t *nd_tll
, struct ofpbuf
*mask
)
1435 struct ovs_key_nd nd_mask
;
1437 memset(&nd_mask
, 0xff, sizeof nd_mask
);
1439 if (strlen(nd_target_s
) != 0 &&
1440 inet_pton(AF_INET6
, nd_target_s
, nd_mask
.nd_target
) != 1) {
1444 put_nd(&nd_mask
, nd_sll
, nd_tll
, mask
);
1449 ovs_frag_type_from_string(const char *s
, enum ovs_frag_type
*type
)
1451 if (!strcasecmp(s
, "no")) {
1452 *type
= OVS_FRAG_TYPE_NONE
;
1453 } else if (!strcasecmp(s
, "first")) {
1454 *type
= OVS_FRAG_TYPE_FIRST
;
1455 } else if (!strcasecmp(s
, "later")) {
1456 *type
= OVS_FRAG_TYPE_LATER
;
1464 mpls_lse_from_components(int mpls_label
, int mpls_tc
, int mpls_ttl
, int mpls_bos
)
1466 return (htonl((mpls_label
<< MPLS_LABEL_SHIFT
) |
1467 (mpls_tc
<< MPLS_TC_SHIFT
) |
1468 (mpls_ttl
<< MPLS_TTL_SHIFT
) |
1469 (mpls_bos
<< MPLS_BOS_SHIFT
)));
1473 parse_odp_key_mask_attr(const char *s
, const struct simap
*port_names
,
1474 struct ofpbuf
*key
, struct ofpbuf
*mask
)
1476 /* Many of the sscanf calls in this function use oversized destination
1477 * fields because some sscanf() implementations truncate the range of %i
1478 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
1479 * value of 0x7fff. The other alternatives are to allow only a single
1480 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
1483 * The tun_id parser has to use an alternative approach because there is no
1484 * type larger than 64 bits. */
1487 unsigned long long int priority
;
1488 unsigned long long int priority_mask
;
1491 if (mask
&& sscanf(s
, "skb_priority(%lli/%lli)%n", &priority
,
1492 &priority_mask
, &n
) > 0 && n
> 0) {
1493 nl_msg_put_u32(key
, OVS_KEY_ATTR_PRIORITY
, priority
);
1494 nl_msg_put_u32(mask
, OVS_KEY_ATTR_PRIORITY
, priority_mask
);
1496 } else if (sscanf(s
, "skb_priority(%lli)%n",
1497 &priority
, &n
) > 0 && n
> 0) {
1498 nl_msg_put_u32(key
, OVS_KEY_ATTR_PRIORITY
, priority
);
1500 nl_msg_put_u32(mask
, OVS_KEY_ATTR_PRIORITY
, UINT32_MAX
);
1507 unsigned long long int mark
;
1508 unsigned long long int mark_mask
;
1511 if (mask
&& sscanf(s
, "skb_mark(%lli/%lli)%n", &mark
,
1512 &mark_mask
, &n
) > 0 && n
> 0) {
1513 nl_msg_put_u32(key
, OVS_KEY_ATTR_SKB_MARK
, mark
);
1514 nl_msg_put_u32(mask
, OVS_KEY_ATTR_SKB_MARK
, mark_mask
);
1516 } else if (sscanf(s
, "skb_mark(%lli)%n", &mark
, &n
) > 0 && n
> 0) {
1517 nl_msg_put_u32(key
, OVS_KEY_ATTR_SKB_MARK
, mark
);
1519 nl_msg_put_u32(mask
, OVS_KEY_ATTR_SKB_MARK
, UINT32_MAX
);
1527 int tos
, tos_mask
, ttl
, ttl_mask
;
1528 struct flow_tnl tun_key
, tun_key_mask
;
1529 unsigned long long tun_id_mask
;
1532 if (mask
&& sscanf(s
, "tunnel(tun_id=%31[x0123456789abcdefABCDEF]/%llx,"
1533 "src="IP_SCAN_FMT
"/"IP_SCAN_FMT
",dst="IP_SCAN_FMT
1534 "/"IP_SCAN_FMT
",tos=%i/%i,ttl=%i/%i,flags%n",
1535 tun_id_s
, &tun_id_mask
,
1536 IP_SCAN_ARGS(&tun_key
.ip_src
),
1537 IP_SCAN_ARGS(&tun_key_mask
.ip_src
),
1538 IP_SCAN_ARGS(&tun_key
.ip_dst
),
1539 IP_SCAN_ARGS(&tun_key_mask
.ip_dst
),
1540 &tos
, &tos_mask
, &ttl
, &ttl_mask
,
1545 tun_key
.tun_id
= htonll(strtoull(tun_id_s
, NULL
, 0));
1546 tun_key_mask
.tun_id
= htonll(tun_id_mask
);
1547 tun_key
.ip_tos
= tos
;
1548 tun_key_mask
.ip_tos
= tos_mask
;
1549 tun_key
.ip_ttl
= ttl
;
1550 tun_key_mask
.ip_ttl
= ttl_mask
;
1551 res
= parse_flags(&s
[n
], flow_tun_flag_to_string
, &flags
);
1552 tun_key
.flags
= flags
;
1553 tun_key_mask
.flags
= UINT16_MAX
;
1563 tun_key_to_attr(key
, &tun_key
);
1565 tun_key_to_attr(mask
, &tun_key_mask
);
1568 } else if (sscanf(s
, "tunnel(tun_id=%31[x0123456789abcdefABCDEF],"
1569 "src="IP_SCAN_FMT
",dst="IP_SCAN_FMT
1570 ",tos=%i,ttl=%i,flags%n", tun_id_s
,
1571 IP_SCAN_ARGS(&tun_key
.ip_src
),
1572 IP_SCAN_ARGS(&tun_key
.ip_dst
), &tos
, &ttl
,
1577 tun_key
.tun_id
= htonll(strtoull(tun_id_s
, NULL
, 0));
1578 tun_key
.ip_tos
= tos
;
1579 tun_key
.ip_ttl
= ttl
;
1580 res
= parse_flags(&s
[n
], flow_tun_flag_to_string
, &flags
);
1581 tun_key
.flags
= flags
;
1591 tun_key_to_attr(key
, &tun_key
);
1594 memset(&tun_key
, 0xff, sizeof tun_key
);
1595 tun_key_to_attr(mask
, &tun_key
);
1602 unsigned long long int in_port
;
1603 unsigned long long int in_port_mask
;
1606 if (mask
&& sscanf(s
, "in_port(%lli/%lli)%n", &in_port
,
1607 &in_port_mask
, &n
) > 0 && n
> 0) {
1608 nl_msg_put_u32(key
, OVS_KEY_ATTR_IN_PORT
, in_port
);
1609 nl_msg_put_u32(mask
, OVS_KEY_ATTR_IN_PORT
, in_port_mask
);
1611 } else if (sscanf(s
, "in_port(%lli)%n", &in_port
, &n
) > 0 && n
> 0) {
1612 nl_msg_put_u32(key
, OVS_KEY_ATTR_IN_PORT
, in_port
);
1614 nl_msg_put_u32(mask
, OVS_KEY_ATTR_IN_PORT
, UINT32_MAX
);
1621 if (port_names
&& !strncmp(s
, "in_port(", 8)) {
1623 const struct simap_node
*node
;
1627 name_len
= strcspn(s
, ")");
1628 node
= simap_find_len(port_names
, name
, name_len
);
1630 nl_msg_put_u32(key
, OVS_KEY_ATTR_IN_PORT
, node
->data
);
1633 nl_msg_put_u32(mask
, OVS_KEY_ATTR_IN_PORT
, UINT32_MAX
);
1635 return 8 + name_len
+ 1;
1640 struct ovs_key_ethernet eth_key
;
1641 struct ovs_key_ethernet eth_key_mask
;
1644 if (mask
&& sscanf(s
,
1645 "eth(src="ETH_ADDR_SCAN_FMT
"/"ETH_ADDR_SCAN_FMT
","
1646 "dst="ETH_ADDR_SCAN_FMT
"/"ETH_ADDR_SCAN_FMT
")%n",
1647 ETH_ADDR_SCAN_ARGS(eth_key
.eth_src
),
1648 ETH_ADDR_SCAN_ARGS(eth_key_mask
.eth_src
),
1649 ETH_ADDR_SCAN_ARGS(eth_key
.eth_dst
),
1650 ETH_ADDR_SCAN_ARGS(eth_key_mask
.eth_dst
), &n
) > 0 && n
> 0) {
1652 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ETHERNET
,
1653 ð_key
, sizeof eth_key
);
1654 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_ETHERNET
,
1655 ð_key_mask
, sizeof eth_key_mask
);
1657 } else if (sscanf(s
,
1658 "eth(src="ETH_ADDR_SCAN_FMT
",dst="ETH_ADDR_SCAN_FMT
")%n",
1659 ETH_ADDR_SCAN_ARGS(eth_key
.eth_src
),
1660 ETH_ADDR_SCAN_ARGS(eth_key
.eth_dst
), &n
) > 0 && n
> 0) {
1661 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ETHERNET
,
1662 ð_key
, sizeof eth_key
);
1665 memset(ð_key
, 0xff, sizeof eth_key
);
1666 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_ETHERNET
,
1667 ð_key
, sizeof eth_key
);
1674 uint16_t vid
, vid_mask
;
1679 if (mask
&& (sscanf(s
, "vlan(vid=%"SCNi16
"/%"SCNi16
",pcp=%i/%i)%n",
1680 &vid
, &vid_mask
, &pcp
, &pcp_mask
, &n
) > 0 && n
> 0)) {
1681 nl_msg_put_be16(key
, OVS_KEY_ATTR_VLAN
,
1682 htons((vid
<< VLAN_VID_SHIFT
) |
1683 (pcp
<< VLAN_PCP_SHIFT
) |
1685 nl_msg_put_be16(mask
, OVS_KEY_ATTR_VLAN
,
1686 htons((vid_mask
<< VLAN_VID_SHIFT
) |
1687 (pcp_mask
<< VLAN_PCP_SHIFT
) |
1688 (1 << VLAN_CFI_SHIFT
)));
1690 } else if ((sscanf(s
, "vlan(vid=%"SCNi16
",pcp=%i)%n",
1691 &vid
, &pcp
, &n
) > 0 && n
> 0)) {
1692 nl_msg_put_be16(key
, OVS_KEY_ATTR_VLAN
,
1693 htons((vid
<< VLAN_VID_SHIFT
) |
1694 (pcp
<< VLAN_PCP_SHIFT
) |
1697 nl_msg_put_be16(mask
, OVS_KEY_ATTR_VLAN
, htons(UINT16_MAX
));
1700 } else if (mask
&& (sscanf(s
, "vlan(vid=%"SCNi16
"/%"SCNi16
",pcp=%i/%i,cfi=%i/%i)%n",
1701 &vid
, &vid_mask
, &pcp
, &pcp_mask
, &cfi
, &cfi_mask
, &n
) > 0 && n
> 0)) {
1702 nl_msg_put_be16(key
, OVS_KEY_ATTR_VLAN
,
1703 htons((vid
<< VLAN_VID_SHIFT
) |
1704 (pcp
<< VLAN_PCP_SHIFT
) |
1705 (cfi
? VLAN_CFI
: 0)));
1706 nl_msg_put_be16(mask
, OVS_KEY_ATTR_VLAN
,
1707 htons((vid_mask
<< VLAN_VID_SHIFT
) |
1708 (pcp_mask
<< VLAN_PCP_SHIFT
) |
1709 (cfi_mask
<< VLAN_CFI_SHIFT
)));
1711 } else if ((sscanf(s
, "vlan(vid=%"SCNi16
",pcp=%i,cfi=%i)%n",
1712 &vid
, &pcp
, &cfi
, &n
) > 0 && n
> 0)) {
1713 nl_msg_put_be16(key
, OVS_KEY_ATTR_VLAN
,
1714 htons((vid
<< VLAN_VID_SHIFT
) |
1715 (pcp
<< VLAN_PCP_SHIFT
) |
1716 (cfi
? VLAN_CFI
: 0)));
1718 nl_msg_put_be16(mask
, OVS_KEY_ATTR_VLAN
, htons(UINT16_MAX
));
1729 if (mask
&& sscanf(s
, "eth_type(%i/%i)%n",
1730 ð_type
, ð_type_mask
, &n
) > 0 && n
> 0) {
1731 if (eth_type
!= 0) {
1732 nl_msg_put_be16(key
, OVS_KEY_ATTR_ETHERTYPE
, htons(eth_type
));
1734 nl_msg_put_be16(mask
, OVS_KEY_ATTR_ETHERTYPE
, htons(eth_type_mask
));
1736 } else if (sscanf(s
, "eth_type(%i)%n", ð_type
, &n
) > 0 && n
> 0) {
1737 nl_msg_put_be16(key
, OVS_KEY_ATTR_ETHERTYPE
, htons(eth_type
));
1739 nl_msg_put_be16(mask
, OVS_KEY_ATTR_ETHERTYPE
,
1747 int label
, tc
, ttl
, bos
;
1748 int label_mask
, tc_mask
, ttl_mask
, bos_mask
;
1751 if (mask
&& sscanf(s
, "mpls(label=%"SCNi32
"/%"SCNi32
",tc=%i/%i,ttl=%i/%i,bos=%i/%i)%n",
1752 &label
, &label_mask
, &tc
, &tc_mask
, &ttl
, &ttl_mask
, &bos
, &bos_mask
, &n
) > 0 && n
> 0) {
1753 struct ovs_key_mpls
*mpls
, *mpls_mask
;
1755 mpls
= nl_msg_put_unspec_uninit(key
, OVS_KEY_ATTR_MPLS
,
1757 mpls
->mpls_lse
= mpls_lse_from_components(label
, tc
, ttl
, bos
);
1759 mpls_mask
= nl_msg_put_unspec_uninit(mask
, OVS_KEY_ATTR_MPLS
,
1761 mpls_mask
->mpls_lse
= mpls_lse_from_components(
1762 label_mask
, tc_mask
, ttl_mask
, bos_mask
);
1764 } else if (sscanf(s
, "mpls(label=%"SCNi32
",tc=%i,ttl=%i,bos=%i)%n",
1765 &label
, &tc
, &ttl
, &bos
, &n
) > 0 &&
1767 struct ovs_key_mpls
*mpls
;
1769 mpls
= nl_msg_put_unspec_uninit(key
, OVS_KEY_ATTR_MPLS
,
1771 mpls
->mpls_lse
= mpls_lse_from_components(label
, tc
, ttl
, bos
);
1773 mpls
= nl_msg_put_unspec_uninit(mask
, OVS_KEY_ATTR_MPLS
,
1775 mpls
->mpls_lse
= htonl(UINT32_MAX
);
1783 ovs_be32 ipv4_src
, ipv4_src_mask
;
1784 ovs_be32 ipv4_dst
, ipv4_dst_mask
;
1785 int ipv4_proto
, ipv4_proto_mask
;
1786 int ipv4_tos
, ipv4_tos_mask
;
1787 int ipv4_ttl
, ipv4_ttl_mask
;
1790 enum ovs_frag_type ipv4_frag
;
1793 if (mask
&& sscanf(s
, "ipv4(src="IP_SCAN_FMT
"/"IP_SCAN_FMT
","
1794 "dst="IP_SCAN_FMT
"/"IP_SCAN_FMT
","
1795 "proto=%i/%i,tos=%i/%i,ttl=%i/%i,"
1796 "frag=%7[a-z]/%i)%n",
1797 IP_SCAN_ARGS(&ipv4_src
), IP_SCAN_ARGS(&ipv4_src_mask
),
1798 IP_SCAN_ARGS(&ipv4_dst
), IP_SCAN_ARGS(&ipv4_dst_mask
),
1799 &ipv4_proto
, &ipv4_proto_mask
,
1800 &ipv4_tos
, &ipv4_tos_mask
, &ipv4_ttl
, &ipv4_ttl_mask
,
1801 frag
, &ipv4_frag_mask
, &n
) > 0
1803 && ovs_frag_type_from_string(frag
, &ipv4_frag
)) {
1804 struct ovs_key_ipv4 ipv4_key
;
1805 struct ovs_key_ipv4 ipv4_mask
;
1807 ipv4_key
.ipv4_src
= ipv4_src
;
1808 ipv4_key
.ipv4_dst
= ipv4_dst
;
1809 ipv4_key
.ipv4_proto
= ipv4_proto
;
1810 ipv4_key
.ipv4_tos
= ipv4_tos
;
1811 ipv4_key
.ipv4_ttl
= ipv4_ttl
;
1812 ipv4_key
.ipv4_frag
= ipv4_frag
;
1813 nl_msg_put_unspec(key
, OVS_KEY_ATTR_IPV4
,
1814 &ipv4_key
, sizeof ipv4_key
);
1816 ipv4_mask
.ipv4_src
= ipv4_src_mask
;
1817 ipv4_mask
.ipv4_dst
= ipv4_dst_mask
;
1818 ipv4_mask
.ipv4_proto
= ipv4_proto_mask
;
1819 ipv4_mask
.ipv4_tos
= ipv4_tos_mask
;
1820 ipv4_mask
.ipv4_ttl
= ipv4_ttl_mask
;
1821 ipv4_mask
.ipv4_frag
= ipv4_frag_mask
;
1822 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_IPV4
,
1823 &ipv4_mask
, sizeof ipv4_mask
);
1825 } else if (sscanf(s
, "ipv4(src="IP_SCAN_FMT
",dst="IP_SCAN_FMT
","
1826 "proto=%i,tos=%i,ttl=%i,frag=%7[a-z])%n",
1827 IP_SCAN_ARGS(&ipv4_src
), IP_SCAN_ARGS(&ipv4_dst
),
1828 &ipv4_proto
, &ipv4_tos
, &ipv4_ttl
, frag
, &n
) > 0
1830 && ovs_frag_type_from_string(frag
, &ipv4_frag
)) {
1831 struct ovs_key_ipv4 ipv4_key
;
1833 ipv4_key
.ipv4_src
= ipv4_src
;
1834 ipv4_key
.ipv4_dst
= ipv4_dst
;
1835 ipv4_key
.ipv4_proto
= ipv4_proto
;
1836 ipv4_key
.ipv4_tos
= ipv4_tos
;
1837 ipv4_key
.ipv4_ttl
= ipv4_ttl
;
1838 ipv4_key
.ipv4_frag
= ipv4_frag
;
1839 nl_msg_put_unspec(key
, OVS_KEY_ATTR_IPV4
,
1840 &ipv4_key
, sizeof ipv4_key
);
1843 memset(&ipv4_key
, 0xff, sizeof ipv4_key
);
1844 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_IPV4
,
1845 &ipv4_key
, sizeof ipv4_key
);
1852 char ipv6_src_s
[IPV6_SCAN_LEN
+ 1];
1853 char ipv6_src_mask_s
[IPV6_SCAN_LEN
+ 1];
1854 char ipv6_dst_s
[IPV6_SCAN_LEN
+ 1];
1855 char ipv6_dst_mask_s
[IPV6_SCAN_LEN
+ 1];
1856 int ipv6_label
, ipv6_label_mask
;
1857 int ipv6_proto
, ipv6_proto_mask
;
1858 int ipv6_tclass
, ipv6_tclass_mask
;
1859 int ipv6_hlimit
, ipv6_hlimit_mask
;
1861 enum ovs_frag_type ipv6_frag
;
1865 if (mask
&& sscanf(s
, "ipv6(src="IPV6_SCAN_FMT
"/"IPV6_SCAN_FMT
",dst="
1866 IPV6_SCAN_FMT
"/"IPV6_SCAN_FMT
","
1867 "label=%i/%i,proto=%i/%i,tclass=%i/%i,"
1868 "hlimit=%i/%i,frag=%7[a-z]/%i)%n",
1869 ipv6_src_s
, ipv6_src_mask_s
, ipv6_dst_s
, ipv6_dst_mask_s
,
1870 &ipv6_label
, &ipv6_label_mask
, &ipv6_proto
,
1871 &ipv6_proto_mask
, &ipv6_tclass
, &ipv6_tclass_mask
,
1872 &ipv6_hlimit
, &ipv6_hlimit_mask
, frag
,
1873 &ipv6_frag_mask
, &n
) > 0
1875 && ovs_frag_type_from_string(frag
, &ipv6_frag
)) {
1876 struct ovs_key_ipv6 ipv6_key
;
1877 struct ovs_key_ipv6 ipv6_mask
;
1879 if (inet_pton(AF_INET6
, ipv6_src_s
, &ipv6_key
.ipv6_src
) != 1 ||
1880 inet_pton(AF_INET6
, ipv6_dst_s
, &ipv6_key
.ipv6_dst
) != 1 ||
1881 inet_pton(AF_INET6
, ipv6_src_mask_s
, &ipv6_mask
.ipv6_src
) != 1 ||
1882 inet_pton(AF_INET6
, ipv6_dst_mask_s
, &ipv6_mask
.ipv6_dst
) != 1) {
1886 ipv6_key
.ipv6_label
= htonl(ipv6_label
);
1887 ipv6_key
.ipv6_proto
= ipv6_proto
;
1888 ipv6_key
.ipv6_tclass
= ipv6_tclass
;
1889 ipv6_key
.ipv6_hlimit
= ipv6_hlimit
;
1890 ipv6_key
.ipv6_frag
= ipv6_frag
;
1891 nl_msg_put_unspec(key
, OVS_KEY_ATTR_IPV6
,
1892 &ipv6_key
, sizeof ipv6_key
);
1894 ipv6_mask
.ipv6_label
= htonl(ipv6_label_mask
);
1895 ipv6_mask
.ipv6_proto
= ipv6_proto_mask
;
1896 ipv6_mask
.ipv6_tclass
= ipv6_tclass_mask
;
1897 ipv6_mask
.ipv6_hlimit
= ipv6_hlimit_mask
;
1898 ipv6_mask
.ipv6_frag
= ipv6_frag_mask
;
1899 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_IPV6
,
1900 &ipv6_mask
, sizeof ipv6_mask
);
1902 } else if (sscanf(s
, "ipv6(src="IPV6_SCAN_FMT
",dst="IPV6_SCAN_FMT
","
1903 "label=%i,proto=%i,tclass=%i,hlimit=%i,frag=%7[a-z])%n",
1904 ipv6_src_s
, ipv6_dst_s
, &ipv6_label
,
1905 &ipv6_proto
, &ipv6_tclass
, &ipv6_hlimit
, frag
, &n
) > 0
1907 && ovs_frag_type_from_string(frag
, &ipv6_frag
)) {
1908 struct ovs_key_ipv6 ipv6_key
;
1910 if (inet_pton(AF_INET6
, ipv6_src_s
, &ipv6_key
.ipv6_src
) != 1 ||
1911 inet_pton(AF_INET6
, ipv6_dst_s
, &ipv6_key
.ipv6_dst
) != 1) {
1914 ipv6_key
.ipv6_label
= htonl(ipv6_label
);
1915 ipv6_key
.ipv6_proto
= ipv6_proto
;
1916 ipv6_key
.ipv6_tclass
= ipv6_tclass
;
1917 ipv6_key
.ipv6_hlimit
= ipv6_hlimit
;
1918 ipv6_key
.ipv6_frag
= ipv6_frag
;
1919 nl_msg_put_unspec(key
, OVS_KEY_ATTR_IPV6
,
1920 &ipv6_key
, sizeof ipv6_key
);
1923 memset(&ipv6_key
, 0xff, sizeof ipv6_key
);
1924 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_IPV6
,
1925 &ipv6_key
, sizeof ipv6_key
);
1938 if (mask
&& sscanf(s
, "tcp(src=%i/%i,dst=%i/%i)%n",
1939 &tcp_src
, &tcp_src_mask
, &tcp_dst
, &tcp_dst_mask
, &n
) > 0
1941 struct ovs_key_tcp tcp_key
;
1942 struct ovs_key_tcp tcp_mask
;
1944 tcp_key
.tcp_src
= htons(tcp_src
);
1945 tcp_key
.tcp_dst
= htons(tcp_dst
);
1946 nl_msg_put_unspec(key
, OVS_KEY_ATTR_TCP
, &tcp_key
, sizeof tcp_key
);
1948 tcp_mask
.tcp_src
= htons(tcp_src_mask
);
1949 tcp_mask
.tcp_dst
= htons(tcp_dst_mask
);
1950 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_TCP
,
1951 &tcp_mask
, sizeof tcp_mask
);
1953 } else if (sscanf(s
, "tcp(src=%i,dst=%i)%n",&tcp_src
, &tcp_dst
, &n
) > 0
1955 struct ovs_key_tcp tcp_key
;
1957 tcp_key
.tcp_src
= htons(tcp_src
);
1958 tcp_key
.tcp_dst
= htons(tcp_dst
);
1959 nl_msg_put_unspec(key
, OVS_KEY_ATTR_TCP
, &tcp_key
, sizeof tcp_key
);
1962 memset(&tcp_key
, 0xff, sizeof tcp_key
);
1963 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_TCP
,
1964 &tcp_key
, sizeof tcp_key
);
1977 if (mask
&& sscanf(s
, "udp(src=%i/%i,dst=%i/%i)%n",
1978 &udp_src
, &udp_src_mask
,
1979 &udp_dst
, &udp_dst_mask
, &n
) > 0 && n
> 0) {
1980 struct ovs_key_udp udp_key
;
1981 struct ovs_key_udp udp_mask
;
1983 udp_key
.udp_src
= htons(udp_src
);
1984 udp_key
.udp_dst
= htons(udp_dst
);
1985 nl_msg_put_unspec(key
, OVS_KEY_ATTR_UDP
, &udp_key
, sizeof udp_key
);
1987 udp_mask
.udp_src
= htons(udp_src_mask
);
1988 udp_mask
.udp_dst
= htons(udp_dst_mask
);
1989 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_UDP
,
1990 &udp_mask
, sizeof udp_mask
);
1993 if (sscanf(s
, "udp(src=%i,dst=%i)%n", &udp_src
, &udp_dst
, &n
) > 0
1995 struct ovs_key_udp udp_key
;
1997 udp_key
.udp_src
= htons(udp_src
);
1998 udp_key
.udp_dst
= htons(udp_dst
);
1999 nl_msg_put_unspec(key
, OVS_KEY_ATTR_UDP
, &udp_key
, sizeof udp_key
);
2002 memset(&udp_key
, 0xff, sizeof udp_key
);
2003 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_UDP
, &udp_key
, sizeof udp_key
);
2016 if (mask
&& sscanf(s
, "icmp(type=%i/%i,code=%i/%i)%n",
2017 &icmp_type
, &icmp_type_mask
,
2018 &icmp_code
, &icmp_code_mask
, &n
) > 0 && n
> 0) {
2019 struct ovs_key_icmp icmp_key
;
2020 struct ovs_key_icmp icmp_mask
;
2022 icmp_key
.icmp_type
= icmp_type
;
2023 icmp_key
.icmp_code
= icmp_code
;
2024 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ICMP
,
2025 &icmp_key
, sizeof icmp_key
);
2027 icmp_mask
.icmp_type
= icmp_type_mask
;
2028 icmp_mask
.icmp_code
= icmp_code_mask
;
2029 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_ICMP
,
2030 &icmp_mask
, sizeof icmp_mask
);
2032 } else if (sscanf(s
, "icmp(type=%i,code=%i)%n",
2033 &icmp_type
, &icmp_code
, &n
) > 0
2035 struct ovs_key_icmp icmp_key
;
2037 icmp_key
.icmp_type
= icmp_type
;
2038 icmp_key
.icmp_code
= icmp_code
;
2039 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ICMP
,
2040 &icmp_key
, sizeof icmp_key
);
2042 memset(&icmp_key
, 0xff, sizeof icmp_key
);
2043 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_ICMP
, &icmp_key
,
2051 struct ovs_key_icmpv6 icmpv6_key
;
2052 struct ovs_key_icmpv6 icmpv6_mask
;
2053 int icmpv6_type_mask
;
2054 int icmpv6_code_mask
;
2057 if (mask
&& sscanf(s
, "icmpv6(type=%"SCNi8
"/%i,code=%"SCNi8
"/%i)%n",
2058 &icmpv6_key
.icmpv6_type
, &icmpv6_type_mask
,
2059 &icmpv6_key
.icmpv6_code
, &icmpv6_code_mask
, &n
) > 0
2061 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ICMPV6
,
2062 &icmpv6_key
, sizeof icmpv6_key
);
2064 icmpv6_mask
.icmpv6_type
= icmpv6_type_mask
;
2065 icmpv6_mask
.icmpv6_code
= icmpv6_code_mask
;
2066 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_ICMPV6
, &icmpv6_mask
,
2067 sizeof icmpv6_mask
);
2069 } else if (sscanf(s
, "icmpv6(type=%"SCNi8
",code=%"SCNi8
")%n",
2070 &icmpv6_key
.icmpv6_type
, &icmpv6_key
.icmpv6_code
,&n
) > 0
2072 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ICMPV6
,
2073 &icmpv6_key
, sizeof icmpv6_key
);
2076 memset(&icmpv6_key
, 0xff, sizeof icmpv6_key
);
2077 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_ICMPV6
, &icmpv6_key
,
2085 ovs_be32 arp_sip
, arp_sip_mask
;
2086 ovs_be32 arp_tip
, arp_tip_mask
;
2087 int arp_op
, arp_op_mask
;
2088 uint8_t arp_sha
[ETH_ADDR_LEN
];
2089 uint8_t arp_sha_mask
[ETH_ADDR_LEN
];
2090 uint8_t arp_tha
[ETH_ADDR_LEN
];
2091 uint8_t arp_tha_mask
[ETH_ADDR_LEN
];
2094 if (mask
&& sscanf(s
, "arp(sip="IP_SCAN_FMT
"/"IP_SCAN_FMT
","
2095 "tip="IP_SCAN_FMT
"/"IP_SCAN_FMT
","
2096 "op=%i/%i,sha="ETH_ADDR_SCAN_FMT
"/"ETH_ADDR_SCAN_FMT
","
2097 "tha="ETH_ADDR_SCAN_FMT
"/"ETH_ADDR_SCAN_FMT
")%n",
2098 IP_SCAN_ARGS(&arp_sip
), IP_SCAN_ARGS(&arp_sip_mask
),
2099 IP_SCAN_ARGS(&arp_tip
), IP_SCAN_ARGS(&arp_tip_mask
),
2100 &arp_op
, &arp_op_mask
,
2101 ETH_ADDR_SCAN_ARGS(arp_sha
),
2102 ETH_ADDR_SCAN_ARGS(arp_sha_mask
),
2103 ETH_ADDR_SCAN_ARGS(arp_tha
),
2104 ETH_ADDR_SCAN_ARGS(arp_tha_mask
), &n
) > 0 && n
> 0) {
2105 struct ovs_key_arp arp_key
;
2106 struct ovs_key_arp arp_mask
;
2108 memset(&arp_key
, 0, sizeof arp_key
);
2109 arp_key
.arp_sip
= arp_sip
;
2110 arp_key
.arp_tip
= arp_tip
;
2111 arp_key
.arp_op
= htons(arp_op
);
2112 memcpy(arp_key
.arp_sha
, arp_sha
, ETH_ADDR_LEN
);
2113 memcpy(arp_key
.arp_tha
, arp_tha
, ETH_ADDR_LEN
);
2114 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ARP
, &arp_key
, sizeof arp_key
);
2116 arp_mask
.arp_sip
= arp_sip_mask
;
2117 arp_mask
.arp_tip
= arp_tip_mask
;
2118 arp_mask
.arp_op
= htons(arp_op_mask
);
2119 memcpy(arp_mask
.arp_sha
, arp_sha_mask
, ETH_ADDR_LEN
);
2120 memcpy(arp_mask
.arp_tha
, arp_tha_mask
, ETH_ADDR_LEN
);
2121 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_ARP
,
2122 &arp_mask
, sizeof arp_mask
);
2124 } else if (sscanf(s
, "arp(sip="IP_SCAN_FMT
",tip="IP_SCAN_FMT
","
2125 "op=%i,sha="ETH_ADDR_SCAN_FMT
",tha="ETH_ADDR_SCAN_FMT
")%n",
2126 IP_SCAN_ARGS(&arp_sip
),
2127 IP_SCAN_ARGS(&arp_tip
),
2129 ETH_ADDR_SCAN_ARGS(arp_sha
),
2130 ETH_ADDR_SCAN_ARGS(arp_tha
), &n
) > 0 && n
> 0) {
2131 struct ovs_key_arp arp_key
;
2133 memset(&arp_key
, 0, sizeof arp_key
);
2134 arp_key
.arp_sip
= arp_sip
;
2135 arp_key
.arp_tip
= arp_tip
;
2136 arp_key
.arp_op
= htons(arp_op
);
2137 memcpy(arp_key
.arp_sha
, arp_sha
, ETH_ADDR_LEN
);
2138 memcpy(arp_key
.arp_tha
, arp_tha
, ETH_ADDR_LEN
);
2139 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ARP
, &arp_key
, sizeof arp_key
);
2142 memset(&arp_key
, 0xff, sizeof arp_key
);
2143 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_ARP
,
2144 &arp_key
, sizeof arp_key
);
2151 char nd_target_s
[IPV6_SCAN_LEN
+ 1];
2152 char nd_target_mask_s
[IPV6_SCAN_LEN
+ 1];
2153 uint8_t nd_sll
[ETH_ADDR_LEN
];
2154 uint8_t nd_sll_mask
[ETH_ADDR_LEN
];
2155 uint8_t nd_tll
[ETH_ADDR_LEN
];
2156 uint8_t nd_tll_mask
[ETH_ADDR_LEN
];
2159 nd_target_mask_s
[0] = 0;
2160 memset(nd_sll_mask
, 0xff, sizeof nd_sll_mask
);
2161 memset(nd_tll_mask
, 0xff, sizeof nd_tll_mask
);
2163 if (mask
&& sscanf(s
, "nd(target="IPV6_SCAN_FMT
"/"IPV6_SCAN_FMT
")%n",
2164 nd_target_s
, nd_target_mask_s
, &n
) > 0 && n
> 0) {
2165 put_nd_key(n
, nd_target_s
, NULL
, NULL
, key
);
2166 put_nd_mask(n
, nd_target_mask_s
, NULL
, NULL
, mask
);
2167 } else if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
")%n",
2168 nd_target_s
, &n
) > 0 && n
> 0) {
2169 put_nd_key(n
, nd_target_s
, NULL
, NULL
, key
);
2171 put_nd_mask(n
, nd_target_mask_s
, NULL
, NULL
, mask
);
2173 } else if (mask
&& sscanf(s
, "nd(target="IPV6_SCAN_FMT
"/"IPV6_SCAN_FMT
2174 ",sll="ETH_ADDR_SCAN_FMT
"/"ETH_ADDR_SCAN_FMT
")%n",
2175 nd_target_s
, nd_target_mask_s
,
2176 ETH_ADDR_SCAN_ARGS(nd_sll
),
2177 ETH_ADDR_SCAN_ARGS(nd_sll_mask
), &n
) > 0 && n
> 0) {
2178 put_nd_key(n
, nd_target_s
, nd_sll
, NULL
, key
);
2179 put_nd_mask(n
, nd_target_mask_s
, nd_sll_mask
, NULL
, mask
);
2180 } else if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
",sll="ETH_ADDR_SCAN_FMT
")%n",
2181 nd_target_s
, ETH_ADDR_SCAN_ARGS(nd_sll
), &n
) > 0
2183 put_nd_key(n
, nd_target_s
, nd_sll
, NULL
, key
);
2185 put_nd_mask(n
, nd_target_mask_s
, nd_sll_mask
, NULL
, mask
);
2187 } else if (mask
&& sscanf(s
, "nd(target="IPV6_SCAN_FMT
"/"IPV6_SCAN_FMT
2188 ",tll="ETH_ADDR_SCAN_FMT
"/"ETH_ADDR_SCAN_FMT
")%n",
2189 nd_target_s
, nd_target_mask_s
,
2190 ETH_ADDR_SCAN_ARGS(nd_tll
),
2191 ETH_ADDR_SCAN_ARGS(nd_tll_mask
), &n
) > 0 && n
> 0) {
2192 put_nd_key(n
, nd_target_s
, NULL
, nd_tll
, key
);
2193 put_nd_mask(n
, nd_target_mask_s
, NULL
, nd_tll_mask
, mask
);
2194 } else if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
",tll="ETH_ADDR_SCAN_FMT
")%n",
2195 nd_target_s
, ETH_ADDR_SCAN_ARGS(nd_tll
), &n
) > 0
2197 put_nd_key(n
, nd_target_s
, NULL
, nd_tll
, key
);
2199 put_nd_mask(n
, nd_target_mask_s
, NULL
, nd_tll_mask
, mask
);
2201 } else if (mask
&& sscanf(s
, "nd(target="IPV6_SCAN_FMT
"/"IPV6_SCAN_FMT
2202 ",sll="ETH_ADDR_SCAN_FMT
"/"ETH_ADDR_SCAN_FMT
","
2203 "tll="ETH_ADDR_SCAN_FMT
"/"ETH_ADDR_SCAN_FMT
")%n",
2204 nd_target_s
, nd_target_mask_s
,
2205 ETH_ADDR_SCAN_ARGS(nd_sll
), ETH_ADDR_SCAN_ARGS(nd_sll_mask
),
2206 ETH_ADDR_SCAN_ARGS(nd_tll
), ETH_ADDR_SCAN_ARGS(nd_tll_mask
),
2209 put_nd_key(n
, nd_target_s
, nd_sll
, nd_tll
, key
);
2210 put_nd_mask(n
, nd_target_mask_s
, nd_sll_mask
, nd_tll_mask
, mask
);
2211 } else if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
",sll="ETH_ADDR_SCAN_FMT
","
2212 "tll="ETH_ADDR_SCAN_FMT
")%n",
2213 nd_target_s
, ETH_ADDR_SCAN_ARGS(nd_sll
),
2214 ETH_ADDR_SCAN_ARGS(nd_tll
), &n
) > 0
2216 put_nd_key(n
, nd_target_s
, nd_sll
, nd_tll
, key
);
2218 put_nd_mask(n
, nd_target_mask_s
,
2219 nd_sll_mask
, nd_tll_mask
, mask
);
2228 if (!strncmp(s
, "encap(", 6)) {
2229 const char *start
= s
;
2230 size_t encap
, encap_mask
= 0;
2232 encap
= nl_msg_start_nested(key
, OVS_KEY_ATTR_ENCAP
);
2234 encap_mask
= nl_msg_start_nested(mask
, OVS_KEY_ATTR_ENCAP
);
2241 s
+= strspn(s
, ", \t\r\n");
2244 } else if (*s
== ')') {
2248 retval
= parse_odp_key_mask_attr(s
, port_names
, key
, mask
);
2256 nl_msg_end_nested(key
, encap
);
2258 nl_msg_end_nested(mask
, encap_mask
);
2267 /* Parses the string representation of a datapath flow key, in the
2268 * format output by odp_flow_key_format(). Returns 0 if successful,
2269 * otherwise a positive errno value. On success, the flow key is
2270 * appended to 'key' as a series of Netlink attributes. On failure, no
2271 * data is appended to 'key'. Either way, 'key''s data might be
2274 * If 'port_names' is nonnull, it points to an simap that maps from a port name
2275 * to a port number. (Port names may be used instead of port numbers in
2278 * On success, the attributes appended to 'key' are individually syntactically
2279 * valid, but they may not be valid as a sequence. 'key' might, for example,
2280 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
2282 odp_flow_from_string(const char *s
, const struct simap
*port_names
,
2283 struct ofpbuf
*key
, struct ofpbuf
*mask
)
2285 const size_t old_size
= key
->size
;
2289 s
+= strspn(s
, delimiters
);
2294 retval
= parse_odp_key_mask_attr(s
, port_names
, key
, mask
);
2296 key
->size
= old_size
;
2306 ovs_to_odp_frag(uint8_t nw_frag
)
2308 return (nw_frag
== 0 ? OVS_FRAG_TYPE_NONE
2309 : nw_frag
== FLOW_NW_FRAG_ANY
? OVS_FRAG_TYPE_FIRST
2310 : OVS_FRAG_TYPE_LATER
);
2314 odp_flow_key_from_flow__(struct ofpbuf
*buf
, const struct flow
*data
,
2315 const struct flow
*flow
, odp_port_t odp_in_port
)
2318 struct ovs_key_ethernet
*eth_key
;
2321 /* We assume that if 'data' and 'flow' are not the same, we should
2322 * treat 'data' as a mask. */
2323 is_mask
= (data
!= flow
);
2325 if (flow
->skb_priority
) {
2326 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, data
->skb_priority
);
2329 if (flow
->tunnel
.ip_dst
) {
2330 tun_key_to_attr(buf
, &data
->tunnel
);
2333 if (flow
->skb_mark
) {
2334 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, data
->skb_mark
);
2337 /* Add an ingress port attribute if this is a mask or 'odp_in_port'
2338 * is not the magical value "ODPP_NONE". */
2339 if (is_mask
|| odp_in_port
!= ODPP_NONE
) {
2340 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, odp_in_port
);
2343 eth_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ETHERNET
,
2345 memcpy(eth_key
->eth_src
, data
->dl_src
, ETH_ADDR_LEN
);
2346 memcpy(eth_key
->eth_dst
, data
->dl_dst
, ETH_ADDR_LEN
);
2348 if (flow
->vlan_tci
!= htons(0) || flow
->dl_type
== htons(ETH_TYPE_VLAN
)) {
2350 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, htons(UINT16_MAX
));
2352 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, htons(ETH_TYPE_VLAN
));
2354 nl_msg_put_be16(buf
, OVS_KEY_ATTR_VLAN
, data
->vlan_tci
);
2355 encap
= nl_msg_start_nested(buf
, OVS_KEY_ATTR_ENCAP
);
2356 if (flow
->vlan_tci
== htons(0)) {
2363 if (ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
2364 /* For backwards compatibility with kernels that don't support
2365 * wildcarding, the following convention is used to encode the
2366 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
2369 * -------- -------- -------
2370 * >0x5ff 0xffff Specified Ethernet II Ethertype.
2371 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
2372 * <none> 0xffff Any non-Ethernet II frame (except valid
2373 * 802.3 SNAP packet with valid eth_type).
2376 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, data
->dl_type
);
2381 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, data
->dl_type
);
2383 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
2384 struct ovs_key_ipv4
*ipv4_key
;
2386 ipv4_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV4
,
2388 ipv4_key
->ipv4_src
= data
->nw_src
;
2389 ipv4_key
->ipv4_dst
= data
->nw_dst
;
2390 ipv4_key
->ipv4_proto
= data
->nw_proto
;
2391 ipv4_key
->ipv4_tos
= data
->nw_tos
;
2392 ipv4_key
->ipv4_ttl
= data
->nw_ttl
;
2393 ipv4_key
->ipv4_frag
= ovs_to_odp_frag(data
->nw_frag
);
2394 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
2395 struct ovs_key_ipv6
*ipv6_key
;
2397 ipv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV6
,
2399 memcpy(ipv6_key
->ipv6_src
, &data
->ipv6_src
, sizeof ipv6_key
->ipv6_src
);
2400 memcpy(ipv6_key
->ipv6_dst
, &data
->ipv6_dst
, sizeof ipv6_key
->ipv6_dst
);
2401 ipv6_key
->ipv6_label
= data
->ipv6_label
;
2402 ipv6_key
->ipv6_proto
= data
->nw_proto
;
2403 ipv6_key
->ipv6_tclass
= data
->nw_tos
;
2404 ipv6_key
->ipv6_hlimit
= data
->nw_ttl
;
2405 ipv6_key
->ipv6_frag
= ovs_to_odp_frag(flow
->nw_frag
);
2406 } else if (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
2407 flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
2408 struct ovs_key_arp
*arp_key
;
2410 arp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ARP
,
2412 memset(arp_key
, 0, sizeof *arp_key
);
2413 arp_key
->arp_sip
= data
->nw_src
;
2414 arp_key
->arp_tip
= data
->nw_dst
;
2415 arp_key
->arp_op
= htons(data
->nw_proto
);
2416 memcpy(arp_key
->arp_sha
, data
->arp_sha
, ETH_ADDR_LEN
);
2417 memcpy(arp_key
->arp_tha
, data
->arp_tha
, ETH_ADDR_LEN
);
2420 if (flow
->mpls_depth
) {
2421 struct ovs_key_mpls
*mpls_key
;
2423 mpls_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_MPLS
,
2425 mpls_key
->mpls_lse
= data
->mpls_lse
;
2428 if (is_ip_any(flow
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
2429 if (flow
->nw_proto
== IPPROTO_TCP
) {
2430 struct ovs_key_tcp
*tcp_key
;
2432 tcp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_TCP
,
2434 tcp_key
->tcp_src
= data
->tp_src
;
2435 tcp_key
->tcp_dst
= data
->tp_dst
;
2436 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
2437 struct ovs_key_udp
*udp_key
;
2439 udp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_UDP
,
2441 udp_key
->udp_src
= data
->tp_src
;
2442 udp_key
->udp_dst
= data
->tp_dst
;
2443 } else if (flow
->dl_type
== htons(ETH_TYPE_IP
)
2444 && flow
->nw_proto
== IPPROTO_ICMP
) {
2445 struct ovs_key_icmp
*icmp_key
;
2447 icmp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMP
,
2449 icmp_key
->icmp_type
= ntohs(data
->tp_src
);
2450 icmp_key
->icmp_code
= ntohs(data
->tp_dst
);
2451 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)
2452 && flow
->nw_proto
== IPPROTO_ICMPV6
) {
2453 struct ovs_key_icmpv6
*icmpv6_key
;
2455 icmpv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMPV6
,
2456 sizeof *icmpv6_key
);
2457 icmpv6_key
->icmpv6_type
= ntohs(data
->tp_src
);
2458 icmpv6_key
->icmpv6_code
= ntohs(data
->tp_dst
);
2460 if (icmpv6_key
->icmpv6_type
== ND_NEIGHBOR_SOLICIT
2461 || icmpv6_key
->icmpv6_type
== ND_NEIGHBOR_ADVERT
) {
2462 struct ovs_key_nd
*nd_key
;
2464 nd_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ND
,
2466 memcpy(nd_key
->nd_target
, &data
->nd_target
,
2467 sizeof nd_key
->nd_target
);
2468 memcpy(nd_key
->nd_sll
, data
->arp_sha
, ETH_ADDR_LEN
);
2469 memcpy(nd_key
->nd_tll
, data
->arp_tha
, ETH_ADDR_LEN
);
2476 nl_msg_end_nested(buf
, encap
);
2480 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
2481 * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
2482 * number rather than a datapath port number). Instead, if 'odp_in_port'
2483 * is anything other than ODPP_NONE, it is included in 'buf' as the input
2486 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
2487 * capable of being expanded to allow for that much space. */
2489 odp_flow_key_from_flow(struct ofpbuf
*buf
, const struct flow
*flow
,
2490 odp_port_t odp_in_port
)
2492 odp_flow_key_from_flow__(buf
, flow
, flow
, odp_in_port
);
2495 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
2496 * 'buf'. 'flow' is used as a template to determine how to interpret
2497 * 'mask'. For example, the 'dl_type' of 'mask' describes the mask, but
2498 * it doesn't indicate whether the other fields should be interpreted as
2499 * ARP, IPv4, IPv6, etc.
2501 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
2502 * capable of being expanded to allow for that much space. */
2504 odp_flow_key_from_mask(struct ofpbuf
*buf
, const struct flow
*mask
,
2505 const struct flow
*flow
, uint32_t odp_in_port_mask
)
2507 odp_flow_key_from_flow__(buf
, mask
, flow
, u32_to_odp(odp_in_port_mask
));
2511 odp_flow_key_hash(const struct nlattr
*key
, size_t key_len
)
2513 BUILD_ASSERT_DECL(!(NLA_ALIGNTO
% sizeof(uint32_t)));
2514 return hash_words((const uint32_t *) key
, key_len
/ sizeof(uint32_t), 0);
2518 log_odp_key_attributes(struct vlog_rate_limit
*rl
, const char *title
,
2519 uint64_t attrs
, int out_of_range_attr
,
2520 const struct nlattr
*key
, size_t key_len
)
2525 if (VLOG_DROP_DBG(rl
)) {
2530 for (i
= 0; i
< 64; i
++) {
2531 if (attrs
& (UINT64_C(1) << i
)) {
2532 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
2534 ds_put_format(&s
, " %s",
2535 ovs_key_attr_to_string(i
, namebuf
, sizeof namebuf
));
2538 if (out_of_range_attr
) {
2539 ds_put_format(&s
, " %d (and possibly others)", out_of_range_attr
);
2542 ds_put_cstr(&s
, ": ");
2543 odp_flow_key_format(key
, key_len
, &s
);
2545 VLOG_DBG("%s:%s", title
, ds_cstr(&s
));
2550 odp_to_ovs_frag(uint8_t odp_frag
, struct flow
*flow
)
2552 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2554 if (odp_frag
> OVS_FRAG_TYPE_LATER
) {
2555 VLOG_ERR_RL(&rl
, "invalid frag %"PRIu8
" in flow key", odp_frag
);
2559 if (odp_frag
!= OVS_FRAG_TYPE_NONE
) {
2560 flow
->nw_frag
|= FLOW_NW_FRAG_ANY
;
2561 if (odp_frag
== OVS_FRAG_TYPE_LATER
) {
2562 flow
->nw_frag
|= FLOW_NW_FRAG_LATER
;
2569 parse_flow_nlattrs(const struct nlattr
*key
, size_t key_len
,
2570 const struct nlattr
*attrs
[], uint64_t *present_attrsp
,
2571 int *out_of_range_attrp
)
2573 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
2574 const struct nlattr
*nla
;
2575 uint64_t present_attrs
;
2578 BUILD_ASSERT(OVS_KEY_ATTR_MAX
< CHAR_BIT
* sizeof present_attrs
);
2580 *out_of_range_attrp
= 0;
2581 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
2582 uint16_t type
= nl_attr_type(nla
);
2583 size_t len
= nl_attr_get_size(nla
);
2584 int expected_len
= odp_flow_key_attr_len(type
);
2586 if (len
!= expected_len
&& expected_len
>= 0) {
2587 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
2589 VLOG_ERR_RL(&rl
, "attribute %s has length %zu but should have "
2590 "length %d", ovs_key_attr_to_string(type
, namebuf
,
2596 if (type
> OVS_KEY_ATTR_MAX
) {
2597 *out_of_range_attrp
= type
;
2599 if (present_attrs
& (UINT64_C(1) << type
)) {
2600 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
2602 VLOG_ERR_RL(&rl
, "duplicate %s attribute in flow key",
2603 ovs_key_attr_to_string(type
,
2604 namebuf
, sizeof namebuf
));
2608 present_attrs
|= UINT64_C(1) << type
;
2613 VLOG_ERR_RL(&rl
, "trailing garbage in flow key");
2617 *present_attrsp
= present_attrs
;
2621 static enum odp_key_fitness
2622 check_expectations(uint64_t present_attrs
, int out_of_range_attr
,
2623 uint64_t expected_attrs
,
2624 const struct nlattr
*key
, size_t key_len
)
2626 uint64_t missing_attrs
;
2627 uint64_t extra_attrs
;
2629 missing_attrs
= expected_attrs
& ~present_attrs
;
2630 if (missing_attrs
) {
2631 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
2632 log_odp_key_attributes(&rl
, "expected but not present",
2633 missing_attrs
, 0, key
, key_len
);
2634 return ODP_FIT_TOO_LITTLE
;
2637 extra_attrs
= present_attrs
& ~expected_attrs
;
2638 if (extra_attrs
|| out_of_range_attr
) {
2639 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
2640 log_odp_key_attributes(&rl
, "present but not expected",
2641 extra_attrs
, out_of_range_attr
, key
, key_len
);
2642 return ODP_FIT_TOO_MUCH
;
2645 return ODP_FIT_PERFECT
;
2649 parse_ethertype(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
2650 uint64_t present_attrs
, uint64_t *expected_attrs
,
2653 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2655 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
)) {
2656 flow
->dl_type
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_ETHERTYPE
]);
2657 if (ntohs(flow
->dl_type
) < 1536) {
2658 VLOG_ERR_RL(&rl
, "invalid Ethertype %"PRIu16
" in flow key",
2659 ntohs(flow
->dl_type
));
2662 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
;
2664 flow
->dl_type
= htons(FLOW_DL_TYPE_NONE
);
2669 static enum odp_key_fitness
2670 parse_l2_5_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
2671 uint64_t present_attrs
, int out_of_range_attr
,
2672 uint64_t expected_attrs
, struct flow
*flow
,
2673 const struct nlattr
*key
, size_t key_len
)
2675 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2677 if (eth_type_mpls(flow
->dl_type
)) {
2678 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_MPLS
);
2680 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
))) {
2681 return ODP_FIT_TOO_LITTLE
;
2683 flow
->mpls_lse
= nl_attr_get_be32(attrs
[OVS_KEY_ATTR_MPLS
]);
2685 } else if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
2686 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV4
;
2687 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
2688 const struct ovs_key_ipv4
*ipv4_key
;
2690 ipv4_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV4
]);
2691 flow
->nw_src
= ipv4_key
->ipv4_src
;
2692 flow
->nw_dst
= ipv4_key
->ipv4_dst
;
2693 flow
->nw_proto
= ipv4_key
->ipv4_proto
;
2694 flow
->nw_tos
= ipv4_key
->ipv4_tos
;
2695 flow
->nw_ttl
= ipv4_key
->ipv4_ttl
;
2696 if (!odp_to_ovs_frag(ipv4_key
->ipv4_frag
, flow
)) {
2697 return ODP_FIT_ERROR
;
2700 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
2701 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV6
;
2702 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
2703 const struct ovs_key_ipv6
*ipv6_key
;
2705 ipv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV6
]);
2706 memcpy(&flow
->ipv6_src
, ipv6_key
->ipv6_src
, sizeof flow
->ipv6_src
);
2707 memcpy(&flow
->ipv6_dst
, ipv6_key
->ipv6_dst
, sizeof flow
->ipv6_dst
);
2708 flow
->ipv6_label
= ipv6_key
->ipv6_label
;
2709 flow
->nw_proto
= ipv6_key
->ipv6_proto
;
2710 flow
->nw_tos
= ipv6_key
->ipv6_tclass
;
2711 flow
->nw_ttl
= ipv6_key
->ipv6_hlimit
;
2712 if (!odp_to_ovs_frag(ipv6_key
->ipv6_frag
, flow
)) {
2713 return ODP_FIT_ERROR
;
2716 } else if (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
2717 flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
2718 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ARP
;
2719 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ARP
)) {
2720 const struct ovs_key_arp
*arp_key
;
2722 arp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ARP
]);
2723 flow
->nw_src
= arp_key
->arp_sip
;
2724 flow
->nw_dst
= arp_key
->arp_tip
;
2725 if (arp_key
->arp_op
& htons(0xff00)) {
2726 VLOG_ERR_RL(&rl
, "unsupported ARP opcode %"PRIu16
" in flow "
2727 "key", ntohs(arp_key
->arp_op
));
2728 return ODP_FIT_ERROR
;
2730 flow
->nw_proto
= ntohs(arp_key
->arp_op
);
2731 memcpy(flow
->arp_sha
, arp_key
->arp_sha
, ETH_ADDR_LEN
);
2732 memcpy(flow
->arp_tha
, arp_key
->arp_tha
, ETH_ADDR_LEN
);
2736 if (flow
->nw_proto
== IPPROTO_TCP
2737 && (flow
->dl_type
== htons(ETH_TYPE_IP
) ||
2738 flow
->dl_type
== htons(ETH_TYPE_IPV6
))
2739 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
2740 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP
;
2741 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP
)) {
2742 const struct ovs_key_tcp
*tcp_key
;
2744 tcp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_TCP
]);
2745 flow
->tp_src
= tcp_key
->tcp_src
;
2746 flow
->tp_dst
= tcp_key
->tcp_dst
;
2748 } else if (flow
->nw_proto
== IPPROTO_UDP
2749 && (flow
->dl_type
== htons(ETH_TYPE_IP
) ||
2750 flow
->dl_type
== htons(ETH_TYPE_IPV6
))
2751 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
2752 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_UDP
;
2753 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_UDP
)) {
2754 const struct ovs_key_udp
*udp_key
;
2756 udp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_UDP
]);
2757 flow
->tp_src
= udp_key
->udp_src
;
2758 flow
->tp_dst
= udp_key
->udp_dst
;
2760 } else if (flow
->nw_proto
== IPPROTO_ICMP
2761 && flow
->dl_type
== htons(ETH_TYPE_IP
)
2762 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
2763 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMP
;
2764 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMP
)) {
2765 const struct ovs_key_icmp
*icmp_key
;
2767 icmp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMP
]);
2768 flow
->tp_src
= htons(icmp_key
->icmp_type
);
2769 flow
->tp_dst
= htons(icmp_key
->icmp_code
);
2771 } else if (flow
->nw_proto
== IPPROTO_ICMPV6
2772 && flow
->dl_type
== htons(ETH_TYPE_IPV6
)
2773 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
2774 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
;
2775 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
)) {
2776 const struct ovs_key_icmpv6
*icmpv6_key
;
2778 icmpv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMPV6
]);
2779 flow
->tp_src
= htons(icmpv6_key
->icmpv6_type
);
2780 flow
->tp_dst
= htons(icmpv6_key
->icmpv6_code
);
2782 if (flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
) ||
2783 flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
)) {
2784 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
2785 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ND
)) {
2786 const struct ovs_key_nd
*nd_key
;
2788 nd_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ND
]);
2789 memcpy(&flow
->nd_target
, nd_key
->nd_target
,
2790 sizeof flow
->nd_target
);
2791 memcpy(flow
->arp_sha
, nd_key
->nd_sll
, ETH_ADDR_LEN
);
2792 memcpy(flow
->arp_tha
, nd_key
->nd_tll
, ETH_ADDR_LEN
);
2798 return check_expectations(present_attrs
, out_of_range_attr
, expected_attrs
,
2802 /* Parse 802.1Q header then encapsulated L3 attributes. */
2803 static enum odp_key_fitness
2804 parse_8021q_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
2805 uint64_t present_attrs
, int out_of_range_attr
,
2806 uint64_t expected_attrs
, struct flow
*flow
,
2807 const struct nlattr
*key
, size_t key_len
)
2809 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2811 const struct nlattr
*encap
2812 = (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)
2813 ? attrs
[OVS_KEY_ATTR_ENCAP
] : NULL
);
2814 enum odp_key_fitness encap_fitness
;
2815 enum odp_key_fitness fitness
;
2818 /* Calculate fitness of outer attributes. */
2819 expected_attrs
|= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN
) |
2820 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
));
2821 fitness
= check_expectations(present_attrs
, out_of_range_attr
,
2822 expected_attrs
, key
, key_len
);
2824 /* Get the VLAN TCI value. */
2825 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
))) {
2826 return ODP_FIT_TOO_LITTLE
;
2828 tci
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
]);
2829 if (tci
== htons(0)) {
2830 /* Corner case for a truncated 802.1Q header. */
2831 if (fitness
== ODP_FIT_PERFECT
&& nl_attr_get_size(encap
)) {
2832 return ODP_FIT_TOO_MUCH
;
2835 } else if (!(tci
& htons(VLAN_CFI
))) {
2836 VLOG_ERR_RL(&rl
, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16
" is nonzero "
2837 "but CFI bit is not set", ntohs(tci
));
2838 return ODP_FIT_ERROR
;
2842 * Remove the TPID from dl_type since it's not the real Ethertype. */
2843 flow
->vlan_tci
= tci
;
2844 flow
->dl_type
= htons(0);
2846 /* Now parse the encapsulated attributes. */
2847 if (!parse_flow_nlattrs(nl_attr_get(encap
), nl_attr_get_size(encap
),
2848 attrs
, &present_attrs
, &out_of_range_attr
)) {
2849 return ODP_FIT_ERROR
;
2853 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
)) {
2854 return ODP_FIT_ERROR
;
2856 encap_fitness
= parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
2857 expected_attrs
, flow
, key
, key_len
);
2859 /* The overall fitness is the worse of the outer and inner attributes. */
2860 return MAX(fitness
, encap_fitness
);
2863 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
2864 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
2865 * 'key' fits our expectations for what a flow key should contain.
2867 * The 'in_port' will be the datapath's understanding of the port. The
2868 * caller will need to translate with odp_port_to_ofp_port() if the
2869 * OpenFlow port is needed.
2871 * This function doesn't take the packet itself as an argument because none of
2872 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
2873 * it is always possible to infer which additional attribute(s) should appear
2874 * by looking at the attributes for lower-level protocols, e.g. if the network
2875 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
2876 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
2877 * must be absent. */
2878 enum odp_key_fitness
2879 odp_flow_key_to_flow(const struct nlattr
*key
, size_t key_len
,
2882 const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1];
2883 uint64_t expected_attrs
;
2884 uint64_t present_attrs
;
2885 int out_of_range_attr
;
2887 memset(flow
, 0, sizeof *flow
);
2889 /* Parse attributes. */
2890 if (!parse_flow_nlattrs(key
, key_len
, attrs
, &present_attrs
,
2891 &out_of_range_attr
)) {
2892 return ODP_FIT_ERROR
;
2897 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
)) {
2898 flow
->skb_priority
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_PRIORITY
]);
2899 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
;
2902 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
)) {
2903 flow
->skb_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_SKB_MARK
]);
2904 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
;
2907 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
)) {
2908 enum odp_key_fitness res
;
2910 res
= odp_tun_key_from_attr(attrs
[OVS_KEY_ATTR_TUNNEL
], &flow
->tunnel
);
2911 if (res
== ODP_FIT_ERROR
) {
2912 return ODP_FIT_ERROR
;
2913 } else if (res
== ODP_FIT_PERFECT
) {
2914 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
;
2918 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
)) {
2919 flow
->in_port
.odp_port
2920 = nl_attr_get_odp_port(attrs
[OVS_KEY_ATTR_IN_PORT
]);
2921 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
;
2923 flow
->in_port
.odp_port
= ODPP_NONE
;
2926 /* Ethernet header. */
2927 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
)) {
2928 const struct ovs_key_ethernet
*eth_key
;
2930 eth_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ETHERNET
]);
2931 memcpy(flow
->dl_src
, eth_key
->eth_src
, ETH_ADDR_LEN
);
2932 memcpy(flow
->dl_dst
, eth_key
->eth_dst
, ETH_ADDR_LEN
);
2934 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
;
2936 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
2937 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
)) {
2938 return ODP_FIT_ERROR
;
2941 if (flow
->dl_type
== htons(ETH_TYPE_VLAN
)) {
2942 return parse_8021q_onward(attrs
, present_attrs
, out_of_range_attr
,
2943 expected_attrs
, flow
, key
, key_len
);
2945 return parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
2946 expected_attrs
, flow
, key
, key_len
);
2949 /* Returns 'fitness' as a string, for use in debug messages. */
2951 odp_key_fitness_to_string(enum odp_key_fitness fitness
)
2954 case ODP_FIT_PERFECT
:
2956 case ODP_FIT_TOO_MUCH
:
2958 case ODP_FIT_TOO_LITTLE
:
2959 return "too_little";
2967 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
2968 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
2969 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
2970 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
2971 * null, then the return value is not meaningful.) */
2973 odp_put_userspace_action(uint32_t pid
,
2974 const void *userdata
, size_t userdata_size
,
2975 struct ofpbuf
*odp_actions
)
2977 size_t userdata_ofs
;
2980 offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_USERSPACE
);
2981 nl_msg_put_u32(odp_actions
, OVS_USERSPACE_ATTR_PID
, pid
);
2983 userdata_ofs
= odp_actions
->size
+ NLA_HDRLEN
;
2984 nl_msg_put_unspec(odp_actions
, OVS_USERSPACE_ATTR_USERDATA
,
2985 userdata
, userdata_size
);
2989 nl_msg_end_nested(odp_actions
, offset
);
2991 return userdata_ofs
;
2995 odp_put_tunnel_action(const struct flow_tnl
*tunnel
,
2996 struct ofpbuf
*odp_actions
)
2998 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
2999 tun_key_to_attr(odp_actions
, tunnel
);
3000 nl_msg_end_nested(odp_actions
, offset
);
3003 /* The commit_odp_actions() function and its helpers. */
3006 commit_set_action(struct ofpbuf
*odp_actions
, enum ovs_key_attr key_type
,
3007 const void *key
, size_t key_size
)
3009 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
3010 nl_msg_put_unspec(odp_actions
, key_type
, key
, key_size
);
3011 nl_msg_end_nested(odp_actions
, offset
);
3015 odp_put_skb_mark_action(const uint32_t skb_mark
,
3016 struct ofpbuf
*odp_actions
)
3018 commit_set_action(odp_actions
, OVS_KEY_ATTR_SKB_MARK
, &skb_mark
,
3022 /* If any of the flow key data that ODP actions can modify are different in
3023 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
3024 * 'odp_actions' that change the flow tunneling information in key from
3025 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
3026 * same way. In other words, operates the same as commit_odp_actions(), but
3027 * only on tunneling information. */
3029 commit_odp_tunnel_action(const struct flow
*flow
, struct flow
*base
,
3030 struct ofpbuf
*odp_actions
)
3032 /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
3033 if (flow
->tunnel
.ip_dst
) {
3034 if (!memcmp(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
)) {
3037 memcpy(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
);
3038 odp_put_tunnel_action(&base
->tunnel
, odp_actions
);
3043 commit_set_ether_addr_action(const struct flow
*flow
, struct flow
*base
,
3044 struct ofpbuf
*odp_actions
,
3045 struct flow_wildcards
*wc
)
3047 struct ovs_key_ethernet eth_key
;
3049 if (eth_addr_equals(base
->dl_src
, flow
->dl_src
) &&
3050 eth_addr_equals(base
->dl_dst
, flow
->dl_dst
)) {
3054 memset(&wc
->masks
.dl_src
, 0xff, sizeof wc
->masks
.dl_src
);
3055 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
3057 memcpy(base
->dl_src
, flow
->dl_src
, ETH_ADDR_LEN
);
3058 memcpy(base
->dl_dst
, flow
->dl_dst
, ETH_ADDR_LEN
);
3060 memcpy(eth_key
.eth_src
, base
->dl_src
, ETH_ADDR_LEN
);
3061 memcpy(eth_key
.eth_dst
, base
->dl_dst
, ETH_ADDR_LEN
);
3063 commit_set_action(odp_actions
, OVS_KEY_ATTR_ETHERNET
,
3064 ð_key
, sizeof(eth_key
));
3068 commit_vlan_action(const struct flow
*flow
, struct flow
*base
,
3069 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
3071 if (base
->vlan_tci
== flow
->vlan_tci
) {
3075 memset(&wc
->masks
.vlan_tci
, 0xff, sizeof wc
->masks
.vlan_tci
);
3077 if (base
->vlan_tci
& htons(VLAN_CFI
)) {
3078 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_VLAN
);
3081 if (flow
->vlan_tci
& htons(VLAN_CFI
)) {
3082 struct ovs_action_push_vlan vlan
;
3084 vlan
.vlan_tpid
= htons(ETH_TYPE_VLAN
);
3085 vlan
.vlan_tci
= flow
->vlan_tci
;
3086 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
3087 &vlan
, sizeof vlan
);
3089 base
->vlan_tci
= flow
->vlan_tci
;
3093 commit_mpls_action(const struct flow
*flow
, struct flow
*base
,
3094 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
3096 if (flow
->mpls_lse
== base
->mpls_lse
&&
3097 flow
->mpls_depth
== base
->mpls_depth
) {
3101 memset(&wc
->masks
.mpls_lse
, 0xff, sizeof wc
->masks
.mpls_lse
);
3103 if (flow
->mpls_depth
< base
->mpls_depth
) {
3104 if (base
->mpls_depth
- flow
->mpls_depth
> 1) {
3105 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
3106 VLOG_WARN_RL(&rl
, "Multiple mpls_pop actions reduced to "
3107 " a single mpls_pop action");
3110 nl_msg_put_be16(odp_actions
, OVS_ACTION_ATTR_POP_MPLS
, flow
->dl_type
);
3111 } else if (flow
->mpls_depth
> base
->mpls_depth
) {
3112 struct ovs_action_push_mpls
*mpls
;
3114 if (flow
->mpls_depth
- base
->mpls_depth
> 1) {
3115 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
3116 VLOG_WARN_RL(&rl
, "Multiple mpls_push actions reduced to "
3117 " a single mpls_push action");
3120 mpls
= nl_msg_put_unspec_uninit(odp_actions
, OVS_ACTION_ATTR_PUSH_MPLS
,
3122 memset(mpls
, 0, sizeof *mpls
);
3123 mpls
->mpls_ethertype
= flow
->dl_type
;
3124 mpls
->mpls_lse
= flow
->mpls_lse
;
3126 struct ovs_key_mpls mpls_key
;
3128 mpls_key
.mpls_lse
= flow
->mpls_lse
;
3129 commit_set_action(odp_actions
, OVS_KEY_ATTR_MPLS
,
3130 &mpls_key
, sizeof(mpls_key
));
3133 base
->dl_type
= flow
->dl_type
;
3134 base
->mpls_lse
= flow
->mpls_lse
;
3135 base
->mpls_depth
= flow
->mpls_depth
;
3139 commit_set_ipv4_action(const struct flow
*flow
, struct flow
*base
,
3140 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
3142 struct ovs_key_ipv4 ipv4_key
;
3144 if (base
->nw_src
== flow
->nw_src
&&
3145 base
->nw_dst
== flow
->nw_dst
&&
3146 base
->nw_tos
== flow
->nw_tos
&&
3147 base
->nw_ttl
== flow
->nw_ttl
&&
3148 base
->nw_frag
== flow
->nw_frag
) {
3152 memset(&wc
->masks
.nw_src
, 0xff, sizeof wc
->masks
.nw_src
);
3153 memset(&wc
->masks
.nw_dst
, 0xff, sizeof wc
->masks
.nw_dst
);
3154 memset(&wc
->masks
.nw_tos
, 0xff, sizeof wc
->masks
.nw_tos
);
3155 memset(&wc
->masks
.nw_ttl
, 0xff, sizeof wc
->masks
.nw_ttl
);
3156 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
3157 memset(&wc
->masks
.nw_frag
, 0xff, sizeof wc
->masks
.nw_frag
);
3159 ipv4_key
.ipv4_src
= base
->nw_src
= flow
->nw_src
;
3160 ipv4_key
.ipv4_dst
= base
->nw_dst
= flow
->nw_dst
;
3161 ipv4_key
.ipv4_tos
= base
->nw_tos
= flow
->nw_tos
;
3162 ipv4_key
.ipv4_ttl
= base
->nw_ttl
= flow
->nw_ttl
;
3163 ipv4_key
.ipv4_proto
= base
->nw_proto
;
3164 ipv4_key
.ipv4_frag
= ovs_to_odp_frag(base
->nw_frag
);
3166 commit_set_action(odp_actions
, OVS_KEY_ATTR_IPV4
,
3167 &ipv4_key
, sizeof(ipv4_key
));
3171 commit_set_ipv6_action(const struct flow
*flow
, struct flow
*base
,
3172 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
3174 struct ovs_key_ipv6 ipv6_key
;
3176 if (ipv6_addr_equals(&base
->ipv6_src
, &flow
->ipv6_src
) &&
3177 ipv6_addr_equals(&base
->ipv6_dst
, &flow
->ipv6_dst
) &&
3178 base
->ipv6_label
== flow
->ipv6_label
&&
3179 base
->nw_tos
== flow
->nw_tos
&&
3180 base
->nw_ttl
== flow
->nw_ttl
&&
3181 base
->nw_frag
== flow
->nw_frag
) {
3185 memset(&wc
->masks
.ipv6_src
, 0xff, sizeof wc
->masks
.ipv6_src
);
3186 memset(&wc
->masks
.ipv6_dst
, 0xff, sizeof wc
->masks
.ipv6_dst
);
3187 memset(&wc
->masks
.ipv6_label
, 0xff, sizeof wc
->masks
.ipv6_label
);
3188 memset(&wc
->masks
.nw_tos
, 0xff, sizeof wc
->masks
.nw_tos
);
3189 memset(&wc
->masks
.nw_ttl
, 0xff, sizeof wc
->masks
.nw_ttl
);
3190 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
3191 memset(&wc
->masks
.nw_frag
, 0xff, sizeof wc
->masks
.nw_frag
);
3193 base
->ipv6_src
= flow
->ipv6_src
;
3194 memcpy(&ipv6_key
.ipv6_src
, &base
->ipv6_src
, sizeof(ipv6_key
.ipv6_src
));
3195 base
->ipv6_dst
= flow
->ipv6_dst
;
3196 memcpy(&ipv6_key
.ipv6_dst
, &base
->ipv6_dst
, sizeof(ipv6_key
.ipv6_dst
));
3198 ipv6_key
.ipv6_label
= base
->ipv6_label
= flow
->ipv6_label
;
3199 ipv6_key
.ipv6_tclass
= base
->nw_tos
= flow
->nw_tos
;
3200 ipv6_key
.ipv6_hlimit
= base
->nw_ttl
= flow
->nw_ttl
;
3201 ipv6_key
.ipv6_proto
= base
->nw_proto
;
3202 ipv6_key
.ipv6_frag
= ovs_to_odp_frag(base
->nw_frag
);
3204 commit_set_action(odp_actions
, OVS_KEY_ATTR_IPV6
,
3205 &ipv6_key
, sizeof(ipv6_key
));
3209 commit_set_nw_action(const struct flow
*flow
, struct flow
*base
,
3210 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
3212 /* Check if flow really have an IP header. */
3213 if (!flow
->nw_proto
) {
3217 if (base
->dl_type
== htons(ETH_TYPE_IP
)) {
3218 commit_set_ipv4_action(flow
, base
, odp_actions
, wc
);
3219 } else if (base
->dl_type
== htons(ETH_TYPE_IPV6
)) {
3220 commit_set_ipv6_action(flow
, base
, odp_actions
, wc
);
3225 commit_set_port_action(const struct flow
*flow
, struct flow
*base
,
3226 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
3228 if (!is_ip_any(base
) || (!base
->tp_src
&& !base
->tp_dst
)) {
3232 if (base
->tp_src
== flow
->tp_src
&&
3233 base
->tp_dst
== flow
->tp_dst
) {
3237 memset(&wc
->masks
.tp_src
, 0xff, sizeof wc
->masks
.tp_src
);
3238 memset(&wc
->masks
.tp_dst
, 0xff, sizeof wc
->masks
.tp_dst
);
3240 if (flow
->nw_proto
== IPPROTO_TCP
) {
3241 struct ovs_key_tcp port_key
;
3243 port_key
.tcp_src
= base
->tp_src
= flow
->tp_src
;
3244 port_key
.tcp_dst
= base
->tp_dst
= flow
->tp_dst
;
3246 commit_set_action(odp_actions
, OVS_KEY_ATTR_TCP
,
3247 &port_key
, sizeof(port_key
));
3249 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
3250 struct ovs_key_udp port_key
;
3252 port_key
.udp_src
= base
->tp_src
= flow
->tp_src
;
3253 port_key
.udp_dst
= base
->tp_dst
= flow
->tp_dst
;
3255 commit_set_action(odp_actions
, OVS_KEY_ATTR_UDP
,
3256 &port_key
, sizeof(port_key
));
3261 commit_set_priority_action(const struct flow
*flow
, struct flow
*base
,
3262 struct ofpbuf
*odp_actions
,
3263 struct flow_wildcards
*wc
)
3265 if (base
->skb_priority
== flow
->skb_priority
) {
3269 memset(&wc
->masks
.skb_priority
, 0xff, sizeof wc
->masks
.skb_priority
);
3270 base
->skb_priority
= flow
->skb_priority
;
3272 commit_set_action(odp_actions
, OVS_KEY_ATTR_PRIORITY
,
3273 &base
->skb_priority
, sizeof(base
->skb_priority
));
3277 commit_set_skb_mark_action(const struct flow
*flow
, struct flow
*base
,
3278 struct ofpbuf
*odp_actions
,
3279 struct flow_wildcards
*wc
)
3281 if (base
->skb_mark
== flow
->skb_mark
) {
3285 memset(&wc
->masks
.skb_mark
, 0xff, sizeof wc
->masks
.skb_mark
);
3286 base
->skb_mark
= flow
->skb_mark
;
3288 odp_put_skb_mark_action(base
->skb_mark
, odp_actions
);
3290 /* If any of the flow key data that ODP actions can modify are different in
3291 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
3292 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
3293 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
3294 * in addition to this function if needed. Sets fields in 'wc' that are
3295 * used as part of the action. */
3297 commit_odp_actions(const struct flow
*flow
, struct flow
*base
,
3298 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
3300 commit_set_ether_addr_action(flow
, base
, odp_actions
, wc
);
3301 commit_vlan_action(flow
, base
, odp_actions
, wc
);
3302 commit_set_nw_action(flow
, base
, odp_actions
, wc
);
3303 commit_set_port_action(flow
, base
, odp_actions
, wc
);
3304 /* Committing MPLS actions should occur after committing nw and port
3305 * actions. This is because committing MPLS actions may alter a packet so
3306 * that it is no longer IP and thus nw and port actions are no longer valid.
3308 commit_mpls_action(flow
, base
, odp_actions
, wc
);
3309 commit_set_priority_action(flow
, base
, odp_actions
, wc
);
3310 commit_set_skb_mark_action(flow
, base
, odp_actions
, wc
);