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
];
938 if (ma
&& odp_mask_attr_is_exact(ma
)) {
942 ds_put_cstr(ds
, ovs_key_attr_to_string(attr
, namebuf
, sizeof namebuf
));
945 expected_len
= odp_flow_key_attr_len(nl_attr_type(a
));
946 if (expected_len
!= -2) {
947 bool bad_key_len
= nl_attr_get_size(a
) != expected_len
;
948 bool bad_mask_len
= ma
&& nl_attr_get_size(a
) != expected_len
;
950 if (bad_key_len
|| bad_mask_len
) {
952 ds_put_format(ds
, "(bad key length %zu, expected %d)(",
954 odp_flow_key_attr_len(nl_attr_type(a
)));
956 format_generic_odp_key(a
, ds
);
958 ds_put_char(ds
, '/');
959 ds_put_format(ds
, "(bad mask length %zu, expected %d)(",
960 nl_attr_get_size(ma
),
961 odp_flow_key_attr_len(nl_attr_type(ma
)));
963 format_generic_odp_key(ma
, ds
);
964 ds_put_char(ds
, ')');
970 ds_put_char(ds
, '(');
972 case OVS_KEY_ATTR_ENCAP
:
973 if (ma
&& nl_attr_get_size(ma
) && nl_attr_get_size(a
)) {
974 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
),
975 nl_attr_get(ma
), nl_attr_get_size(ma
), ds
);
976 } else if (nl_attr_get_size(a
)) {
977 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
), NULL
, 0, ds
);
981 case OVS_KEY_ATTR_PRIORITY
:
982 case OVS_KEY_ATTR_SKB_MARK
:
983 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
985 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
989 case OVS_KEY_ATTR_TUNNEL
:
990 memset(&tun_key
, 0, sizeof tun_key
);
991 if (odp_tun_key_from_attr(a
, &tun_key
) == ODP_FIT_ERROR
) {
992 ds_put_format(ds
, "error");
994 struct flow_tnl tun_mask
;
996 memset(&tun_mask
, 0, sizeof tun_mask
);
997 odp_tun_key_from_attr(ma
, &tun_mask
);
998 ds_put_format(ds
, "tun_id=%#"PRIx64
"/%#"PRIx64
999 ",src="IP_FMT
"/"IP_FMT
",dst="IP_FMT
"/"IP_FMT
1000 ",tos=%#"PRIx8
"/%#"PRIx8
",ttl=%"PRIu8
"/%#"PRIx8
1002 ntohll(tun_key
.tun_id
), ntohll(tun_mask
.tun_id
),
1003 IP_ARGS(tun_key
.ip_src
), IP_ARGS(tun_mask
.ip_src
),
1004 IP_ARGS(tun_key
.ip_dst
), IP_ARGS(tun_mask
.ip_dst
),
1005 tun_key
.ip_tos
, tun_mask
.ip_tos
,
1006 tun_key
.ip_ttl
, tun_mask
.ip_ttl
);
1008 format_flags(ds
, flow_tun_flag_to_string
, tun_key
.flags
, ',');
1010 /* XXX This code is correct, but enabling it would break the unit
1011 test. Disable it for now until the input parser is fixed.
1013 ds_put_char(ds, '/');
1014 format_flags(ds, flow_tun_flag_to_string, tun_mask.flags, ',');
1016 ds_put_char(ds
, ')');
1018 ds_put_format(ds
, "tun_id=0x%"PRIx64
",src="IP_FMT
",dst="IP_FMT
","
1019 "tos=0x%"PRIx8
",ttl=%"PRIu8
",flags(",
1020 ntohll(tun_key
.tun_id
),
1021 IP_ARGS(tun_key
.ip_src
),
1022 IP_ARGS(tun_key
.ip_dst
),
1023 tun_key
.ip_tos
, tun_key
.ip_ttl
);
1025 format_flags(ds
, flow_tun_flag_to_string
, tun_key
.flags
, ',');
1026 ds_put_char(ds
, ')');
1030 case OVS_KEY_ATTR_IN_PORT
:
1031 ds_put_format(ds
, "%"PRIu32
, nl_attr_get_u32(a
));
1033 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
1037 case OVS_KEY_ATTR_ETHERNET
:
1039 const struct ovs_key_ethernet
*eth_mask
= nl_attr_get(ma
);
1040 const struct ovs_key_ethernet
*eth_key
= nl_attr_get(a
);
1042 ds_put_format(ds
, "src="ETH_ADDR_FMT
"/"ETH_ADDR_FMT
1043 ",dst="ETH_ADDR_FMT
"/"ETH_ADDR_FMT
,
1044 ETH_ADDR_ARGS(eth_key
->eth_src
),
1045 ETH_ADDR_ARGS(eth_mask
->eth_src
),
1046 ETH_ADDR_ARGS(eth_key
->eth_dst
),
1047 ETH_ADDR_ARGS(eth_mask
->eth_dst
));
1049 const struct ovs_key_ethernet
*eth_key
= nl_attr_get(a
);
1051 ds_put_format(ds
, "src="ETH_ADDR_FMT
",dst="ETH_ADDR_FMT
,
1052 ETH_ADDR_ARGS(eth_key
->eth_src
),
1053 ETH_ADDR_ARGS(eth_key
->eth_dst
));
1057 case OVS_KEY_ATTR_VLAN
:
1059 ovs_be16 vlan_tci
= nl_attr_get_be16(a
);
1061 ovs_be16 mask
= nl_attr_get_be16(ma
);
1062 ds_put_format(ds
, "vid=%"PRIu16
"/%"PRIx16
",pcp=%d/0x%x,cfi=%d/%d",
1063 vlan_tci_to_vid(vlan_tci
),
1064 vlan_tci_to_vid(mask
),
1065 vlan_tci_to_pcp(vlan_tci
),
1066 vlan_tci_to_vid(mask
),
1067 vlan_tci_to_cfi(vlan_tci
),
1068 vlan_tci_to_cfi(mask
));
1070 format_vlan_tci(ds
, vlan_tci
);
1075 case OVS_KEY_ATTR_MPLS
: {
1076 const struct ovs_key_mpls
*mpls_key
= nl_attr_get(a
);
1077 const struct ovs_key_mpls
*mpls_mask
= NULL
;
1079 mpls_mask
= nl_attr_get(ma
);
1081 format_mpls(ds
, mpls_key
, mpls_mask
);
1085 case OVS_KEY_ATTR_ETHERTYPE
:
1086 ds_put_format(ds
, "0x%04"PRIx16
, ntohs(nl_attr_get_be16(a
)));
1088 ds_put_format(ds
, "/0x%04"PRIx16
, ntohs(nl_attr_get_be16(ma
)));
1092 case OVS_KEY_ATTR_IPV4
:
1094 const struct ovs_key_ipv4
*ipv4_key
= nl_attr_get(a
);
1095 const struct ovs_key_ipv4
*ipv4_mask
= nl_attr_get(ma
);
1097 ds_put_format(ds
, "src="IP_FMT
"/"IP_FMT
",dst="IP_FMT
"/"IP_FMT
1098 ",proto=%"PRIu8
"/%#"PRIx8
",tos=%#"PRIx8
"/%#"PRIx8
1099 ",ttl=%"PRIu8
"/%#"PRIx8
",frag=%s/%#"PRIx8
,
1100 IP_ARGS(ipv4_key
->ipv4_src
),
1101 IP_ARGS(ipv4_mask
->ipv4_src
),
1102 IP_ARGS(ipv4_key
->ipv4_dst
),
1103 IP_ARGS(ipv4_mask
->ipv4_dst
),
1104 ipv4_key
->ipv4_proto
, ipv4_mask
->ipv4_proto
,
1105 ipv4_key
->ipv4_tos
, ipv4_mask
->ipv4_tos
,
1106 ipv4_key
->ipv4_ttl
, ipv4_mask
->ipv4_ttl
,
1107 ovs_frag_type_to_string(ipv4_key
->ipv4_frag
),
1108 ipv4_mask
->ipv4_frag
);
1110 const struct ovs_key_ipv4
*ipv4_key
= nl_attr_get(a
);
1112 ds_put_format(ds
, "src="IP_FMT
",dst="IP_FMT
",proto=%"PRIu8
1113 ",tos=%#"PRIx8
",ttl=%"PRIu8
",frag=%s",
1114 IP_ARGS(ipv4_key
->ipv4_src
),
1115 IP_ARGS(ipv4_key
->ipv4_dst
),
1116 ipv4_key
->ipv4_proto
, ipv4_key
->ipv4_tos
,
1118 ovs_frag_type_to_string(ipv4_key
->ipv4_frag
));
1122 case OVS_KEY_ATTR_IPV6
:
1124 const struct ovs_key_ipv6
*ipv6_key
, *ipv6_mask
;
1125 char src_str
[INET6_ADDRSTRLEN
];
1126 char dst_str
[INET6_ADDRSTRLEN
];
1127 char src_mask
[INET6_ADDRSTRLEN
];
1128 char dst_mask
[INET6_ADDRSTRLEN
];
1130 ipv6_key
= nl_attr_get(a
);
1131 inet_ntop(AF_INET6
, ipv6_key
->ipv6_src
, src_str
, sizeof src_str
);
1132 inet_ntop(AF_INET6
, ipv6_key
->ipv6_dst
, dst_str
, sizeof dst_str
);
1134 ipv6_mask
= nl_attr_get(ma
);
1135 inet_ntop(AF_INET6
, ipv6_mask
->ipv6_src
, src_mask
, sizeof src_mask
);
1136 inet_ntop(AF_INET6
, ipv6_mask
->ipv6_dst
, dst_mask
, sizeof dst_mask
);
1138 ds_put_format(ds
, "src=%s/%s,dst=%s/%s,label=%#"PRIx32
"/%#"PRIx32
1139 ",proto=%"PRIu8
"/%#"PRIx8
",tclass=%#"PRIx8
"/%#"PRIx8
1140 ",hlimit=%"PRIu8
"/%#"PRIx8
",frag=%s/%#"PRIx8
,
1141 src_str
, src_mask
, dst_str
, dst_mask
,
1142 ntohl(ipv6_key
->ipv6_label
),
1143 ntohl(ipv6_mask
->ipv6_label
),
1144 ipv6_key
->ipv6_proto
, ipv6_mask
->ipv6_proto
,
1145 ipv6_key
->ipv6_tclass
, ipv6_mask
->ipv6_tclass
,
1146 ipv6_key
->ipv6_hlimit
, ipv6_mask
->ipv6_hlimit
,
1147 ovs_frag_type_to_string(ipv6_key
->ipv6_frag
),
1148 ipv6_mask
->ipv6_frag
);
1150 const struct ovs_key_ipv6
*ipv6_key
;
1151 char src_str
[INET6_ADDRSTRLEN
];
1152 char dst_str
[INET6_ADDRSTRLEN
];
1154 ipv6_key
= nl_attr_get(a
);
1155 inet_ntop(AF_INET6
, ipv6_key
->ipv6_src
, src_str
, sizeof src_str
);
1156 inet_ntop(AF_INET6
, ipv6_key
->ipv6_dst
, dst_str
, sizeof dst_str
);
1158 ds_put_format(ds
, "src=%s,dst=%s,label=%#"PRIx32
",proto=%"PRIu8
1159 ",tclass=%#"PRIx8
",hlimit=%"PRIu8
",frag=%s",
1160 src_str
, dst_str
, ntohl(ipv6_key
->ipv6_label
),
1161 ipv6_key
->ipv6_proto
, ipv6_key
->ipv6_tclass
,
1162 ipv6_key
->ipv6_hlimit
,
1163 ovs_frag_type_to_string(ipv6_key
->ipv6_frag
));
1167 case OVS_KEY_ATTR_TCP
:
1169 const struct ovs_key_tcp
*tcp_mask
= nl_attr_get(ma
);
1170 const struct ovs_key_tcp
*tcp_key
= nl_attr_get(a
);
1172 ds_put_format(ds
, "src=%"PRIu16
"/%#"PRIx16
1173 ",dst=%"PRIu16
"/%#"PRIx16
,
1174 ntohs(tcp_key
->tcp_src
), ntohs(tcp_mask
->tcp_src
),
1175 ntohs(tcp_key
->tcp_dst
), ntohs(tcp_mask
->tcp_dst
));
1177 const struct ovs_key_tcp
*tcp_key
= nl_attr_get(a
);
1179 ds_put_format(ds
, "src=%"PRIu16
",dst=%"PRIu16
,
1180 ntohs(tcp_key
->tcp_src
), ntohs(tcp_key
->tcp_dst
));
1184 case OVS_KEY_ATTR_UDP
:
1186 const struct ovs_key_udp
*udp_mask
= nl_attr_get(ma
);
1187 const struct ovs_key_udp
*udp_key
= nl_attr_get(a
);
1189 ds_put_format(ds
, "src=%"PRIu16
"/%#"PRIx16
1190 ",dst=%"PRIu16
"/%#"PRIx16
,
1191 ntohs(udp_key
->udp_src
), ntohs(udp_mask
->udp_src
),
1192 ntohs(udp_key
->udp_dst
), ntohs(udp_mask
->udp_dst
));
1194 const struct ovs_key_udp
*udp_key
= nl_attr_get(a
);
1196 ds_put_format(ds
, "src=%"PRIu16
",dst=%"PRIu16
,
1197 ntohs(udp_key
->udp_src
), ntohs(udp_key
->udp_dst
));
1201 case OVS_KEY_ATTR_ICMP
:
1203 const struct ovs_key_icmp
*icmp_mask
= nl_attr_get(ma
);
1204 const struct ovs_key_icmp
*icmp_key
= nl_attr_get(a
);
1206 ds_put_format(ds
, "type=%"PRIu8
"/%#"PRIx8
",code=%"PRIu8
"/%#"PRIx8
,
1207 icmp_key
->icmp_type
, icmp_mask
->icmp_type
,
1208 icmp_key
->icmp_code
, icmp_mask
->icmp_code
);
1210 const struct ovs_key_icmp
*icmp_key
= nl_attr_get(a
);
1212 ds_put_format(ds
, "type=%"PRIu8
",code=%"PRIu8
,
1213 icmp_key
->icmp_type
, icmp_key
->icmp_code
);
1217 case OVS_KEY_ATTR_ICMPV6
:
1219 const struct ovs_key_icmpv6
*icmpv6_mask
= nl_attr_get(ma
);
1220 const struct ovs_key_icmpv6
*icmpv6_key
= nl_attr_get(a
);
1222 ds_put_format(ds
, "type=%"PRIu8
"/%#"PRIx8
",code=%"PRIu8
"/%#"PRIx8
,
1223 icmpv6_key
->icmpv6_type
, icmpv6_mask
->icmpv6_type
,
1224 icmpv6_key
->icmpv6_code
, icmpv6_mask
->icmpv6_code
);
1226 const struct ovs_key_icmpv6
*icmpv6_key
= nl_attr_get(a
);
1228 ds_put_format(ds
, "type=%"PRIu8
",code=%"PRIu8
,
1229 icmpv6_key
->icmpv6_type
, icmpv6_key
->icmpv6_code
);
1233 case OVS_KEY_ATTR_ARP
:
1235 const struct ovs_key_arp
*arp_mask
= nl_attr_get(ma
);
1236 const struct ovs_key_arp
*arp_key
= nl_attr_get(a
);
1238 ds_put_format(ds
, "sip="IP_FMT
"/"IP_FMT
",tip="IP_FMT
"/"IP_FMT
1239 ",op=%"PRIu16
"/%#"PRIx16
1240 ",sha="ETH_ADDR_FMT
"/"ETH_ADDR_FMT
1241 ",tha="ETH_ADDR_FMT
"/"ETH_ADDR_FMT
,
1242 IP_ARGS(arp_key
->arp_sip
),
1243 IP_ARGS(arp_mask
->arp_sip
),
1244 IP_ARGS(arp_key
->arp_tip
),
1245 IP_ARGS(arp_mask
->arp_tip
),
1246 ntohs(arp_key
->arp_op
), ntohs(arp_mask
->arp_op
),
1247 ETH_ADDR_ARGS(arp_key
->arp_sha
),
1248 ETH_ADDR_ARGS(arp_mask
->arp_sha
),
1249 ETH_ADDR_ARGS(arp_key
->arp_tha
),
1250 ETH_ADDR_ARGS(arp_mask
->arp_tha
));
1252 const struct ovs_key_arp
*arp_key
= nl_attr_get(a
);
1254 ds_put_format(ds
, "sip="IP_FMT
",tip="IP_FMT
",op=%"PRIu16
","
1255 "sha="ETH_ADDR_FMT
",tha="ETH_ADDR_FMT
,
1256 IP_ARGS(arp_key
->arp_sip
), IP_ARGS(arp_key
->arp_tip
),
1257 ntohs(arp_key
->arp_op
),
1258 ETH_ADDR_ARGS(arp_key
->arp_sha
),
1259 ETH_ADDR_ARGS(arp_key
->arp_tha
));
1263 case OVS_KEY_ATTR_ND
: {
1264 const struct ovs_key_nd
*nd_key
, *nd_mask
;
1265 char target
[INET6_ADDRSTRLEN
];
1267 nd_key
= nl_attr_get(a
);
1268 nd_mask
= ma
? nl_attr_get(ma
) : NULL
;
1270 inet_ntop(AF_INET6
, nd_key
->nd_target
, target
, sizeof target
);
1271 ds_put_format(ds
, "target=%s", target
);
1273 inet_ntop(AF_INET6
, nd_mask
->nd_target
, target
, sizeof target
);
1274 ds_put_format(ds
, "/%s", target
);
1277 if (!eth_addr_is_zero(nd_key
->nd_sll
)) {
1278 ds_put_format(ds
, ",sll="ETH_ADDR_FMT
,
1279 ETH_ADDR_ARGS(nd_key
->nd_sll
));
1281 ds_put_format(ds
, "/"ETH_ADDR_FMT
,
1282 ETH_ADDR_ARGS(nd_mask
->nd_sll
));
1285 if (!eth_addr_is_zero(nd_key
->nd_tll
)) {
1286 ds_put_format(ds
, ",tll="ETH_ADDR_FMT
,
1287 ETH_ADDR_ARGS(nd_key
->nd_tll
));
1289 ds_put_format(ds
, "/"ETH_ADDR_FMT
,
1290 ETH_ADDR_ARGS(nd_mask
->nd_tll
));
1296 case OVS_KEY_ATTR_UNSPEC
:
1297 case __OVS_KEY_ATTR_MAX
:
1299 format_generic_odp_key(a
, ds
);
1301 ds_put_char(ds
, '/');
1302 format_generic_odp_key(ma
, ds
);
1306 ds_put_char(ds
, ')');
1309 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1310 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
1311 * 'mask_len' bytes of 'mask' which apply to 'key'. */
1313 odp_flow_format(const struct nlattr
*key
, size_t key_len
,
1314 const struct nlattr
*mask
, size_t mask_len
,
1318 const struct nlattr
*a
;
1320 bool has_ethtype_key
= false;
1321 const struct nlattr
*ma
= NULL
;
1323 NL_ATTR_FOR_EACH (a
, left
, key
, key_len
) {
1325 ds_put_char(ds
, ',');
1327 if (nl_attr_type(a
) == OVS_KEY_ATTR_ETHERTYPE
) {
1328 has_ethtype_key
= true;
1330 if (mask
&& mask_len
) {
1331 ma
= nl_attr_find__(mask
, mask_len
, nl_attr_type(a
));
1333 format_odp_key_attr(a
, ma
, ds
);
1338 if (left
== key_len
) {
1339 ds_put_cstr(ds
, "<empty>");
1341 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
1342 for (i
= 0; i
< left
; i
++) {
1343 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
1345 ds_put_char(ds
, ')');
1347 if (!has_ethtype_key
) {
1348 ma
= nl_attr_find__(mask
, mask_len
, OVS_KEY_ATTR_ETHERTYPE
);
1350 ds_put_format(ds
, ",eth_type(0/0x%04"PRIx16
")",
1351 ntohs(nl_attr_get_be16(ma
)));
1355 ds_put_cstr(ds
, "<empty>");
1359 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1360 * OVS_KEY_ATTR_* attributes in 'key'. */
1362 odp_flow_key_format(const struct nlattr
*key
,
1363 size_t key_len
, struct ds
*ds
)
1365 odp_flow_format(key
, key_len
, NULL
, 0, ds
);
1369 put_nd_key(int n
, const char *nd_target_s
,
1370 const uint8_t *nd_sll
, const uint8_t *nd_tll
, struct ofpbuf
*key
)
1372 struct ovs_key_nd nd_key
;
1374 memset(&nd_key
, 0, sizeof nd_key
);
1375 if (inet_pton(AF_INET6
, nd_target_s
, nd_key
.nd_target
) != 1) {
1379 memcpy(nd_key
.nd_sll
, nd_sll
, ETH_ADDR_LEN
);
1382 memcpy(nd_key
.nd_tll
, nd_tll
, ETH_ADDR_LEN
);
1384 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ND
, &nd_key
, sizeof nd_key
);
1389 ovs_frag_type_from_string(const char *s
, enum ovs_frag_type
*type
)
1391 if (!strcasecmp(s
, "no")) {
1392 *type
= OVS_FRAG_TYPE_NONE
;
1393 } else if (!strcasecmp(s
, "first")) {
1394 *type
= OVS_FRAG_TYPE_FIRST
;
1395 } else if (!strcasecmp(s
, "later")) {
1396 *type
= OVS_FRAG_TYPE_LATER
;
1404 mpls_lse_from_components(int mpls_label
, int mpls_tc
, int mpls_ttl
, int mpls_bos
)
1406 return (htonl((mpls_label
<< MPLS_LABEL_SHIFT
) |
1407 (mpls_tc
<< MPLS_TC_SHIFT
) |
1408 (mpls_ttl
<< MPLS_TTL_SHIFT
) |
1409 (mpls_bos
<< MPLS_BOS_SHIFT
)));
1413 parse_odp_key_mask_attr(const char *s
, const struct simap
*port_names
,
1414 struct ofpbuf
*key
, struct ofpbuf
*mask
)
1416 /* Many of the sscanf calls in this function use oversized destination
1417 * fields because some sscanf() implementations truncate the range of %i
1418 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
1419 * value of 0x7fff. The other alternatives are to allow only a single
1420 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
1423 * The tun_id parser has to use an alternative approach because there is no
1424 * type larger than 64 bits. */
1427 unsigned long long int priority
;
1428 unsigned long long int priority_mask
;
1431 if (mask
&& sscanf(s
, "skb_priority(%lli/%lli)%n", &priority
,
1432 &priority_mask
, &n
) > 0 && n
> 0) {
1433 nl_msg_put_u32(key
, OVS_KEY_ATTR_PRIORITY
, priority
);
1434 nl_msg_put_u32(mask
, OVS_KEY_ATTR_PRIORITY
, priority_mask
);
1436 } else if (sscanf(s
, "skb_priority(%lli)%n",
1437 &priority
, &n
) > 0 && n
> 0) {
1438 nl_msg_put_u32(key
, OVS_KEY_ATTR_PRIORITY
, priority
);
1440 nl_msg_put_u32(mask
, OVS_KEY_ATTR_PRIORITY
, UINT32_MAX
);
1447 unsigned long long int mark
;
1448 unsigned long long int mark_mask
;
1451 if (mask
&& sscanf(s
, "skb_mark(%lli/%lli)%n", &mark
,
1452 &mark_mask
, &n
) > 0 && n
> 0) {
1453 nl_msg_put_u32(key
, OVS_KEY_ATTR_SKB_MARK
, mark
);
1454 nl_msg_put_u32(mask
, OVS_KEY_ATTR_SKB_MARK
, mark_mask
);
1456 } else if (sscanf(s
, "skb_mark(%lli)%n", &mark
, &n
) > 0 && n
> 0) {
1457 nl_msg_put_u32(key
, OVS_KEY_ATTR_SKB_MARK
, mark
);
1459 nl_msg_put_u32(mask
, OVS_KEY_ATTR_SKB_MARK
, UINT32_MAX
);
1467 int tos
, tos_mask
, ttl
, ttl_mask
;
1468 struct flow_tnl tun_key
, tun_key_mask
;
1469 unsigned long long tun_id_mask
;
1472 if (mask
&& sscanf(s
, "tunnel(tun_id=%31[x0123456789abcdefABCDEF]/%llx,"
1473 "src="IP_SCAN_FMT
"/"IP_SCAN_FMT
",dst="IP_SCAN_FMT
1474 "/"IP_SCAN_FMT
",tos=%i/%i,ttl=%i/%i,flags%n",
1475 tun_id_s
, &tun_id_mask
,
1476 IP_SCAN_ARGS(&tun_key
.ip_src
),
1477 IP_SCAN_ARGS(&tun_key_mask
.ip_src
),
1478 IP_SCAN_ARGS(&tun_key
.ip_dst
),
1479 IP_SCAN_ARGS(&tun_key_mask
.ip_dst
),
1480 &tos
, &tos_mask
, &ttl
, &ttl_mask
,
1485 tun_key
.tun_id
= htonll(strtoull(tun_id_s
, NULL
, 0));
1486 tun_key_mask
.tun_id
= htonll(tun_id_mask
);
1487 tun_key
.ip_tos
= tos
;
1488 tun_key_mask
.ip_tos
= tos_mask
;
1489 tun_key
.ip_ttl
= ttl
;
1490 tun_key_mask
.ip_ttl
= ttl_mask
;
1491 res
= parse_flags(&s
[n
], flow_tun_flag_to_string
, &flags
);
1492 tun_key
.flags
= flags
;
1493 tun_key_mask
.flags
= UINT16_MAX
;
1503 tun_key_to_attr(key
, &tun_key
);
1505 tun_key_to_attr(mask
, &tun_key_mask
);
1508 } else if (sscanf(s
, "tunnel(tun_id=%31[x0123456789abcdefABCDEF],"
1509 "src="IP_SCAN_FMT
",dst="IP_SCAN_FMT
1510 ",tos=%i,ttl=%i,flags%n", tun_id_s
,
1511 IP_SCAN_ARGS(&tun_key
.ip_src
),
1512 IP_SCAN_ARGS(&tun_key
.ip_dst
), &tos
, &ttl
,
1517 tun_key
.tun_id
= htonll(strtoull(tun_id_s
, NULL
, 0));
1518 tun_key
.ip_tos
= tos
;
1519 tun_key
.ip_ttl
= ttl
;
1520 res
= parse_flags(&s
[n
], flow_tun_flag_to_string
, &flags
);
1521 tun_key
.flags
= flags
;
1531 tun_key_to_attr(key
, &tun_key
);
1534 memset(&tun_key
, 0xff, sizeof tun_key
);
1535 tun_key_to_attr(mask
, &tun_key
);
1542 unsigned long long int in_port
;
1543 unsigned long long int in_port_mask
;
1546 if (mask
&& sscanf(s
, "in_port(%lli/%lli)%n", &in_port
,
1547 &in_port_mask
, &n
) > 0 && n
> 0) {
1548 nl_msg_put_u32(key
, OVS_KEY_ATTR_IN_PORT
, in_port
);
1549 nl_msg_put_u32(mask
, OVS_KEY_ATTR_IN_PORT
, in_port_mask
);
1551 } else if (sscanf(s
, "in_port(%lli)%n", &in_port
, &n
) > 0 && n
> 0) {
1552 nl_msg_put_u32(key
, OVS_KEY_ATTR_IN_PORT
, in_port
);
1554 nl_msg_put_u32(mask
, OVS_KEY_ATTR_IN_PORT
, UINT32_MAX
);
1561 if (port_names
&& !strncmp(s
, "in_port(", 8)) {
1563 const struct simap_node
*node
;
1567 name_len
= strcspn(s
, ")");
1568 node
= simap_find_len(port_names
, name
, name_len
);
1570 nl_msg_put_u32(key
, OVS_KEY_ATTR_IN_PORT
, node
->data
);
1573 nl_msg_put_u32(mask
, OVS_KEY_ATTR_IN_PORT
, UINT32_MAX
);
1575 return 8 + name_len
+ 1;
1580 struct ovs_key_ethernet eth_key
;
1581 struct ovs_key_ethernet eth_key_mask
;
1584 if (mask
&& sscanf(s
,
1585 "eth(src="ETH_ADDR_SCAN_FMT
"/"ETH_ADDR_SCAN_FMT
","
1586 "dst="ETH_ADDR_SCAN_FMT
"/"ETH_ADDR_SCAN_FMT
")%n",
1587 ETH_ADDR_SCAN_ARGS(eth_key
.eth_src
),
1588 ETH_ADDR_SCAN_ARGS(eth_key_mask
.eth_src
),
1589 ETH_ADDR_SCAN_ARGS(eth_key
.eth_dst
),
1590 ETH_ADDR_SCAN_ARGS(eth_key_mask
.eth_dst
), &n
) > 0 && n
> 0) {
1592 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ETHERNET
,
1593 ð_key
, sizeof eth_key
);
1594 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_ETHERNET
,
1595 ð_key_mask
, sizeof eth_key_mask
);
1597 } else if (sscanf(s
,
1598 "eth(src="ETH_ADDR_SCAN_FMT
",dst="ETH_ADDR_SCAN_FMT
")%n",
1599 ETH_ADDR_SCAN_ARGS(eth_key
.eth_src
),
1600 ETH_ADDR_SCAN_ARGS(eth_key
.eth_dst
), &n
) > 0 && n
> 0) {
1601 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ETHERNET
,
1602 ð_key
, sizeof eth_key
);
1605 memset(ð_key
, 0xff, sizeof eth_key
);
1606 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_ETHERNET
,
1607 ð_key
, sizeof eth_key
);
1614 uint16_t vid
, vid_mask
;
1619 if (mask
&& (sscanf(s
, "vlan(vid=%"SCNi16
"/%"SCNi16
",pcp=%i/%i)%n",
1620 &vid
, &vid_mask
, &pcp
, &pcp_mask
, &n
) > 0 && n
> 0)) {
1621 nl_msg_put_be16(key
, OVS_KEY_ATTR_VLAN
,
1622 htons((vid
<< VLAN_VID_SHIFT
) |
1623 (pcp
<< VLAN_PCP_SHIFT
) |
1625 nl_msg_put_be16(mask
, OVS_KEY_ATTR_VLAN
,
1626 htons((vid_mask
<< VLAN_VID_SHIFT
) |
1627 (pcp_mask
<< VLAN_PCP_SHIFT
) |
1628 (1 << VLAN_CFI_SHIFT
)));
1630 } else if ((sscanf(s
, "vlan(vid=%"SCNi16
",pcp=%i)%n",
1631 &vid
, &pcp
, &n
) > 0 && n
> 0)) {
1632 nl_msg_put_be16(key
, OVS_KEY_ATTR_VLAN
,
1633 htons((vid
<< VLAN_VID_SHIFT
) |
1634 (pcp
<< VLAN_PCP_SHIFT
) |
1637 nl_msg_put_be16(mask
, OVS_KEY_ATTR_VLAN
, htons(UINT16_MAX
));
1640 } else if (mask
&& (sscanf(s
, "vlan(vid=%"SCNi16
"/%"SCNi16
",pcp=%i/%i,cfi=%i/%i)%n",
1641 &vid
, &vid_mask
, &pcp
, &pcp_mask
, &cfi
, &cfi_mask
, &n
) > 0 && n
> 0)) {
1642 nl_msg_put_be16(key
, OVS_KEY_ATTR_VLAN
,
1643 htons((vid
<< VLAN_VID_SHIFT
) |
1644 (pcp
<< VLAN_PCP_SHIFT
) |
1645 (cfi
? VLAN_CFI
: 0)));
1646 nl_msg_put_be16(mask
, OVS_KEY_ATTR_VLAN
,
1647 htons((vid_mask
<< VLAN_VID_SHIFT
) |
1648 (pcp_mask
<< VLAN_PCP_SHIFT
) |
1649 (cfi_mask
<< VLAN_CFI_SHIFT
)));
1651 } else if ((sscanf(s
, "vlan(vid=%"SCNi16
",pcp=%i,cfi=%i)%n",
1652 &vid
, &pcp
, &cfi
, &n
) > 0 && n
> 0)) {
1653 nl_msg_put_be16(key
, OVS_KEY_ATTR_VLAN
,
1654 htons((vid
<< VLAN_VID_SHIFT
) |
1655 (pcp
<< VLAN_PCP_SHIFT
) |
1656 (cfi
? VLAN_CFI
: 0)));
1658 nl_msg_put_be16(mask
, OVS_KEY_ATTR_VLAN
, htons(UINT16_MAX
));
1669 if (mask
&& sscanf(s
, "eth_type(%i/%i)%n",
1670 ð_type
, ð_type_mask
, &n
) > 0 && n
> 0) {
1671 if (eth_type
!= 0) {
1672 nl_msg_put_be16(key
, OVS_KEY_ATTR_ETHERTYPE
, htons(eth_type
));
1674 nl_msg_put_be16(mask
, OVS_KEY_ATTR_ETHERTYPE
, htons(eth_type_mask
));
1676 } else if (sscanf(s
, "eth_type(%i)%n", ð_type
, &n
) > 0 && n
> 0) {
1677 nl_msg_put_be16(key
, OVS_KEY_ATTR_ETHERTYPE
, htons(eth_type
));
1679 nl_msg_put_be16(mask
, OVS_KEY_ATTR_ETHERTYPE
,
1687 int label
, tc
, ttl
, bos
;
1688 int label_mask
, tc_mask
, ttl_mask
, bos_mask
;
1691 if (mask
&& sscanf(s
, "mpls(label=%"SCNi32
"/%"SCNi32
",tc=%i/%i,ttl=%i/%i,bos=%i/%i)%n",
1692 &label
, &label_mask
, &tc
, &tc_mask
, &ttl
, &ttl_mask
, &bos
, &bos_mask
, &n
) > 0 && n
> 0) {
1693 struct ovs_key_mpls
*mpls
, *mpls_mask
;
1695 mpls
= nl_msg_put_unspec_uninit(key
, OVS_KEY_ATTR_MPLS
,
1697 mpls
->mpls_lse
= mpls_lse_from_components(label
, tc
, ttl
, bos
);
1699 mpls_mask
= nl_msg_put_unspec_uninit(mask
, OVS_KEY_ATTR_MPLS
,
1701 mpls_mask
->mpls_lse
= mpls_lse_from_components(
1702 label_mask
, tc_mask
, ttl_mask
, bos_mask
);
1704 } else if (sscanf(s
, "mpls(label=%"SCNi32
",tc=%i,ttl=%i,bos=%i)%n",
1705 &label
, &tc
, &ttl
, &bos
, &n
) > 0 &&
1707 struct ovs_key_mpls
*mpls
;
1709 mpls
= nl_msg_put_unspec_uninit(key
, OVS_KEY_ATTR_MPLS
,
1711 mpls
->mpls_lse
= mpls_lse_from_components(label
, tc
, ttl
, bos
);
1713 mpls
= nl_msg_put_unspec_uninit(mask
, OVS_KEY_ATTR_MPLS
,
1715 mpls
->mpls_lse
= htonl(UINT32_MAX
);
1723 ovs_be32 ipv4_src
, ipv4_src_mask
;
1724 ovs_be32 ipv4_dst
, ipv4_dst_mask
;
1725 int ipv4_proto
, ipv4_proto_mask
;
1726 int ipv4_tos
, ipv4_tos_mask
;
1727 int ipv4_ttl
, ipv4_ttl_mask
;
1730 enum ovs_frag_type ipv4_frag
;
1733 if (mask
&& sscanf(s
, "ipv4(src="IP_SCAN_FMT
"/"IP_SCAN_FMT
","
1734 "dst="IP_SCAN_FMT
"/"IP_SCAN_FMT
","
1735 "proto=%i/%i,tos=%i/%i,ttl=%i/%i,"
1736 "frag=%7[a-z]/%i)%n",
1737 IP_SCAN_ARGS(&ipv4_src
), IP_SCAN_ARGS(&ipv4_src_mask
),
1738 IP_SCAN_ARGS(&ipv4_dst
), IP_SCAN_ARGS(&ipv4_dst_mask
),
1739 &ipv4_proto
, &ipv4_proto_mask
,
1740 &ipv4_tos
, &ipv4_tos_mask
, &ipv4_ttl
, &ipv4_ttl_mask
,
1741 frag
, &ipv4_frag_mask
, &n
) > 0
1743 && ovs_frag_type_from_string(frag
, &ipv4_frag
)) {
1744 struct ovs_key_ipv4 ipv4_key
;
1745 struct ovs_key_ipv4 ipv4_mask
;
1747 ipv4_key
.ipv4_src
= ipv4_src
;
1748 ipv4_key
.ipv4_dst
= ipv4_dst
;
1749 ipv4_key
.ipv4_proto
= ipv4_proto
;
1750 ipv4_key
.ipv4_tos
= ipv4_tos
;
1751 ipv4_key
.ipv4_ttl
= ipv4_ttl
;
1752 ipv4_key
.ipv4_frag
= ipv4_frag
;
1753 nl_msg_put_unspec(key
, OVS_KEY_ATTR_IPV4
,
1754 &ipv4_key
, sizeof ipv4_key
);
1756 ipv4_mask
.ipv4_src
= ipv4_src_mask
;
1757 ipv4_mask
.ipv4_dst
= ipv4_dst_mask
;
1758 ipv4_mask
.ipv4_proto
= ipv4_proto_mask
;
1759 ipv4_mask
.ipv4_tos
= ipv4_tos_mask
;
1760 ipv4_mask
.ipv4_ttl
= ipv4_ttl_mask
;
1761 ipv4_mask
.ipv4_frag
= ipv4_frag_mask
;
1762 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_IPV4
,
1763 &ipv4_mask
, sizeof ipv4_mask
);
1765 } else if (sscanf(s
, "ipv4(src="IP_SCAN_FMT
",dst="IP_SCAN_FMT
","
1766 "proto=%i,tos=%i,ttl=%i,frag=%7[a-z])%n",
1767 IP_SCAN_ARGS(&ipv4_src
), IP_SCAN_ARGS(&ipv4_dst
),
1768 &ipv4_proto
, &ipv4_tos
, &ipv4_ttl
, frag
, &n
) > 0
1770 && ovs_frag_type_from_string(frag
, &ipv4_frag
)) {
1771 struct ovs_key_ipv4 ipv4_key
;
1773 ipv4_key
.ipv4_src
= ipv4_src
;
1774 ipv4_key
.ipv4_dst
= ipv4_dst
;
1775 ipv4_key
.ipv4_proto
= ipv4_proto
;
1776 ipv4_key
.ipv4_tos
= ipv4_tos
;
1777 ipv4_key
.ipv4_ttl
= ipv4_ttl
;
1778 ipv4_key
.ipv4_frag
= ipv4_frag
;
1779 nl_msg_put_unspec(key
, OVS_KEY_ATTR_IPV4
,
1780 &ipv4_key
, sizeof ipv4_key
);
1783 memset(&ipv4_key
, 0xff, sizeof ipv4_key
);
1784 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_IPV4
,
1785 &ipv4_key
, sizeof ipv4_key
);
1792 char ipv6_src_s
[IPV6_SCAN_LEN
+ 1];
1793 char ipv6_src_mask_s
[IPV6_SCAN_LEN
+ 1];
1794 char ipv6_dst_s
[IPV6_SCAN_LEN
+ 1];
1795 char ipv6_dst_mask_s
[IPV6_SCAN_LEN
+ 1];
1796 int ipv6_label
, ipv6_label_mask
;
1797 int ipv6_proto
, ipv6_proto_mask
;
1798 int ipv6_tclass
, ipv6_tclass_mask
;
1799 int ipv6_hlimit
, ipv6_hlimit_mask
;
1801 enum ovs_frag_type ipv6_frag
;
1805 if (mask
&& sscanf(s
, "ipv6(src="IPV6_SCAN_FMT
"/"IPV6_SCAN_FMT
",dst="
1806 IPV6_SCAN_FMT
"/"IPV6_SCAN_FMT
","
1807 "label=%i/%i,proto=%i/%i,tclass=%i/%i,"
1808 "hlimit=%i/%i,frag=%7[a-z]/%i)%n",
1809 ipv6_src_s
, ipv6_src_mask_s
, ipv6_dst_s
, ipv6_dst_mask_s
,
1810 &ipv6_label
, &ipv6_label_mask
, &ipv6_proto
,
1811 &ipv6_proto_mask
, &ipv6_tclass
, &ipv6_tclass_mask
,
1812 &ipv6_hlimit
, &ipv6_hlimit_mask
, frag
,
1813 &ipv6_frag_mask
, &n
) > 0
1815 && ovs_frag_type_from_string(frag
, &ipv6_frag
)) {
1816 struct ovs_key_ipv6 ipv6_key
;
1817 struct ovs_key_ipv6 ipv6_mask
;
1819 if (inet_pton(AF_INET6
, ipv6_src_s
, &ipv6_key
.ipv6_src
) != 1 ||
1820 inet_pton(AF_INET6
, ipv6_dst_s
, &ipv6_key
.ipv6_dst
) != 1 ||
1821 inet_pton(AF_INET6
, ipv6_src_mask_s
, &ipv6_mask
.ipv6_src
) != 1 ||
1822 inet_pton(AF_INET6
, ipv6_dst_mask_s
, &ipv6_mask
.ipv6_dst
) != 1) {
1826 ipv6_key
.ipv6_label
= htonl(ipv6_label
);
1827 ipv6_key
.ipv6_proto
= ipv6_proto
;
1828 ipv6_key
.ipv6_tclass
= ipv6_tclass
;
1829 ipv6_key
.ipv6_hlimit
= ipv6_hlimit
;
1830 ipv6_key
.ipv6_frag
= ipv6_frag
;
1831 nl_msg_put_unspec(key
, OVS_KEY_ATTR_IPV6
,
1832 &ipv6_key
, sizeof ipv6_key
);
1834 ipv6_mask
.ipv6_label
= htonl(ipv6_label_mask
);
1835 ipv6_mask
.ipv6_proto
= ipv6_proto_mask
;
1836 ipv6_mask
.ipv6_tclass
= ipv6_tclass_mask
;
1837 ipv6_mask
.ipv6_hlimit
= ipv6_hlimit_mask
;
1838 ipv6_mask
.ipv6_frag
= ipv6_frag_mask
;
1839 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_IPV6
,
1840 &ipv6_mask
, sizeof ipv6_mask
);
1842 } else if (sscanf(s
, "ipv6(src="IPV6_SCAN_FMT
",dst="IPV6_SCAN_FMT
","
1843 "label=%i,proto=%i,tclass=%i,hlimit=%i,frag=%7[a-z])%n",
1844 ipv6_src_s
, ipv6_dst_s
, &ipv6_label
,
1845 &ipv6_proto
, &ipv6_tclass
, &ipv6_hlimit
, frag
, &n
) > 0
1847 && ovs_frag_type_from_string(frag
, &ipv6_frag
)) {
1848 struct ovs_key_ipv6 ipv6_key
;
1850 if (inet_pton(AF_INET6
, ipv6_src_s
, &ipv6_key
.ipv6_src
) != 1 ||
1851 inet_pton(AF_INET6
, ipv6_dst_s
, &ipv6_key
.ipv6_dst
) != 1) {
1854 ipv6_key
.ipv6_label
= htonl(ipv6_label
);
1855 ipv6_key
.ipv6_proto
= ipv6_proto
;
1856 ipv6_key
.ipv6_tclass
= ipv6_tclass
;
1857 ipv6_key
.ipv6_hlimit
= ipv6_hlimit
;
1858 ipv6_key
.ipv6_frag
= ipv6_frag
;
1859 nl_msg_put_unspec(key
, OVS_KEY_ATTR_IPV6
,
1860 &ipv6_key
, sizeof ipv6_key
);
1863 memset(&ipv6_key
, 0xff, sizeof ipv6_key
);
1864 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_IPV6
,
1865 &ipv6_key
, sizeof ipv6_key
);
1878 if (mask
&& sscanf(s
, "tcp(src=%i/%i,dst=%i/%i)%n",
1879 &tcp_src
, &tcp_src_mask
, &tcp_dst
, &tcp_dst_mask
, &n
) > 0
1881 struct ovs_key_tcp tcp_key
;
1882 struct ovs_key_tcp tcp_mask
;
1884 tcp_key
.tcp_src
= htons(tcp_src
);
1885 tcp_key
.tcp_dst
= htons(tcp_dst
);
1886 nl_msg_put_unspec(key
, OVS_KEY_ATTR_TCP
, &tcp_key
, sizeof tcp_key
);
1888 tcp_mask
.tcp_src
= htons(tcp_src_mask
);
1889 tcp_mask
.tcp_dst
= htons(tcp_dst_mask
);
1890 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_TCP
,
1891 &tcp_mask
, sizeof tcp_mask
);
1893 } else if (sscanf(s
, "tcp(src=%i,dst=%i)%n",&tcp_src
, &tcp_dst
, &n
) > 0
1895 struct ovs_key_tcp tcp_key
;
1897 tcp_key
.tcp_src
= htons(tcp_src
);
1898 tcp_key
.tcp_dst
= htons(tcp_dst
);
1899 nl_msg_put_unspec(key
, OVS_KEY_ATTR_TCP
, &tcp_key
, sizeof tcp_key
);
1902 memset(&tcp_key
, 0xff, sizeof tcp_key
);
1903 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_TCP
,
1904 &tcp_key
, sizeof tcp_key
);
1917 if (mask
&& sscanf(s
, "udp(src=%i/%i,dst=%i/%i)%n",
1918 &udp_src
, &udp_src_mask
,
1919 &udp_dst
, &udp_dst_mask
, &n
) > 0 && n
> 0) {
1920 struct ovs_key_udp udp_key
;
1921 struct ovs_key_udp udp_mask
;
1923 udp_key
.udp_src
= htons(udp_src
);
1924 udp_key
.udp_dst
= htons(udp_dst
);
1925 nl_msg_put_unspec(key
, OVS_KEY_ATTR_UDP
, &udp_key
, sizeof udp_key
);
1927 udp_mask
.udp_src
= htons(udp_src_mask
);
1928 udp_mask
.udp_dst
= htons(udp_dst_mask
);
1929 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_UDP
,
1930 &udp_mask
, sizeof udp_mask
);
1933 if (sscanf(s
, "udp(src=%i,dst=%i)%n", &udp_src
, &udp_dst
, &n
) > 0
1935 struct ovs_key_udp udp_key
;
1937 udp_key
.udp_src
= htons(udp_src
);
1938 udp_key
.udp_dst
= htons(udp_dst
);
1939 nl_msg_put_unspec(key
, OVS_KEY_ATTR_UDP
, &udp_key
, sizeof udp_key
);
1942 memset(&udp_key
, 0xff, sizeof udp_key
);
1943 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_UDP
, &udp_key
, sizeof udp_key
);
1956 if (mask
&& sscanf(s
, "icmp(type=%i/%i,code=%i/%i)%n",
1957 &icmp_type
, &icmp_type_mask
,
1958 &icmp_code
, &icmp_code_mask
, &n
) > 0 && n
> 0) {
1959 struct ovs_key_icmp icmp_key
;
1960 struct ovs_key_icmp icmp_mask
;
1962 icmp_key
.icmp_type
= icmp_type
;
1963 icmp_key
.icmp_code
= icmp_code
;
1964 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ICMP
,
1965 &icmp_key
, sizeof icmp_key
);
1967 icmp_mask
.icmp_type
= icmp_type_mask
;
1968 icmp_mask
.icmp_code
= icmp_code_mask
;
1969 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_ICMP
,
1970 &icmp_mask
, sizeof icmp_mask
);
1972 } else if (sscanf(s
, "icmp(type=%i,code=%i)%n",
1973 &icmp_type
, &icmp_code
, &n
) > 0
1975 struct ovs_key_icmp icmp_key
;
1977 icmp_key
.icmp_type
= icmp_type
;
1978 icmp_key
.icmp_code
= icmp_code
;
1979 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ICMP
,
1980 &icmp_key
, sizeof icmp_key
);
1982 memset(&icmp_key
, 0xff, sizeof icmp_key
);
1983 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_ICMP
, &icmp_key
,
1991 struct ovs_key_icmpv6 icmpv6_key
;
1992 struct ovs_key_icmpv6 icmpv6_mask
;
1993 int icmpv6_type_mask
;
1994 int icmpv6_code_mask
;
1997 if (mask
&& sscanf(s
, "icmpv6(type=%"SCNi8
"/%i,code=%"SCNi8
"/%i)%n",
1998 &icmpv6_key
.icmpv6_type
, &icmpv6_type_mask
,
1999 &icmpv6_key
.icmpv6_code
, &icmpv6_code_mask
, &n
) > 0
2001 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ICMPV6
,
2002 &icmpv6_key
, sizeof icmpv6_key
);
2004 icmpv6_mask
.icmpv6_type
= icmpv6_type_mask
;
2005 icmpv6_mask
.icmpv6_code
= icmpv6_code_mask
;
2006 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_ICMPV6
, &icmpv6_mask
,
2007 sizeof icmpv6_mask
);
2009 } else if (sscanf(s
, "icmpv6(type=%"SCNi8
",code=%"SCNi8
")%n",
2010 &icmpv6_key
.icmpv6_type
, &icmpv6_key
.icmpv6_code
,&n
) > 0
2012 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ICMPV6
,
2013 &icmpv6_key
, sizeof icmpv6_key
);
2016 memset(&icmpv6_key
, 0xff, sizeof icmpv6_key
);
2017 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_ICMPV6
, &icmpv6_key
,
2025 ovs_be32 arp_sip
, arp_sip_mask
;
2026 ovs_be32 arp_tip
, arp_tip_mask
;
2027 int arp_op
, arp_op_mask
;
2028 uint8_t arp_sha
[ETH_ADDR_LEN
];
2029 uint8_t arp_sha_mask
[ETH_ADDR_LEN
];
2030 uint8_t arp_tha
[ETH_ADDR_LEN
];
2031 uint8_t arp_tha_mask
[ETH_ADDR_LEN
];
2034 if (mask
&& sscanf(s
, "arp(sip="IP_SCAN_FMT
"/"IP_SCAN_FMT
","
2035 "tip="IP_SCAN_FMT
"/"IP_SCAN_FMT
","
2036 "op=%i/%i,sha="ETH_ADDR_SCAN_FMT
"/"ETH_ADDR_SCAN_FMT
","
2037 "tha="ETH_ADDR_SCAN_FMT
"/"ETH_ADDR_SCAN_FMT
")%n",
2038 IP_SCAN_ARGS(&arp_sip
), IP_SCAN_ARGS(&arp_sip_mask
),
2039 IP_SCAN_ARGS(&arp_tip
), IP_SCAN_ARGS(&arp_tip_mask
),
2040 &arp_op
, &arp_op_mask
,
2041 ETH_ADDR_SCAN_ARGS(arp_sha
),
2042 ETH_ADDR_SCAN_ARGS(arp_sha_mask
),
2043 ETH_ADDR_SCAN_ARGS(arp_tha
),
2044 ETH_ADDR_SCAN_ARGS(arp_tha_mask
), &n
) > 0 && n
> 0) {
2045 struct ovs_key_arp arp_key
;
2046 struct ovs_key_arp arp_mask
;
2048 memset(&arp_key
, 0, sizeof arp_key
);
2049 arp_key
.arp_sip
= arp_sip
;
2050 arp_key
.arp_tip
= arp_tip
;
2051 arp_key
.arp_op
= htons(arp_op
);
2052 memcpy(arp_key
.arp_sha
, arp_sha
, ETH_ADDR_LEN
);
2053 memcpy(arp_key
.arp_tha
, arp_tha
, ETH_ADDR_LEN
);
2054 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ARP
, &arp_key
, sizeof arp_key
);
2056 arp_mask
.arp_sip
= arp_sip_mask
;
2057 arp_mask
.arp_tip
= arp_tip_mask
;
2058 arp_mask
.arp_op
= htons(arp_op_mask
);
2059 memcpy(arp_mask
.arp_sha
, arp_sha_mask
, ETH_ADDR_LEN
);
2060 memcpy(arp_mask
.arp_tha
, arp_tha_mask
, ETH_ADDR_LEN
);
2061 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_ARP
,
2062 &arp_mask
, sizeof arp_mask
);
2064 } else if (sscanf(s
, "arp(sip="IP_SCAN_FMT
",tip="IP_SCAN_FMT
","
2065 "op=%i,sha="ETH_ADDR_SCAN_FMT
",tha="ETH_ADDR_SCAN_FMT
")%n",
2066 IP_SCAN_ARGS(&arp_sip
),
2067 IP_SCAN_ARGS(&arp_tip
),
2069 ETH_ADDR_SCAN_ARGS(arp_sha
),
2070 ETH_ADDR_SCAN_ARGS(arp_tha
), &n
) > 0 && n
> 0) {
2071 struct ovs_key_arp arp_key
;
2073 memset(&arp_key
, 0, sizeof arp_key
);
2074 arp_key
.arp_sip
= arp_sip
;
2075 arp_key
.arp_tip
= arp_tip
;
2076 arp_key
.arp_op
= htons(arp_op
);
2077 memcpy(arp_key
.arp_sha
, arp_sha
, ETH_ADDR_LEN
);
2078 memcpy(arp_key
.arp_tha
, arp_tha
, ETH_ADDR_LEN
);
2079 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ARP
, &arp_key
, sizeof arp_key
);
2082 memset(&arp_key
, 0xff, sizeof arp_key
);
2083 nl_msg_put_unspec(mask
, OVS_KEY_ATTR_ARP
,
2084 &arp_key
, sizeof arp_key
);
2091 char nd_target_s
[IPV6_SCAN_LEN
+ 1];
2092 char nd_target_mask_s
[IPV6_SCAN_LEN
+ 1];
2093 uint8_t nd_sll
[ETH_ADDR_LEN
];
2094 uint8_t nd_sll_mask
[ETH_ADDR_LEN
];
2095 uint8_t nd_tll
[ETH_ADDR_LEN
];
2096 uint8_t nd_tll_mask
[ETH_ADDR_LEN
];
2099 memset(&nd_target_mask_s
[0], 0xff, sizeof nd_target_s
);
2100 memset(&nd_sll_mask
[0], 0xff, sizeof nd_sll
);
2101 memset(&nd_tll_mask
[0], 0xff, sizeof nd_tll
);
2103 if (mask
&& sscanf(s
, "nd(target="IPV6_SCAN_FMT
"/"IPV6_SCAN_FMT
")%n",
2104 nd_target_s
, nd_target_mask_s
, &n
) > 0 && n
> 0) {
2105 put_nd_key(n
, nd_target_s
, NULL
, NULL
, key
);
2106 put_nd_key(n
, nd_target_mask_s
, NULL
, NULL
, mask
);
2107 } else if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
")%n",
2108 nd_target_s
, &n
) > 0 && n
> 0) {
2109 put_nd_key(n
, nd_target_s
, NULL
, NULL
, key
);
2111 put_nd_key(n
, nd_target_mask_s
, NULL
, NULL
, mask
);
2113 } else if (mask
&& sscanf(s
, "nd(target="IPV6_SCAN_FMT
"/"IPV6_SCAN_FMT
2114 ",sll="ETH_ADDR_SCAN_FMT
"/"ETH_ADDR_SCAN_FMT
")%n",
2115 nd_target_s
, nd_target_mask_s
,
2116 ETH_ADDR_SCAN_ARGS(nd_sll
),
2117 ETH_ADDR_SCAN_ARGS(nd_sll_mask
), &n
) > 0 && n
> 0) {
2118 put_nd_key(n
, nd_target_s
, nd_sll
, NULL
, key
);
2119 put_nd_key(n
, nd_target_mask_s
, nd_sll_mask
, NULL
, mask
);
2120 } else if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
",sll="ETH_ADDR_SCAN_FMT
")%n",
2121 nd_target_s
, ETH_ADDR_SCAN_ARGS(nd_sll
), &n
) > 0
2123 put_nd_key(n
, nd_target_s
, nd_sll
, NULL
, key
);
2125 put_nd_key(n
, nd_target_mask_s
, nd_sll_mask
, NULL
, mask
);
2127 } else if (mask
&& sscanf(s
, "nd(target="IPV6_SCAN_FMT
"/"IPV6_SCAN_FMT
2128 ",tll="ETH_ADDR_SCAN_FMT
"/"ETH_ADDR_SCAN_FMT
")%n",
2129 nd_target_s
, nd_target_mask_s
,
2130 ETH_ADDR_SCAN_ARGS(nd_tll
),
2131 ETH_ADDR_SCAN_ARGS(nd_tll_mask
), &n
) > 0 && n
> 0) {
2132 put_nd_key(n
, nd_target_s
, NULL
, nd_tll
, key
);
2133 put_nd_key(n
, nd_target_mask_s
, NULL
, nd_tll_mask
, mask
);
2134 } else if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
",tll="ETH_ADDR_SCAN_FMT
")%n",
2135 nd_target_s
, ETH_ADDR_SCAN_ARGS(nd_tll
), &n
) > 0
2137 put_nd_key(n
, nd_target_s
, NULL
, nd_tll
, key
);
2139 put_nd_key(n
, nd_target_mask_s
, NULL
, nd_tll_mask
, mask
);
2141 } else if (mask
&& sscanf(s
, "nd(target="IPV6_SCAN_FMT
"/"IPV6_SCAN_FMT
2142 ",sll="ETH_ADDR_SCAN_FMT
"/"ETH_ADDR_SCAN_FMT
","
2143 "tll="ETH_ADDR_SCAN_FMT
"/"ETH_ADDR_SCAN_FMT
")%n",
2144 nd_target_s
, nd_target_mask_s
,
2145 ETH_ADDR_SCAN_ARGS(nd_sll
), ETH_ADDR_SCAN_ARGS(nd_sll_mask
),
2146 ETH_ADDR_SCAN_ARGS(nd_tll
), ETH_ADDR_SCAN_ARGS(nd_tll_mask
),
2149 put_nd_key(n
, nd_target_s
, nd_sll
, nd_tll
, key
);
2150 put_nd_key(n
, nd_target_mask_s
, nd_sll_mask
, nd_tll_mask
, mask
);
2151 } else if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
",sll="ETH_ADDR_SCAN_FMT
","
2152 "tll="ETH_ADDR_SCAN_FMT
")%n",
2153 nd_target_s
, ETH_ADDR_SCAN_ARGS(nd_sll
),
2154 ETH_ADDR_SCAN_ARGS(nd_tll
), &n
) > 0
2156 put_nd_key(n
, nd_target_s
, nd_sll
, nd_tll
, key
);
2158 put_nd_key(n
, nd_target_mask_s
, nd_sll_mask
, nd_tll_mask
, mask
);
2167 if (!strncmp(s
, "encap(", 6)) {
2168 const char *start
= s
;
2169 size_t encap
, encap_mask
= 0;
2171 encap
= nl_msg_start_nested(key
, OVS_KEY_ATTR_ENCAP
);
2173 encap_mask
= nl_msg_start_nested(mask
, OVS_KEY_ATTR_ENCAP
);
2180 s
+= strspn(s
, ", \t\r\n");
2183 } else if (*s
== ')') {
2187 retval
= parse_odp_key_mask_attr(s
, port_names
, key
, mask
);
2195 nl_msg_end_nested(key
, encap
);
2197 nl_msg_end_nested(mask
, encap_mask
);
2206 /* Parses the string representation of a datapath flow key, in the
2207 * format output by odp_flow_key_format(). Returns 0 if successful,
2208 * otherwise a positive errno value. On success, the flow key is
2209 * appended to 'key' as a series of Netlink attributes. On failure, no
2210 * data is appended to 'key'. Either way, 'key''s data might be
2213 * If 'port_names' is nonnull, it points to an simap that maps from a port name
2214 * to a port number. (Port names may be used instead of port numbers in
2217 * On success, the attributes appended to 'key' are individually syntactically
2218 * valid, but they may not be valid as a sequence. 'key' might, for example,
2219 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
2221 odp_flow_from_string(const char *s
, const struct simap
*port_names
,
2222 struct ofpbuf
*key
, struct ofpbuf
*mask
)
2224 const size_t old_size
= key
->size
;
2228 s
+= strspn(s
, delimiters
);
2233 retval
= parse_odp_key_mask_attr(s
, port_names
, key
, mask
);
2235 key
->size
= old_size
;
2245 ovs_to_odp_frag(uint8_t nw_frag
)
2247 return (nw_frag
== 0 ? OVS_FRAG_TYPE_NONE
2248 : nw_frag
== FLOW_NW_FRAG_ANY
? OVS_FRAG_TYPE_FIRST
2249 : OVS_FRAG_TYPE_LATER
);
2253 odp_flow_key_from_flow__(struct ofpbuf
*buf
, const struct flow
*data
,
2254 const struct flow
*flow
, odp_port_t odp_in_port
)
2257 struct ovs_key_ethernet
*eth_key
;
2260 /* We assume that if 'data' and 'flow' are not the same, we should
2261 * treat 'data' as a mask. */
2262 is_mask
= (data
!= flow
);
2264 if (flow
->skb_priority
) {
2265 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, data
->skb_priority
);
2268 if (flow
->tunnel
.ip_dst
) {
2269 tun_key_to_attr(buf
, &data
->tunnel
);
2272 if (flow
->skb_mark
) {
2273 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, data
->skb_mark
);
2276 /* Add an ingress port attribute if this is a mask or 'odp_in_port'
2277 * is not the magical value "ODPP_NONE". */
2278 if (is_mask
|| odp_in_port
!= ODPP_NONE
) {
2279 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, odp_in_port
);
2282 eth_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ETHERNET
,
2284 memcpy(eth_key
->eth_src
, data
->dl_src
, ETH_ADDR_LEN
);
2285 memcpy(eth_key
->eth_dst
, data
->dl_dst
, ETH_ADDR_LEN
);
2287 if (flow
->vlan_tci
!= htons(0) || flow
->dl_type
== htons(ETH_TYPE_VLAN
)) {
2288 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, htons(ETH_TYPE_VLAN
));
2289 nl_msg_put_be16(buf
, OVS_KEY_ATTR_VLAN
, data
->vlan_tci
);
2290 encap
= nl_msg_start_nested(buf
, OVS_KEY_ATTR_ENCAP
);
2291 if (flow
->vlan_tci
== htons(0)) {
2298 if (ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
2299 /* For backwards compatibility with kernels that don't support
2300 * wildcarding, the following convention is used to encode the
2301 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
2304 * -------- -------- -------
2305 * >0x5ff 0xffff Specified Ethernet II Ethertype.
2306 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
2307 * <none> 0xffff Any non-Ethernet II frame (except valid
2308 * 802.3 SNAP packet with valid eth_type).
2311 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, data
->dl_type
);
2316 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, data
->dl_type
);
2318 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
2319 struct ovs_key_ipv4
*ipv4_key
;
2321 ipv4_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV4
,
2323 ipv4_key
->ipv4_src
= data
->nw_src
;
2324 ipv4_key
->ipv4_dst
= data
->nw_dst
;
2325 ipv4_key
->ipv4_proto
= data
->nw_proto
;
2326 ipv4_key
->ipv4_tos
= data
->nw_tos
;
2327 ipv4_key
->ipv4_ttl
= data
->nw_ttl
;
2328 ipv4_key
->ipv4_frag
= ovs_to_odp_frag(data
->nw_frag
);
2329 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
2330 struct ovs_key_ipv6
*ipv6_key
;
2332 ipv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV6
,
2334 memcpy(ipv6_key
->ipv6_src
, &data
->ipv6_src
, sizeof ipv6_key
->ipv6_src
);
2335 memcpy(ipv6_key
->ipv6_dst
, &data
->ipv6_dst
, sizeof ipv6_key
->ipv6_dst
);
2336 ipv6_key
->ipv6_label
= data
->ipv6_label
;
2337 ipv6_key
->ipv6_proto
= data
->nw_proto
;
2338 ipv6_key
->ipv6_tclass
= data
->nw_tos
;
2339 ipv6_key
->ipv6_hlimit
= data
->nw_ttl
;
2340 ipv6_key
->ipv6_frag
= ovs_to_odp_frag(flow
->nw_frag
);
2341 } else if (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
2342 flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
2343 struct ovs_key_arp
*arp_key
;
2345 arp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ARP
,
2347 memset(arp_key
, 0, sizeof *arp_key
);
2348 arp_key
->arp_sip
= data
->nw_src
;
2349 arp_key
->arp_tip
= data
->nw_dst
;
2350 arp_key
->arp_op
= htons(data
->nw_proto
);
2351 memcpy(arp_key
->arp_sha
, data
->arp_sha
, ETH_ADDR_LEN
);
2352 memcpy(arp_key
->arp_tha
, data
->arp_tha
, ETH_ADDR_LEN
);
2355 if (flow
->mpls_depth
) {
2356 struct ovs_key_mpls
*mpls_key
;
2358 mpls_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_MPLS
,
2360 mpls_key
->mpls_lse
= data
->mpls_lse
;
2363 if (is_ip_any(flow
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
2364 if (flow
->nw_proto
== IPPROTO_TCP
) {
2365 struct ovs_key_tcp
*tcp_key
;
2367 tcp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_TCP
,
2369 tcp_key
->tcp_src
= data
->tp_src
;
2370 tcp_key
->tcp_dst
= data
->tp_dst
;
2371 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
2372 struct ovs_key_udp
*udp_key
;
2374 udp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_UDP
,
2376 udp_key
->udp_src
= data
->tp_src
;
2377 udp_key
->udp_dst
= data
->tp_dst
;
2378 } else if (flow
->dl_type
== htons(ETH_TYPE_IP
)
2379 && flow
->nw_proto
== IPPROTO_ICMP
) {
2380 struct ovs_key_icmp
*icmp_key
;
2382 icmp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMP
,
2384 icmp_key
->icmp_type
= ntohs(data
->tp_src
);
2385 icmp_key
->icmp_code
= ntohs(data
->tp_dst
);
2386 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)
2387 && flow
->nw_proto
== IPPROTO_ICMPV6
) {
2388 struct ovs_key_icmpv6
*icmpv6_key
;
2390 icmpv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMPV6
,
2391 sizeof *icmpv6_key
);
2392 icmpv6_key
->icmpv6_type
= ntohs(data
->tp_src
);
2393 icmpv6_key
->icmpv6_code
= ntohs(data
->tp_dst
);
2395 if (icmpv6_key
->icmpv6_type
== ND_NEIGHBOR_SOLICIT
2396 || icmpv6_key
->icmpv6_type
== ND_NEIGHBOR_ADVERT
) {
2397 struct ovs_key_nd
*nd_key
;
2399 nd_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ND
,
2401 memcpy(nd_key
->nd_target
, &data
->nd_target
,
2402 sizeof nd_key
->nd_target
);
2403 memcpy(nd_key
->nd_sll
, data
->arp_sha
, ETH_ADDR_LEN
);
2404 memcpy(nd_key
->nd_tll
, data
->arp_tha
, ETH_ADDR_LEN
);
2411 nl_msg_end_nested(buf
, encap
);
2415 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
2416 * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
2417 * number rather than a datapath port number). Instead, if 'odp_in_port'
2418 * is anything other than ODPP_NONE, it is included in 'buf' as the input
2421 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
2422 * capable of being expanded to allow for that much space. */
2424 odp_flow_key_from_flow(struct ofpbuf
*buf
, const struct flow
*flow
,
2425 odp_port_t odp_in_port
)
2427 odp_flow_key_from_flow__(buf
, flow
, flow
, odp_in_port
);
2430 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
2431 * 'buf'. 'flow' is used as a template to determine how to interpret
2432 * 'mask'. For example, the 'dl_type' of 'mask' describes the mask, but
2433 * it doesn't indicate whether the other fields should be interpreted as
2434 * ARP, IPv4, IPv6, etc.
2436 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
2437 * capable of being expanded to allow for that much space. */
2439 odp_flow_key_from_mask(struct ofpbuf
*buf
, const struct flow
*mask
,
2440 const struct flow
*flow
, uint32_t odp_in_port_mask
)
2442 odp_flow_key_from_flow__(buf
, mask
, flow
, u32_to_odp(odp_in_port_mask
));
2446 odp_flow_key_hash(const struct nlattr
*key
, size_t key_len
)
2448 BUILD_ASSERT_DECL(!(NLA_ALIGNTO
% sizeof(uint32_t)));
2449 return hash_words((const uint32_t *) key
, key_len
/ sizeof(uint32_t), 0);
2453 log_odp_key_attributes(struct vlog_rate_limit
*rl
, const char *title
,
2454 uint64_t attrs
, int out_of_range_attr
,
2455 const struct nlattr
*key
, size_t key_len
)
2460 if (VLOG_DROP_DBG(rl
)) {
2465 for (i
= 0; i
< 64; i
++) {
2466 if (attrs
& (UINT64_C(1) << i
)) {
2467 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
2469 ds_put_format(&s
, " %s",
2470 ovs_key_attr_to_string(i
, namebuf
, sizeof namebuf
));
2473 if (out_of_range_attr
) {
2474 ds_put_format(&s
, " %d (and possibly others)", out_of_range_attr
);
2477 ds_put_cstr(&s
, ": ");
2478 odp_flow_key_format(key
, key_len
, &s
);
2480 VLOG_DBG("%s:%s", title
, ds_cstr(&s
));
2485 odp_to_ovs_frag(uint8_t odp_frag
, struct flow
*flow
)
2487 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2489 if (odp_frag
> OVS_FRAG_TYPE_LATER
) {
2490 VLOG_ERR_RL(&rl
, "invalid frag %"PRIu8
" in flow key", odp_frag
);
2494 if (odp_frag
!= OVS_FRAG_TYPE_NONE
) {
2495 flow
->nw_frag
|= FLOW_NW_FRAG_ANY
;
2496 if (odp_frag
== OVS_FRAG_TYPE_LATER
) {
2497 flow
->nw_frag
|= FLOW_NW_FRAG_LATER
;
2504 parse_flow_nlattrs(const struct nlattr
*key
, size_t key_len
,
2505 const struct nlattr
*attrs
[], uint64_t *present_attrsp
,
2506 int *out_of_range_attrp
)
2508 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
2509 const struct nlattr
*nla
;
2510 uint64_t present_attrs
;
2513 BUILD_ASSERT(OVS_KEY_ATTR_MAX
< CHAR_BIT
* sizeof present_attrs
);
2515 *out_of_range_attrp
= 0;
2516 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
2517 uint16_t type
= nl_attr_type(nla
);
2518 size_t len
= nl_attr_get_size(nla
);
2519 int expected_len
= odp_flow_key_attr_len(type
);
2521 if (len
!= expected_len
&& expected_len
>= 0) {
2522 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
2524 VLOG_ERR_RL(&rl
, "attribute %s has length %zu but should have "
2525 "length %d", ovs_key_attr_to_string(type
, namebuf
,
2531 if (type
> OVS_KEY_ATTR_MAX
) {
2532 *out_of_range_attrp
= type
;
2534 if (present_attrs
& (UINT64_C(1) << type
)) {
2535 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
2537 VLOG_ERR_RL(&rl
, "duplicate %s attribute in flow key",
2538 ovs_key_attr_to_string(type
,
2539 namebuf
, sizeof namebuf
));
2543 present_attrs
|= UINT64_C(1) << type
;
2548 VLOG_ERR_RL(&rl
, "trailing garbage in flow key");
2552 *present_attrsp
= present_attrs
;
2556 static enum odp_key_fitness
2557 check_expectations(uint64_t present_attrs
, int out_of_range_attr
,
2558 uint64_t expected_attrs
,
2559 const struct nlattr
*key
, size_t key_len
)
2561 uint64_t missing_attrs
;
2562 uint64_t extra_attrs
;
2564 missing_attrs
= expected_attrs
& ~present_attrs
;
2565 if (missing_attrs
) {
2566 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
2567 log_odp_key_attributes(&rl
, "expected but not present",
2568 missing_attrs
, 0, key
, key_len
);
2569 return ODP_FIT_TOO_LITTLE
;
2572 extra_attrs
= present_attrs
& ~expected_attrs
;
2573 if (extra_attrs
|| out_of_range_attr
) {
2574 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
2575 log_odp_key_attributes(&rl
, "present but not expected",
2576 extra_attrs
, out_of_range_attr
, key
, key_len
);
2577 return ODP_FIT_TOO_MUCH
;
2580 return ODP_FIT_PERFECT
;
2584 parse_ethertype(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
2585 uint64_t present_attrs
, uint64_t *expected_attrs
,
2588 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2590 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
)) {
2591 flow
->dl_type
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_ETHERTYPE
]);
2592 if (ntohs(flow
->dl_type
) < 1536) {
2593 VLOG_ERR_RL(&rl
, "invalid Ethertype %"PRIu16
" in flow key",
2594 ntohs(flow
->dl_type
));
2597 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
;
2599 flow
->dl_type
= htons(FLOW_DL_TYPE_NONE
);
2604 static enum odp_key_fitness
2605 parse_l2_5_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
2606 uint64_t present_attrs
, int out_of_range_attr
,
2607 uint64_t expected_attrs
, struct flow
*flow
,
2608 const struct nlattr
*key
, size_t key_len
)
2610 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2612 if (eth_type_mpls(flow
->dl_type
)) {
2613 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_MPLS
);
2615 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
))) {
2616 return ODP_FIT_TOO_LITTLE
;
2618 flow
->mpls_lse
= nl_attr_get_be32(attrs
[OVS_KEY_ATTR_MPLS
]);
2620 } else if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
2621 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV4
;
2622 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
2623 const struct ovs_key_ipv4
*ipv4_key
;
2625 ipv4_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV4
]);
2626 flow
->nw_src
= ipv4_key
->ipv4_src
;
2627 flow
->nw_dst
= ipv4_key
->ipv4_dst
;
2628 flow
->nw_proto
= ipv4_key
->ipv4_proto
;
2629 flow
->nw_tos
= ipv4_key
->ipv4_tos
;
2630 flow
->nw_ttl
= ipv4_key
->ipv4_ttl
;
2631 if (!odp_to_ovs_frag(ipv4_key
->ipv4_frag
, flow
)) {
2632 return ODP_FIT_ERROR
;
2635 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
2636 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV6
;
2637 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
2638 const struct ovs_key_ipv6
*ipv6_key
;
2640 ipv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV6
]);
2641 memcpy(&flow
->ipv6_src
, ipv6_key
->ipv6_src
, sizeof flow
->ipv6_src
);
2642 memcpy(&flow
->ipv6_dst
, ipv6_key
->ipv6_dst
, sizeof flow
->ipv6_dst
);
2643 flow
->ipv6_label
= ipv6_key
->ipv6_label
;
2644 flow
->nw_proto
= ipv6_key
->ipv6_proto
;
2645 flow
->nw_tos
= ipv6_key
->ipv6_tclass
;
2646 flow
->nw_ttl
= ipv6_key
->ipv6_hlimit
;
2647 if (!odp_to_ovs_frag(ipv6_key
->ipv6_frag
, flow
)) {
2648 return ODP_FIT_ERROR
;
2651 } else if (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
2652 flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
2653 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ARP
;
2654 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ARP
)) {
2655 const struct ovs_key_arp
*arp_key
;
2657 arp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ARP
]);
2658 flow
->nw_src
= arp_key
->arp_sip
;
2659 flow
->nw_dst
= arp_key
->arp_tip
;
2660 if (arp_key
->arp_op
& htons(0xff00)) {
2661 VLOG_ERR_RL(&rl
, "unsupported ARP opcode %"PRIu16
" in flow "
2662 "key", ntohs(arp_key
->arp_op
));
2663 return ODP_FIT_ERROR
;
2665 flow
->nw_proto
= ntohs(arp_key
->arp_op
);
2666 memcpy(flow
->arp_sha
, arp_key
->arp_sha
, ETH_ADDR_LEN
);
2667 memcpy(flow
->arp_tha
, arp_key
->arp_tha
, ETH_ADDR_LEN
);
2671 if (flow
->nw_proto
== IPPROTO_TCP
2672 && (flow
->dl_type
== htons(ETH_TYPE_IP
) ||
2673 flow
->dl_type
== htons(ETH_TYPE_IPV6
))
2674 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
2675 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP
;
2676 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP
)) {
2677 const struct ovs_key_tcp
*tcp_key
;
2679 tcp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_TCP
]);
2680 flow
->tp_src
= tcp_key
->tcp_src
;
2681 flow
->tp_dst
= tcp_key
->tcp_dst
;
2683 } else if (flow
->nw_proto
== IPPROTO_UDP
2684 && (flow
->dl_type
== htons(ETH_TYPE_IP
) ||
2685 flow
->dl_type
== htons(ETH_TYPE_IPV6
))
2686 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
2687 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_UDP
;
2688 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_UDP
)) {
2689 const struct ovs_key_udp
*udp_key
;
2691 udp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_UDP
]);
2692 flow
->tp_src
= udp_key
->udp_src
;
2693 flow
->tp_dst
= udp_key
->udp_dst
;
2695 } else if (flow
->nw_proto
== IPPROTO_ICMP
2696 && flow
->dl_type
== htons(ETH_TYPE_IP
)
2697 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
2698 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMP
;
2699 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMP
)) {
2700 const struct ovs_key_icmp
*icmp_key
;
2702 icmp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMP
]);
2703 flow
->tp_src
= htons(icmp_key
->icmp_type
);
2704 flow
->tp_dst
= htons(icmp_key
->icmp_code
);
2706 } else if (flow
->nw_proto
== IPPROTO_ICMPV6
2707 && flow
->dl_type
== htons(ETH_TYPE_IPV6
)
2708 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
2709 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
;
2710 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
)) {
2711 const struct ovs_key_icmpv6
*icmpv6_key
;
2713 icmpv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMPV6
]);
2714 flow
->tp_src
= htons(icmpv6_key
->icmpv6_type
);
2715 flow
->tp_dst
= htons(icmpv6_key
->icmpv6_code
);
2717 if (flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
) ||
2718 flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
)) {
2719 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
2720 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ND
)) {
2721 const struct ovs_key_nd
*nd_key
;
2723 nd_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ND
]);
2724 memcpy(&flow
->nd_target
, nd_key
->nd_target
,
2725 sizeof flow
->nd_target
);
2726 memcpy(flow
->arp_sha
, nd_key
->nd_sll
, ETH_ADDR_LEN
);
2727 memcpy(flow
->arp_tha
, nd_key
->nd_tll
, ETH_ADDR_LEN
);
2733 return check_expectations(present_attrs
, out_of_range_attr
, expected_attrs
,
2737 /* Parse 802.1Q header then encapsulated L3 attributes. */
2738 static enum odp_key_fitness
2739 parse_8021q_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
2740 uint64_t present_attrs
, int out_of_range_attr
,
2741 uint64_t expected_attrs
, struct flow
*flow
,
2742 const struct nlattr
*key
, size_t key_len
)
2744 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2746 const struct nlattr
*encap
2747 = (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)
2748 ? attrs
[OVS_KEY_ATTR_ENCAP
] : NULL
);
2749 enum odp_key_fitness encap_fitness
;
2750 enum odp_key_fitness fitness
;
2753 /* Calculate fitness of outer attributes. */
2754 expected_attrs
|= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN
) |
2755 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
));
2756 fitness
= check_expectations(present_attrs
, out_of_range_attr
,
2757 expected_attrs
, key
, key_len
);
2759 /* Get the VLAN TCI value. */
2760 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
))) {
2761 return ODP_FIT_TOO_LITTLE
;
2763 tci
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
]);
2764 if (tci
== htons(0)) {
2765 /* Corner case for a truncated 802.1Q header. */
2766 if (fitness
== ODP_FIT_PERFECT
&& nl_attr_get_size(encap
)) {
2767 return ODP_FIT_TOO_MUCH
;
2770 } else if (!(tci
& htons(VLAN_CFI
))) {
2771 VLOG_ERR_RL(&rl
, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16
" is nonzero "
2772 "but CFI bit is not set", ntohs(tci
));
2773 return ODP_FIT_ERROR
;
2777 * Remove the TPID from dl_type since it's not the real Ethertype. */
2778 flow
->vlan_tci
= tci
;
2779 flow
->dl_type
= htons(0);
2781 /* Now parse the encapsulated attributes. */
2782 if (!parse_flow_nlattrs(nl_attr_get(encap
), nl_attr_get_size(encap
),
2783 attrs
, &present_attrs
, &out_of_range_attr
)) {
2784 return ODP_FIT_ERROR
;
2788 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
)) {
2789 return ODP_FIT_ERROR
;
2791 encap_fitness
= parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
2792 expected_attrs
, flow
, key
, key_len
);
2794 /* The overall fitness is the worse of the outer and inner attributes. */
2795 return MAX(fitness
, encap_fitness
);
2798 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
2799 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
2800 * 'key' fits our expectations for what a flow key should contain.
2802 * The 'in_port' will be the datapath's understanding of the port. The
2803 * caller will need to translate with odp_port_to_ofp_port() if the
2804 * OpenFlow port is needed.
2806 * This function doesn't take the packet itself as an argument because none of
2807 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
2808 * it is always possible to infer which additional attribute(s) should appear
2809 * by looking at the attributes for lower-level protocols, e.g. if the network
2810 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
2811 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
2812 * must be absent. */
2813 enum odp_key_fitness
2814 odp_flow_key_to_flow(const struct nlattr
*key
, size_t key_len
,
2817 const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1];
2818 uint64_t expected_attrs
;
2819 uint64_t present_attrs
;
2820 int out_of_range_attr
;
2822 memset(flow
, 0, sizeof *flow
);
2824 /* Parse attributes. */
2825 if (!parse_flow_nlattrs(key
, key_len
, attrs
, &present_attrs
,
2826 &out_of_range_attr
)) {
2827 return ODP_FIT_ERROR
;
2832 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
)) {
2833 flow
->skb_priority
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_PRIORITY
]);
2834 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
;
2837 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
)) {
2838 flow
->skb_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_SKB_MARK
]);
2839 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
;
2842 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
)) {
2843 enum odp_key_fitness res
;
2845 res
= odp_tun_key_from_attr(attrs
[OVS_KEY_ATTR_TUNNEL
], &flow
->tunnel
);
2846 if (res
== ODP_FIT_ERROR
) {
2847 return ODP_FIT_ERROR
;
2848 } else if (res
== ODP_FIT_PERFECT
) {
2849 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
;
2853 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
)) {
2854 flow
->in_port
.odp_port
2855 = nl_attr_get_odp_port(attrs
[OVS_KEY_ATTR_IN_PORT
]);
2856 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
;
2858 flow
->in_port
.odp_port
= ODPP_NONE
;
2861 /* Ethernet header. */
2862 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
)) {
2863 const struct ovs_key_ethernet
*eth_key
;
2865 eth_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ETHERNET
]);
2866 memcpy(flow
->dl_src
, eth_key
->eth_src
, ETH_ADDR_LEN
);
2867 memcpy(flow
->dl_dst
, eth_key
->eth_dst
, ETH_ADDR_LEN
);
2869 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
;
2871 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
2872 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
)) {
2873 return ODP_FIT_ERROR
;
2876 if (flow
->dl_type
== htons(ETH_TYPE_VLAN
)) {
2877 return parse_8021q_onward(attrs
, present_attrs
, out_of_range_attr
,
2878 expected_attrs
, flow
, key
, key_len
);
2880 return parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
2881 expected_attrs
, flow
, key
, key_len
);
2884 /* Returns 'fitness' as a string, for use in debug messages. */
2886 odp_key_fitness_to_string(enum odp_key_fitness fitness
)
2889 case ODP_FIT_PERFECT
:
2891 case ODP_FIT_TOO_MUCH
:
2893 case ODP_FIT_TOO_LITTLE
:
2894 return "too_little";
2902 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
2903 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
2904 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
2905 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
2906 * null, then the return value is not meaningful.) */
2908 odp_put_userspace_action(uint32_t pid
,
2909 const void *userdata
, size_t userdata_size
,
2910 struct ofpbuf
*odp_actions
)
2912 size_t userdata_ofs
;
2915 offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_USERSPACE
);
2916 nl_msg_put_u32(odp_actions
, OVS_USERSPACE_ATTR_PID
, pid
);
2918 userdata_ofs
= odp_actions
->size
+ NLA_HDRLEN
;
2919 nl_msg_put_unspec(odp_actions
, OVS_USERSPACE_ATTR_USERDATA
,
2920 userdata
, userdata_size
);
2924 nl_msg_end_nested(odp_actions
, offset
);
2926 return userdata_ofs
;
2930 odp_put_tunnel_action(const struct flow_tnl
*tunnel
,
2931 struct ofpbuf
*odp_actions
)
2933 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
2934 tun_key_to_attr(odp_actions
, tunnel
);
2935 nl_msg_end_nested(odp_actions
, offset
);
2938 /* The commit_odp_actions() function and its helpers. */
2941 commit_set_action(struct ofpbuf
*odp_actions
, enum ovs_key_attr key_type
,
2942 const void *key
, size_t key_size
)
2944 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
2945 nl_msg_put_unspec(odp_actions
, key_type
, key
, key_size
);
2946 nl_msg_end_nested(odp_actions
, offset
);
2950 odp_put_skb_mark_action(const uint32_t skb_mark
,
2951 struct ofpbuf
*odp_actions
)
2953 commit_set_action(odp_actions
, OVS_KEY_ATTR_SKB_MARK
, &skb_mark
,
2957 /* If any of the flow key data that ODP actions can modify are different in
2958 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
2959 * 'odp_actions' that change the flow tunneling information in key from
2960 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
2961 * same way. In other words, operates the same as commit_odp_actions(), but
2962 * only on tunneling information. */
2964 commit_odp_tunnel_action(const struct flow
*flow
, struct flow
*base
,
2965 struct ofpbuf
*odp_actions
)
2967 /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
2968 if (flow
->tunnel
.ip_dst
) {
2969 if (!memcmp(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
)) {
2972 memcpy(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
);
2973 odp_put_tunnel_action(&base
->tunnel
, odp_actions
);
2978 commit_set_ether_addr_action(const struct flow
*flow
, struct flow
*base
,
2979 struct ofpbuf
*odp_actions
,
2980 struct flow_wildcards
*wc
)
2982 struct ovs_key_ethernet eth_key
;
2984 if (eth_addr_equals(base
->dl_src
, flow
->dl_src
) &&
2985 eth_addr_equals(base
->dl_dst
, flow
->dl_dst
)) {
2989 memset(&wc
->masks
.dl_src
, 0xff, sizeof wc
->masks
.dl_src
);
2990 memset(&wc
->masks
.dl_dst
, 0xff, sizeof wc
->masks
.dl_dst
);
2992 memcpy(base
->dl_src
, flow
->dl_src
, ETH_ADDR_LEN
);
2993 memcpy(base
->dl_dst
, flow
->dl_dst
, ETH_ADDR_LEN
);
2995 memcpy(eth_key
.eth_src
, base
->dl_src
, ETH_ADDR_LEN
);
2996 memcpy(eth_key
.eth_dst
, base
->dl_dst
, ETH_ADDR_LEN
);
2998 commit_set_action(odp_actions
, OVS_KEY_ATTR_ETHERNET
,
2999 ð_key
, sizeof(eth_key
));
3003 commit_vlan_action(const struct flow
*flow
, struct flow
*base
,
3004 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
3006 if (base
->vlan_tci
== flow
->vlan_tci
) {
3010 memset(&wc
->masks
.vlan_tci
, 0xff, sizeof wc
->masks
.vlan_tci
);
3012 if (base
->vlan_tci
& htons(VLAN_CFI
)) {
3013 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_VLAN
);
3016 if (flow
->vlan_tci
& htons(VLAN_CFI
)) {
3017 struct ovs_action_push_vlan vlan
;
3019 vlan
.vlan_tpid
= htons(ETH_TYPE_VLAN
);
3020 vlan
.vlan_tci
= flow
->vlan_tci
;
3021 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
3022 &vlan
, sizeof vlan
);
3024 base
->vlan_tci
= flow
->vlan_tci
;
3028 commit_mpls_action(const struct flow
*flow
, struct flow
*base
,
3029 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
3031 if (flow
->mpls_lse
== base
->mpls_lse
&&
3032 flow
->mpls_depth
== base
->mpls_depth
) {
3036 memset(&wc
->masks
.mpls_lse
, 0xff, sizeof wc
->masks
.mpls_lse
);
3038 if (flow
->mpls_depth
< base
->mpls_depth
) {
3039 if (base
->mpls_depth
- flow
->mpls_depth
> 1) {
3040 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
3041 VLOG_WARN_RL(&rl
, "Multiple mpls_pop actions reduced to "
3042 " a single mpls_pop action");
3045 nl_msg_put_be16(odp_actions
, OVS_ACTION_ATTR_POP_MPLS
, flow
->dl_type
);
3046 } else if (flow
->mpls_depth
> base
->mpls_depth
) {
3047 struct ovs_action_push_mpls
*mpls
;
3049 if (flow
->mpls_depth
- base
->mpls_depth
> 1) {
3050 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
3051 VLOG_WARN_RL(&rl
, "Multiple mpls_push actions reduced to "
3052 " a single mpls_push action");
3055 mpls
= nl_msg_put_unspec_uninit(odp_actions
, OVS_ACTION_ATTR_PUSH_MPLS
,
3057 memset(mpls
, 0, sizeof *mpls
);
3058 mpls
->mpls_ethertype
= flow
->dl_type
;
3059 mpls
->mpls_lse
= flow
->mpls_lse
;
3061 struct ovs_key_mpls mpls_key
;
3063 mpls_key
.mpls_lse
= flow
->mpls_lse
;
3064 commit_set_action(odp_actions
, OVS_KEY_ATTR_MPLS
,
3065 &mpls_key
, sizeof(mpls_key
));
3068 base
->dl_type
= flow
->dl_type
;
3069 base
->mpls_lse
= flow
->mpls_lse
;
3070 base
->mpls_depth
= flow
->mpls_depth
;
3074 commit_set_ipv4_action(const struct flow
*flow
, struct flow
*base
,
3075 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
3077 struct ovs_key_ipv4 ipv4_key
;
3079 if (base
->nw_src
== flow
->nw_src
&&
3080 base
->nw_dst
== flow
->nw_dst
&&
3081 base
->nw_tos
== flow
->nw_tos
&&
3082 base
->nw_ttl
== flow
->nw_ttl
&&
3083 base
->nw_frag
== flow
->nw_frag
) {
3087 memset(&wc
->masks
.nw_src
, 0xff, sizeof wc
->masks
.nw_src
);
3088 memset(&wc
->masks
.nw_dst
, 0xff, sizeof wc
->masks
.nw_dst
);
3089 memset(&wc
->masks
.nw_tos
, 0xff, sizeof wc
->masks
.nw_tos
);
3090 memset(&wc
->masks
.nw_ttl
, 0xff, sizeof wc
->masks
.nw_ttl
);
3091 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
3092 memset(&wc
->masks
.nw_frag
, 0xff, sizeof wc
->masks
.nw_frag
);
3094 ipv4_key
.ipv4_src
= base
->nw_src
= flow
->nw_src
;
3095 ipv4_key
.ipv4_dst
= base
->nw_dst
= flow
->nw_dst
;
3096 ipv4_key
.ipv4_tos
= base
->nw_tos
= flow
->nw_tos
;
3097 ipv4_key
.ipv4_ttl
= base
->nw_ttl
= flow
->nw_ttl
;
3098 ipv4_key
.ipv4_proto
= base
->nw_proto
;
3099 ipv4_key
.ipv4_frag
= ovs_to_odp_frag(base
->nw_frag
);
3101 commit_set_action(odp_actions
, OVS_KEY_ATTR_IPV4
,
3102 &ipv4_key
, sizeof(ipv4_key
));
3106 commit_set_ipv6_action(const struct flow
*flow
, struct flow
*base
,
3107 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
3109 struct ovs_key_ipv6 ipv6_key
;
3111 if (ipv6_addr_equals(&base
->ipv6_src
, &flow
->ipv6_src
) &&
3112 ipv6_addr_equals(&base
->ipv6_dst
, &flow
->ipv6_dst
) &&
3113 base
->ipv6_label
== flow
->ipv6_label
&&
3114 base
->nw_tos
== flow
->nw_tos
&&
3115 base
->nw_ttl
== flow
->nw_ttl
&&
3116 base
->nw_frag
== flow
->nw_frag
) {
3120 memset(&wc
->masks
.ipv6_src
, 0xff, sizeof wc
->masks
.ipv6_src
);
3121 memset(&wc
->masks
.ipv6_dst
, 0xff, sizeof wc
->masks
.ipv6_dst
);
3122 memset(&wc
->masks
.ipv6_label
, 0xff, sizeof wc
->masks
.ipv6_label
);
3123 memset(&wc
->masks
.nw_tos
, 0xff, sizeof wc
->masks
.nw_tos
);
3124 memset(&wc
->masks
.nw_ttl
, 0xff, sizeof wc
->masks
.nw_ttl
);
3125 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
3126 memset(&wc
->masks
.nw_frag
, 0xff, sizeof wc
->masks
.nw_frag
);
3128 base
->ipv6_src
= flow
->ipv6_src
;
3129 memcpy(&ipv6_key
.ipv6_src
, &base
->ipv6_src
, sizeof(ipv6_key
.ipv6_src
));
3130 base
->ipv6_dst
= flow
->ipv6_dst
;
3131 memcpy(&ipv6_key
.ipv6_dst
, &base
->ipv6_dst
, sizeof(ipv6_key
.ipv6_dst
));
3133 ipv6_key
.ipv6_label
= base
->ipv6_label
= flow
->ipv6_label
;
3134 ipv6_key
.ipv6_tclass
= base
->nw_tos
= flow
->nw_tos
;
3135 ipv6_key
.ipv6_hlimit
= base
->nw_ttl
= flow
->nw_ttl
;
3136 ipv6_key
.ipv6_proto
= base
->nw_proto
;
3137 ipv6_key
.ipv6_frag
= ovs_to_odp_frag(base
->nw_frag
);
3139 commit_set_action(odp_actions
, OVS_KEY_ATTR_IPV6
,
3140 &ipv6_key
, sizeof(ipv6_key
));
3144 commit_set_nw_action(const struct flow
*flow
, struct flow
*base
,
3145 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
3147 /* Check if flow really have an IP header. */
3148 if (!flow
->nw_proto
) {
3152 if (base
->dl_type
== htons(ETH_TYPE_IP
)) {
3153 commit_set_ipv4_action(flow
, base
, odp_actions
, wc
);
3154 } else if (base
->dl_type
== htons(ETH_TYPE_IPV6
)) {
3155 commit_set_ipv6_action(flow
, base
, odp_actions
, wc
);
3160 commit_set_port_action(const struct flow
*flow
, struct flow
*base
,
3161 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
3163 if (!is_ip_any(base
) || (!base
->tp_src
&& !base
->tp_dst
)) {
3167 if (base
->tp_src
== flow
->tp_src
&&
3168 base
->tp_dst
== flow
->tp_dst
) {
3172 memset(&wc
->masks
.tp_src
, 0xff, sizeof wc
->masks
.tp_src
);
3173 memset(&wc
->masks
.tp_dst
, 0xff, sizeof wc
->masks
.tp_dst
);
3175 if (flow
->nw_proto
== IPPROTO_TCP
) {
3176 struct ovs_key_tcp port_key
;
3178 port_key
.tcp_src
= base
->tp_src
= flow
->tp_src
;
3179 port_key
.tcp_dst
= base
->tp_dst
= flow
->tp_dst
;
3181 commit_set_action(odp_actions
, OVS_KEY_ATTR_TCP
,
3182 &port_key
, sizeof(port_key
));
3184 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
3185 struct ovs_key_udp port_key
;
3187 port_key
.udp_src
= base
->tp_src
= flow
->tp_src
;
3188 port_key
.udp_dst
= base
->tp_dst
= flow
->tp_dst
;
3190 commit_set_action(odp_actions
, OVS_KEY_ATTR_UDP
,
3191 &port_key
, sizeof(port_key
));
3196 commit_set_priority_action(const struct flow
*flow
, struct flow
*base
,
3197 struct ofpbuf
*odp_actions
,
3198 struct flow_wildcards
*wc
)
3200 if (base
->skb_priority
== flow
->skb_priority
) {
3204 memset(&wc
->masks
.skb_priority
, 0xff, sizeof wc
->masks
.skb_priority
);
3205 base
->skb_priority
= flow
->skb_priority
;
3207 commit_set_action(odp_actions
, OVS_KEY_ATTR_PRIORITY
,
3208 &base
->skb_priority
, sizeof(base
->skb_priority
));
3212 commit_set_skb_mark_action(const struct flow
*flow
, struct flow
*base
,
3213 struct ofpbuf
*odp_actions
,
3214 struct flow_wildcards
*wc
)
3216 if (base
->skb_mark
== flow
->skb_mark
) {
3220 memset(&wc
->masks
.skb_mark
, 0xff, sizeof wc
->masks
.skb_mark
);
3221 base
->skb_mark
= flow
->skb_mark
;
3223 odp_put_skb_mark_action(base
->skb_mark
, odp_actions
);
3225 /* If any of the flow key data that ODP actions can modify are different in
3226 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
3227 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
3228 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
3229 * in addition to this function if needed. Sets fields in 'wc' that are
3230 * used as part of the action. */
3232 commit_odp_actions(const struct flow
*flow
, struct flow
*base
,
3233 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
3235 commit_set_ether_addr_action(flow
, base
, odp_actions
, wc
);
3236 commit_vlan_action(flow
, base
, odp_actions
, wc
);
3237 commit_set_nw_action(flow
, base
, odp_actions
, wc
);
3238 commit_set_port_action(flow
, base
, odp_actions
, wc
);
3239 /* Committing MPLS actions should occur after committing nw and port
3240 * actions. This is because committing MPLS actions may alter a packet so
3241 * that it is no longer IP and thus nw and port actions are no longer valid.
3243 commit_mpls_action(flow
, base
, odp_actions
, wc
);
3244 commit_set_priority_action(flow
, base
, odp_actions
, wc
);
3245 commit_set_skb_mark_action(flow
, base
, odp_actions
, wc
);