2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
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.
17 #include <arpa/inet.h>
22 #include <netinet/in.h>
23 #include <netinet/icmp6.h>
26 #include "byte-order.h"
28 #include "dynamic-string.h"
32 #include "openvswitch/tunnel.h"
38 VLOG_DEFINE_THIS_MODULE(odp_util);
40 /* The interface between userspace and kernel uses an "OVS_*" prefix.
41 * Since this is fairly non-specific for the OVS userspace components,
42 * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
43 * interactions with the datapath.
46 static void format_odp_key_attr(const struct nlattr *a, struct ds *ds);
48 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
51 * - For an action whose argument has a fixed length, returned that
52 * nonnegative length in bytes.
54 * - For an action with a variable-length argument, returns -2.
56 * - For an invalid 'type', returns -1. */
58 odp_action_len(uint16_t type)
60 if (type > OVS_ACTION_ATTR_MAX) {
64 switch ((enum ovs_action_attr) type) {
65 case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
66 case OVS_ACTION_ATTR_USERSPACE: return -2;
67 case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
68 case OVS_ACTION_ATTR_POP_VLAN: return 0;
69 case OVS_ACTION_ATTR_SET: return -2;
70 case OVS_ACTION_ATTR_SAMPLE: return -2;
72 case OVS_ACTION_ATTR_UNSPEC:
73 case __OVS_ACTION_ATTR_MAX:
81 ovs_key_attr_to_string(enum ovs_key_attr attr)
83 static char unknown_attr[3 + INT_STRLEN(unsigned int) + 1];
86 case OVS_KEY_ATTR_UNSPEC: return "unspec";
87 case OVS_KEY_ATTR_ENCAP: return "encap";
88 case OVS_KEY_ATTR_PRIORITY: return "priority";
89 case OVS_KEY_ATTR_IN_PORT: return "in_port";
90 case OVS_KEY_ATTR_ETHERNET: return "eth";
91 case OVS_KEY_ATTR_VLAN: return "vlan";
92 case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
93 case OVS_KEY_ATTR_IPV4: return "ipv4";
94 case OVS_KEY_ATTR_IPV6: return "ipv6";
95 case OVS_KEY_ATTR_TCP: return "tcp";
96 case OVS_KEY_ATTR_UDP: return "udp";
97 case OVS_KEY_ATTR_ICMP: return "icmp";
98 case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
99 case OVS_KEY_ATTR_ARP: return "arp";
100 case OVS_KEY_ATTR_ND: return "nd";
101 case OVS_KEY_ATTR_TUN_ID: return "tun_id";
103 case __OVS_KEY_ATTR_MAX:
105 snprintf(unknown_attr, sizeof unknown_attr, "key%u",
106 (unsigned int) attr);
112 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
114 size_t len = nl_attr_get_size(a);
116 ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
118 const uint8_t *unspec;
121 unspec = nl_attr_get(a);
122 for (i = 0; i < len; i++) {
123 ds_put_char(ds, i ? ' ': '(');
124 ds_put_format(ds, "%02x", unspec[i]);
126 ds_put_char(ds, ')');
131 format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
133 static const struct nl_policy ovs_sample_policy[] = {
134 [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 },
135 [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED }
137 struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
139 const struct nlattr *nla_acts;
142 ds_put_cstr(ds, "sample");
144 if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
145 ds_put_cstr(ds, "(error)");
149 percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
152 ds_put_format(ds, "(sample=%.1f%%,", percentage);
154 ds_put_cstr(ds, "actions(");
155 nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
156 len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
157 format_odp_actions(ds, nla_acts, len);
158 ds_put_format(ds, "))");
162 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
164 static const struct nl_policy ovs_userspace_policy[] = {
165 [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 },
166 [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
168 struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
170 if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
171 ds_put_cstr(ds, "userspace(error)");
175 ds_put_format(ds, "userspace(pid=%"PRIu32,
176 nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
178 if (a[OVS_USERSPACE_ATTR_USERDATA]) {
179 uint64_t userdata = nl_attr_get_u64(a[OVS_USERSPACE_ATTR_USERDATA]);
180 struct user_action_cookie cookie;
182 memcpy(&cookie, &userdata, sizeof cookie);
184 if (cookie.type == USER_ACTION_COOKIE_CONTROLLER) {
185 ds_put_format(ds, ",controller,length=%"PRIu32, cookie.data);
186 } else if (cookie.type == USER_ACTION_COOKIE_SFLOW) {
187 ds_put_format(ds, ",sFlow,n_output=%"PRIu8","
188 "vid=%"PRIu16",pcp=%"PRIu8",ifindex=%"PRIu32,
189 cookie.n_output, vlan_tci_to_vid(cookie.vlan_tci),
190 vlan_tci_to_pcp(cookie.vlan_tci), cookie.data);
192 ds_put_format(ds, ",userdata=0x%"PRIx64, userdata);
196 ds_put_char(ds, ')');
200 format_vlan_tci(struct ds *ds, ovs_be16 vlan_tci)
202 ds_put_format(ds, "vid=%"PRIu16",pcp=%d",
203 vlan_tci_to_vid(vlan_tci),
204 vlan_tci_to_pcp(vlan_tci));
205 if (!(vlan_tci & htons(VLAN_CFI))) {
206 ds_put_cstr(ds, ",cfi=0");
211 format_odp_action(struct ds *ds, const struct nlattr *a)
214 enum ovs_action_attr type = nl_attr_type(a);
215 const struct ovs_action_push_vlan *vlan;
217 expected_len = odp_action_len(nl_attr_type(a));
218 if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
219 ds_put_format(ds, "bad length %zu, expected %d for: ",
220 nl_attr_get_size(a), expected_len);
221 format_generic_odp_action(ds, a);
226 case OVS_ACTION_ATTR_OUTPUT:
227 ds_put_format(ds, "%"PRIu16, nl_attr_get_u32(a));
229 case OVS_ACTION_ATTR_USERSPACE:
230 format_odp_userspace_action(ds, a);
232 case OVS_ACTION_ATTR_SET:
233 ds_put_cstr(ds, "set(");
234 format_odp_key_attr(nl_attr_get(a), ds);
235 ds_put_cstr(ds, ")");
237 case OVS_ACTION_ATTR_PUSH_VLAN:
238 vlan = nl_attr_get(a);
239 ds_put_cstr(ds, "push_vlan(");
240 if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
241 ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
243 format_vlan_tci(ds, vlan->vlan_tci);
244 ds_put_char(ds, ')');
246 case OVS_ACTION_ATTR_POP_VLAN:
247 ds_put_cstr(ds, "pop_vlan");
249 case OVS_ACTION_ATTR_SAMPLE:
250 format_odp_sample_action(ds, a);
252 case OVS_ACTION_ATTR_UNSPEC:
253 case __OVS_ACTION_ATTR_MAX:
255 format_generic_odp_action(ds, a);
261 format_odp_actions(struct ds *ds, const struct nlattr *actions,
265 const struct nlattr *a;
268 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
270 ds_put_char(ds, ',');
272 format_odp_action(ds, a);
275 if (left == actions_len) {
276 ds_put_cstr(ds, "<empty>");
278 ds_put_format(ds, ",***%u leftover bytes***", left);
281 ds_put_cstr(ds, "drop");
285 /* Returns the correct length of the payload for a flow key attribute of the
286 * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
287 * is variable length. */
289 odp_flow_key_attr_len(uint16_t type)
291 if (type > OVS_KEY_ATTR_MAX) {
295 switch ((enum ovs_key_attr) type) {
296 case OVS_KEY_ATTR_ENCAP: return -2;
297 case OVS_KEY_ATTR_PRIORITY: return 4;
298 case OVS_KEY_ATTR_TUN_ID: return 8;
299 case OVS_KEY_ATTR_IN_PORT: return 4;
300 case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet);
301 case OVS_KEY_ATTR_VLAN: return sizeof(ovs_be16);
302 case OVS_KEY_ATTR_ETHERTYPE: return 2;
303 case OVS_KEY_ATTR_IPV4: return sizeof(struct ovs_key_ipv4);
304 case OVS_KEY_ATTR_IPV6: return sizeof(struct ovs_key_ipv6);
305 case OVS_KEY_ATTR_TCP: return sizeof(struct ovs_key_tcp);
306 case OVS_KEY_ATTR_UDP: return sizeof(struct ovs_key_udp);
307 case OVS_KEY_ATTR_ICMP: return sizeof(struct ovs_key_icmp);
308 case OVS_KEY_ATTR_ICMPV6: return sizeof(struct ovs_key_icmpv6);
309 case OVS_KEY_ATTR_ARP: return sizeof(struct ovs_key_arp);
310 case OVS_KEY_ATTR_ND: return sizeof(struct ovs_key_nd);
312 case OVS_KEY_ATTR_UNSPEC:
313 case __OVS_KEY_ATTR_MAX:
321 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
323 size_t len = nl_attr_get_size(a);
325 const uint8_t *unspec;
328 unspec = nl_attr_get(a);
329 for (i = 0; i < len; i++) {
330 ds_put_char(ds, i ? ' ': '(');
331 ds_put_format(ds, "%02x", unspec[i]);
333 ds_put_char(ds, ')');
338 ovs_frag_type_to_string(enum ovs_frag_type type)
341 case OVS_FRAG_TYPE_NONE:
343 case OVS_FRAG_TYPE_FIRST:
345 case OVS_FRAG_TYPE_LATER:
347 case __OVS_FRAG_TYPE_MAX:
354 format_odp_key_attr(const struct nlattr *a, struct ds *ds)
356 const struct ovs_key_ethernet *eth_key;
357 const struct ovs_key_ipv4 *ipv4_key;
358 const struct ovs_key_ipv6 *ipv6_key;
359 const struct ovs_key_tcp *tcp_key;
360 const struct ovs_key_udp *udp_key;
361 const struct ovs_key_icmp *icmp_key;
362 const struct ovs_key_icmpv6 *icmpv6_key;
363 const struct ovs_key_arp *arp_key;
364 const struct ovs_key_nd *nd_key;
365 enum ovs_key_attr attr = nl_attr_type(a);
368 ds_put_cstr(ds, ovs_key_attr_to_string(attr));
369 expected_len = odp_flow_key_attr_len(nl_attr_type(a));
370 if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
371 ds_put_format(ds, "(bad length %zu, expected %d)",
373 odp_flow_key_attr_len(nl_attr_type(a)));
374 format_generic_odp_key(a, ds);
379 case OVS_KEY_ATTR_ENCAP:
380 ds_put_cstr(ds, "(");
381 if (nl_attr_get_size(a)) {
382 odp_flow_key_format(nl_attr_get(a), nl_attr_get_size(a), ds);
384 ds_put_char(ds, ')');
387 case OVS_KEY_ATTR_PRIORITY:
388 ds_put_format(ds, "(%"PRIu32")", nl_attr_get_u32(a));
391 case OVS_KEY_ATTR_TUN_ID:
392 ds_put_format(ds, "(%#"PRIx64")", ntohll(nl_attr_get_be64(a)));
395 case OVS_KEY_ATTR_IN_PORT:
396 ds_put_format(ds, "(%"PRIu32")", nl_attr_get_u32(a));
399 case OVS_KEY_ATTR_ETHERNET:
400 eth_key = nl_attr_get(a);
401 ds_put_format(ds, "(src="ETH_ADDR_FMT",dst="ETH_ADDR_FMT")",
402 ETH_ADDR_ARGS(eth_key->eth_src),
403 ETH_ADDR_ARGS(eth_key->eth_dst));
406 case OVS_KEY_ATTR_VLAN:
407 ds_put_char(ds, '(');
408 format_vlan_tci(ds, nl_attr_get_be16(a));
409 ds_put_char(ds, ')');
412 case OVS_KEY_ATTR_ETHERTYPE:
413 ds_put_format(ds, "(0x%04"PRIx16")",
414 ntohs(nl_attr_get_be16(a)));
417 case OVS_KEY_ATTR_IPV4:
418 ipv4_key = nl_attr_get(a);
419 ds_put_format(ds, "(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
420 ",tos=%#"PRIx8",ttl=%"PRIu8",frag=%s)",
421 IP_ARGS(&ipv4_key->ipv4_src),
422 IP_ARGS(&ipv4_key->ipv4_dst),
423 ipv4_key->ipv4_proto, ipv4_key->ipv4_tos,
425 ovs_frag_type_to_string(ipv4_key->ipv4_frag));
428 case OVS_KEY_ATTR_IPV6: {
429 char src_str[INET6_ADDRSTRLEN];
430 char dst_str[INET6_ADDRSTRLEN];
432 ipv6_key = nl_attr_get(a);
433 inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
434 inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
436 ds_put_format(ds, "(src=%s,dst=%s,label=%#"PRIx32",proto=%"PRIu8
437 ",tclass=%#"PRIx8",hlimit=%"PRIu8",frag=%s)",
438 src_str, dst_str, ntohl(ipv6_key->ipv6_label),
439 ipv6_key->ipv6_proto, ipv6_key->ipv6_tclass,
440 ipv6_key->ipv6_hlimit,
441 ovs_frag_type_to_string(ipv6_key->ipv6_frag));
445 case OVS_KEY_ATTR_TCP:
446 tcp_key = nl_attr_get(a);
447 ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")",
448 ntohs(tcp_key->tcp_src), ntohs(tcp_key->tcp_dst));
451 case OVS_KEY_ATTR_UDP:
452 udp_key = nl_attr_get(a);
453 ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")",
454 ntohs(udp_key->udp_src), ntohs(udp_key->udp_dst));
457 case OVS_KEY_ATTR_ICMP:
458 icmp_key = nl_attr_get(a);
459 ds_put_format(ds, "(type=%"PRIu8",code=%"PRIu8")",
460 icmp_key->icmp_type, icmp_key->icmp_code);
463 case OVS_KEY_ATTR_ICMPV6:
464 icmpv6_key = nl_attr_get(a);
465 ds_put_format(ds, "(type=%"PRIu8",code=%"PRIu8")",
466 icmpv6_key->icmpv6_type, icmpv6_key->icmpv6_code);
469 case OVS_KEY_ATTR_ARP:
470 arp_key = nl_attr_get(a);
471 ds_put_format(ds, "(sip="IP_FMT",tip="IP_FMT",op=%"PRIu16","
472 "sha="ETH_ADDR_FMT",tha="ETH_ADDR_FMT")",
473 IP_ARGS(&arp_key->arp_sip), IP_ARGS(&arp_key->arp_tip),
474 ntohs(arp_key->arp_op), ETH_ADDR_ARGS(arp_key->arp_sha),
475 ETH_ADDR_ARGS(arp_key->arp_tha));
478 case OVS_KEY_ATTR_ND: {
479 char target[INET6_ADDRSTRLEN];
481 nd_key = nl_attr_get(a);
482 inet_ntop(AF_INET6, nd_key->nd_target, target, sizeof target);
484 ds_put_format(ds, "(target=%s", target);
485 if (!eth_addr_is_zero(nd_key->nd_sll)) {
486 ds_put_format(ds, ",sll="ETH_ADDR_FMT,
487 ETH_ADDR_ARGS(nd_key->nd_sll));
489 if (!eth_addr_is_zero(nd_key->nd_tll)) {
490 ds_put_format(ds, ",tll="ETH_ADDR_FMT,
491 ETH_ADDR_ARGS(nd_key->nd_tll));
493 ds_put_char(ds, ')');
497 case OVS_KEY_ATTR_UNSPEC:
498 case __OVS_KEY_ATTR_MAX:
500 format_generic_odp_key(a, ds);
505 /* Appends to 'ds' a string representation of the 'key_len' bytes of
506 * OVS_KEY_ATTR_* attributes in 'key'. */
508 odp_flow_key_format(const struct nlattr *key, size_t key_len, struct ds *ds)
511 const struct nlattr *a;
514 NL_ATTR_FOR_EACH (a, left, key, key_len) {
516 ds_put_char(ds, ',');
518 format_odp_key_attr(a, ds);
521 if (left == key_len) {
522 ds_put_cstr(ds, "<empty>");
524 ds_put_format(ds, ",***%u leftover bytes***", left);
527 ds_put_cstr(ds, "<empty>");
532 put_nd_key(int n, const char *nd_target_s,
533 const uint8_t *nd_sll, const uint8_t *nd_tll, struct ofpbuf *key)
535 struct ovs_key_nd nd_key;
537 memset(&nd_key, 0, sizeof nd_key);
538 if (inet_pton(AF_INET6, nd_target_s, nd_key.nd_target) != 1) {
542 memcpy(nd_key.nd_sll, nd_sll, ETH_ADDR_LEN);
545 memcpy(nd_key.nd_tll, nd_tll, ETH_ADDR_LEN);
547 nl_msg_put_unspec(key, OVS_KEY_ATTR_ND, &nd_key, sizeof nd_key);
552 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
554 if (!strcasecmp(s, "no")) {
555 *type = OVS_FRAG_TYPE_NONE;
556 } else if (!strcasecmp(s, "first")) {
557 *type = OVS_FRAG_TYPE_FIRST;
558 } else if (!strcasecmp(s, "later")) {
559 *type = OVS_FRAG_TYPE_LATER;
567 parse_odp_key_attr(const char *s, struct ofpbuf *key)
569 /* Many of the sscanf calls in this function use oversized destination
570 * fields because some sscanf() implementations truncate the range of %i
571 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
572 * value of 0x7fff. The other alternatives are to allow only a single
573 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
576 * The tun_id parser has to use an alternative approach because there is no
577 * type larger than 64 bits. */
580 unsigned long long int priority;
583 if (sscanf(s, "priority(%lli)%n", &priority, &n) > 0 && n > 0) {
584 nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority);
593 if (sscanf(s, "tun_id(%31[x0123456789abcdefABCDEF])%n",
594 tun_id_s, &n) > 0 && n > 0) {
595 uint64_t tun_id = strtoull(tun_id_s, NULL, 0);
596 nl_msg_put_be64(key, OVS_KEY_ATTR_TUN_ID, htonll(tun_id));
602 unsigned long long int in_port;
605 if (sscanf(s, "in_port(%lli)%n", &in_port, &n) > 0 && n > 0) {
606 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port);
612 struct ovs_key_ethernet eth_key;
616 "eth(src="ETH_ADDR_SCAN_FMT",dst="ETH_ADDR_SCAN_FMT")%n",
617 ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
618 ETH_ADDR_SCAN_ARGS(eth_key.eth_dst), &n) > 0 && n > 0) {
619 nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET,
620 ð_key, sizeof eth_key);
631 if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i)%n", &vid, &pcp, &n) > 0
633 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
634 htons((vid << VLAN_VID_SHIFT) |
635 (pcp << VLAN_PCP_SHIFT) |
638 } else if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i,cfi=%i)%n",
639 &vid, &pcp, &cfi, &n) > 0
641 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
642 htons((vid << VLAN_VID_SHIFT) |
643 (pcp << VLAN_PCP_SHIFT) |
644 (cfi ? VLAN_CFI : 0)));
653 if (sscanf(s, "eth_type(%i)%n", ð_type, &n) > 0 && n > 0) {
654 nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type));
666 enum ovs_frag_type ipv4_frag;
669 if (sscanf(s, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT","
670 "proto=%i,tos=%i,ttl=%i,frag=%7[a-z])%n",
671 IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_dst),
672 &ipv4_proto, &ipv4_tos, &ipv4_ttl, frag, &n) > 0
674 && ovs_frag_type_from_string(frag, &ipv4_frag)) {
675 struct ovs_key_ipv4 ipv4_key;
677 ipv4_key.ipv4_src = ipv4_src;
678 ipv4_key.ipv4_dst = ipv4_dst;
679 ipv4_key.ipv4_proto = ipv4_proto;
680 ipv4_key.ipv4_tos = ipv4_tos;
681 ipv4_key.ipv4_ttl = ipv4_ttl;
682 ipv4_key.ipv4_frag = ipv4_frag;
683 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
684 &ipv4_key, sizeof ipv4_key);
690 char ipv6_src_s[IPV6_SCAN_LEN + 1];
691 char ipv6_dst_s[IPV6_SCAN_LEN + 1];
697 enum ovs_frag_type ipv6_frag;
700 if (sscanf(s, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT","
701 "label=%i,proto=%i,tclass=%i,hlimit=%i,frag=%7[a-z])%n",
702 ipv6_src_s, ipv6_dst_s, &ipv6_label,
703 &ipv6_proto, &ipv6_tclass, &ipv6_hlimit, frag, &n) > 0
705 && ovs_frag_type_from_string(frag, &ipv6_frag)) {
706 struct ovs_key_ipv6 ipv6_key;
708 if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
709 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1) {
712 ipv6_key.ipv6_label = htonl(ipv6_label);
713 ipv6_key.ipv6_proto = ipv6_proto;
714 ipv6_key.ipv6_tclass = ipv6_tclass;
715 ipv6_key.ipv6_hlimit = ipv6_hlimit;
716 ipv6_key.ipv6_frag = ipv6_frag;
717 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
718 &ipv6_key, sizeof ipv6_key);
728 if (sscanf(s, "tcp(src=%i,dst=%i)%n",&tcp_src, &tcp_dst, &n) > 0
730 struct ovs_key_tcp tcp_key;
732 tcp_key.tcp_src = htons(tcp_src);
733 tcp_key.tcp_dst = htons(tcp_dst);
734 nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
744 if (sscanf(s, "udp(src=%i,dst=%i)%n", &udp_src, &udp_dst, &n) > 0
746 struct ovs_key_udp udp_key;
748 udp_key.udp_src = htons(udp_src);
749 udp_key.udp_dst = htons(udp_dst);
750 nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
760 if (sscanf(s, "icmp(type=%i,code=%i)%n",
761 &icmp_type, &icmp_code, &n) > 0
763 struct ovs_key_icmp icmp_key;
765 icmp_key.icmp_type = icmp_type;
766 icmp_key.icmp_code = icmp_code;
767 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
768 &icmp_key, sizeof icmp_key);
774 struct ovs_key_icmpv6 icmpv6_key;
777 if (sscanf(s, "icmpv6(type=%"SCNi8",code=%"SCNi8")%n",
778 &icmpv6_key.icmpv6_type, &icmpv6_key.icmpv6_code,&n) > 0
780 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
781 &icmpv6_key, sizeof icmpv6_key);
790 uint8_t arp_sha[ETH_ADDR_LEN];
791 uint8_t arp_tha[ETH_ADDR_LEN];
794 if (sscanf(s, "arp(sip="IP_SCAN_FMT",tip="IP_SCAN_FMT","
795 "op=%i,sha="ETH_ADDR_SCAN_FMT",tha="ETH_ADDR_SCAN_FMT")%n",
796 IP_SCAN_ARGS(&arp_sip),
797 IP_SCAN_ARGS(&arp_tip),
799 ETH_ADDR_SCAN_ARGS(arp_sha),
800 ETH_ADDR_SCAN_ARGS(arp_tha), &n) > 0 && n > 0) {
801 struct ovs_key_arp arp_key;
803 memset(&arp_key, 0, sizeof arp_key);
804 arp_key.arp_sip = arp_sip;
805 arp_key.arp_tip = arp_tip;
806 arp_key.arp_op = htons(arp_op);
807 memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN);
808 memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN);
809 nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
815 char nd_target_s[IPV6_SCAN_LEN + 1];
816 uint8_t nd_sll[ETH_ADDR_LEN];
817 uint8_t nd_tll[ETH_ADDR_LEN];
820 if (sscanf(s, "nd(target="IPV6_SCAN_FMT")%n",
821 nd_target_s, &n) > 0 && n > 0) {
822 return put_nd_key(n, nd_target_s, NULL, NULL, key);
824 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT")%n",
825 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), &n) > 0
827 return put_nd_key(n, nd_target_s, nd_sll, NULL, key);
829 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",tll="ETH_ADDR_SCAN_FMT")%n",
830 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
832 return put_nd_key(n, nd_target_s, NULL, nd_tll, key);
834 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT","
835 "tll="ETH_ADDR_SCAN_FMT")%n",
836 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll),
837 ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
839 return put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
843 if (!strncmp(s, "encap(", 6)) {
844 const char *start = s;
847 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
853 s += strspn(s, ", \t\r\n");
856 } else if (*s == ')') {
860 retval = parse_odp_key_attr(s, key);
868 nl_msg_end_nested(key, encap);
876 /* Parses the string representation of a datapath flow key, in the
877 * format output by odp_flow_key_format(). Returns 0 if successful,
878 * otherwise a positive errno value. On success, the flow key is
879 * appended to 'key' as a series of Netlink attributes. On failure, no
880 * data is appended to 'key'. Either way, 'key''s data might be
883 * On success, the attributes appended to 'key' are individually syntactically
884 * valid, but they may not be valid as a sequence. 'key' might, for example,
885 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
887 odp_flow_key_from_string(const char *s, struct ofpbuf *key)
889 const size_t old_size = key->size;
893 s += strspn(s, ", \t\r\n");
898 retval = parse_odp_key_attr(s, key);
900 key->size = old_size;
910 ovs_to_odp_frag(uint8_t ovs_frag)
912 return (ovs_frag & FLOW_NW_FRAG_LATER ? OVS_FRAG_TYPE_LATER
913 : ovs_frag & FLOW_NW_FRAG_ANY ? OVS_FRAG_TYPE_FIRST
914 : OVS_FRAG_TYPE_NONE);
917 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'. */
919 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow)
921 struct ovs_key_ethernet *eth_key;
924 if (flow->priority) {
925 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, flow->priority);
928 if (flow->tun_id != htonll(0)) {
929 nl_msg_put_be64(buf, OVS_KEY_ATTR_TUN_ID, flow->tun_id);
932 if (flow->in_port != OFPP_NONE) {
933 nl_msg_put_u32(buf, OVS_KEY_ATTR_IN_PORT,
934 ofp_port_to_odp_port(flow->in_port));
937 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
939 memcpy(eth_key->eth_src, flow->dl_src, ETH_ADDR_LEN);
940 memcpy(eth_key->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
942 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
943 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
944 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, flow->vlan_tci);
945 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
946 if (flow->vlan_tci == htons(0)) {
953 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
957 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, flow->dl_type);
959 if (flow->dl_type == htons(ETH_TYPE_IP)) {
960 struct ovs_key_ipv4 *ipv4_key;
962 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
964 ipv4_key->ipv4_src = flow->nw_src;
965 ipv4_key->ipv4_dst = flow->nw_dst;
966 ipv4_key->ipv4_proto = flow->nw_proto;
967 ipv4_key->ipv4_tos = flow->nw_tos;
968 ipv4_key->ipv4_ttl = flow->nw_ttl;
969 ipv4_key->ipv4_frag = ovs_to_odp_frag(flow->nw_frag);
970 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
971 struct ovs_key_ipv6 *ipv6_key;
973 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
975 memcpy(ipv6_key->ipv6_src, &flow->ipv6_src, sizeof ipv6_key->ipv6_src);
976 memcpy(ipv6_key->ipv6_dst, &flow->ipv6_dst, sizeof ipv6_key->ipv6_dst);
977 ipv6_key->ipv6_label = flow->ipv6_label;
978 ipv6_key->ipv6_proto = flow->nw_proto;
979 ipv6_key->ipv6_tclass = flow->nw_tos;
980 ipv6_key->ipv6_hlimit = flow->nw_ttl;
981 ipv6_key->ipv6_frag = ovs_to_odp_frag(flow->nw_frag);
982 } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
983 struct ovs_key_arp *arp_key;
985 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
987 memset(arp_key, 0, sizeof *arp_key);
988 arp_key->arp_sip = flow->nw_src;
989 arp_key->arp_tip = flow->nw_dst;
990 arp_key->arp_op = htons(flow->nw_proto);
991 memcpy(arp_key->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
992 memcpy(arp_key->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
995 if ((flow->dl_type == htons(ETH_TYPE_IP)
996 || flow->dl_type == htons(ETH_TYPE_IPV6))
997 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
999 if (flow->nw_proto == IPPROTO_TCP) {
1000 struct ovs_key_tcp *tcp_key;
1002 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
1004 tcp_key->tcp_src = flow->tp_src;
1005 tcp_key->tcp_dst = flow->tp_dst;
1006 } else if (flow->nw_proto == IPPROTO_UDP) {
1007 struct ovs_key_udp *udp_key;
1009 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
1011 udp_key->udp_src = flow->tp_src;
1012 udp_key->udp_dst = flow->tp_dst;
1013 } else if (flow->dl_type == htons(ETH_TYPE_IP)
1014 && flow->nw_proto == IPPROTO_ICMP) {
1015 struct ovs_key_icmp *icmp_key;
1017 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
1019 icmp_key->icmp_type = ntohs(flow->tp_src);
1020 icmp_key->icmp_code = ntohs(flow->tp_dst);
1021 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
1022 && flow->nw_proto == IPPROTO_ICMPV6) {
1023 struct ovs_key_icmpv6 *icmpv6_key;
1025 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
1026 sizeof *icmpv6_key);
1027 icmpv6_key->icmpv6_type = ntohs(flow->tp_src);
1028 icmpv6_key->icmpv6_code = ntohs(flow->tp_dst);
1030 if (icmpv6_key->icmpv6_type == ND_NEIGHBOR_SOLICIT
1031 || icmpv6_key->icmpv6_type == ND_NEIGHBOR_ADVERT) {
1032 struct ovs_key_nd *nd_key;
1034 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
1036 memcpy(nd_key->nd_target, &flow->nd_target,
1037 sizeof nd_key->nd_target);
1038 memcpy(nd_key->nd_sll, flow->arp_sha, ETH_ADDR_LEN);
1039 memcpy(nd_key->nd_tll, flow->arp_tha, ETH_ADDR_LEN);
1046 nl_msg_end_nested(buf, encap);
1051 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
1053 const struct nlattr *key, size_t key_len)
1058 if (VLOG_DROP_WARN(rl)) {
1063 ds_put_format(&s, "%s:", title);
1064 for (i = 0; i < 32; i++) {
1065 if (attrs & (1u << i)) {
1066 ds_put_format(&s, " %s", ovs_key_attr_to_string(i));
1070 ds_put_cstr(&s, ": ");
1071 odp_flow_key_format(key, key_len, &s);
1073 VLOG_WARN("%s", ds_cstr(&s));
1078 odp_to_ovs_frag(uint8_t odp_frag, struct flow *flow)
1080 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1082 if (odp_frag > OVS_FRAG_TYPE_LATER) {
1083 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key",
1088 if (odp_frag != OVS_FRAG_TYPE_NONE) {
1089 flow->nw_frag |= FLOW_NW_FRAG_ANY;
1090 if (odp_frag == OVS_FRAG_TYPE_LATER) {
1091 flow->nw_frag |= FLOW_NW_FRAG_LATER;
1098 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
1099 const struct nlattr *attrs[], uint64_t *present_attrsp)
1101 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1102 const struct nlattr *nla;
1103 uint64_t present_attrs;
1107 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
1108 uint16_t type = nl_attr_type(nla);
1109 size_t len = nl_attr_get_size(nla);
1110 int expected_len = odp_flow_key_attr_len(type);
1112 if (len != expected_len && expected_len != -2) {
1113 if (expected_len == -1) {
1114 VLOG_ERR_RL(&rl, "unknown attribute %"PRIu16" in flow key",
1117 VLOG_ERR_RL(&rl, "attribute %s has length %zu but should have "
1118 "length %d", ovs_key_attr_to_string(type),
1122 } else if (present_attrs & (UINT64_C(1) << type)) {
1123 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
1124 ovs_key_attr_to_string(type));
1128 present_attrs |= UINT64_C(1) << type;
1132 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
1136 *present_attrsp = present_attrs;
1141 check_expectations(uint64_t present_attrs, uint64_t expected_attrs,
1142 const struct nlattr *key, size_t key_len)
1144 uint64_t missing_attrs;
1145 uint64_t extra_attrs;
1147 missing_attrs = expected_attrs & ~present_attrs;
1148 if (missing_attrs) {
1149 static struct vlog_rate_limit miss_rl = VLOG_RATE_LIMIT_INIT(10, 10);
1150 log_odp_key_attributes(&miss_rl, "expected but not present",
1151 missing_attrs, key, key_len);
1155 extra_attrs = present_attrs & ~expected_attrs;
1157 static struct vlog_rate_limit extra_rl = VLOG_RATE_LIMIT_INIT(10, 10);
1158 log_odp_key_attributes(&extra_rl, "present but not expected",
1159 extra_attrs, key, key_len);
1166 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
1167 * structure in 'flow'. Returns 0 if successful, otherwise EINVAL. */
1169 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
1172 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1173 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
1174 uint64_t expected_attrs;
1175 uint64_t present_attrs;
1178 memset(flow, 0, sizeof *flow);
1180 error = parse_flow_nlattrs(key, key_len, attrs, &present_attrs);
1187 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
1188 flow->priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
1189 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
1192 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUN_ID)) {
1193 flow->tun_id = nl_attr_get_be64(attrs[OVS_KEY_ATTR_TUN_ID]);
1194 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUN_ID;
1197 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
1198 uint32_t in_port = nl_attr_get_u32(attrs[OVS_KEY_ATTR_IN_PORT]);
1199 if (in_port >= UINT16_MAX || in_port >= OFPP_MAX) {
1200 VLOG_ERR_RL(&rl, "in_port %"PRIu32" out of supported range",
1204 flow->in_port = odp_port_to_ofp_port(in_port);
1205 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
1207 flow->in_port = OFPP_NONE;
1210 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
1211 const struct ovs_key_ethernet *eth_key;
1213 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
1214 memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN);
1215 memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN);
1217 VLOG_ERR_RL(&rl, "missing Ethernet attribute in flow key");
1220 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
1222 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)
1223 && (nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE])
1224 == htons(ETH_TYPE_VLAN))) {
1225 /* The Ethernet type is 0x8100 so there must be a VLAN tag
1226 * and encapsulated protocol information. */
1227 const struct nlattr *encap;
1231 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE) |
1232 (UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
1233 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
1234 error = check_expectations(present_attrs, expected_attrs,
1240 encap = attrs[OVS_KEY_ATTR_ENCAP];
1241 tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
1242 if (tci & htons(VLAN_CFI)) {
1243 flow->vlan_tci = tci;
1245 error = parse_flow_nlattrs(nl_attr_get(encap),
1246 nl_attr_get_size(encap),
1247 attrs, &present_attrs);
1252 } else if (tci == htons(0)) {
1253 /* Corner case for a truncated 802.1Q header. */
1254 if (nl_attr_get_size(encap)) {
1258 flow->dl_type = htons(ETH_TYPE_VLAN);
1265 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
1266 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
1267 if (ntohs(flow->dl_type) < 1536) {
1268 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
1269 ntohs(flow->dl_type));
1272 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
1274 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
1277 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1278 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
1279 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
1280 const struct ovs_key_ipv4 *ipv4_key;
1282 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
1283 flow->nw_src = ipv4_key->ipv4_src;
1284 flow->nw_dst = ipv4_key->ipv4_dst;
1285 flow->nw_proto = ipv4_key->ipv4_proto;
1286 flow->nw_tos = ipv4_key->ipv4_tos;
1287 flow->nw_ttl = ipv4_key->ipv4_ttl;
1288 if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) {
1292 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1293 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
1294 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
1295 const struct ovs_key_ipv6 *ipv6_key;
1297 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
1298 memcpy(&flow->ipv6_src, ipv6_key->ipv6_src, sizeof flow->ipv6_src);
1299 memcpy(&flow->ipv6_dst, ipv6_key->ipv6_dst, sizeof flow->ipv6_dst);
1300 flow->ipv6_label = ipv6_key->ipv6_label;
1301 flow->nw_proto = ipv6_key->ipv6_proto;
1302 flow->nw_tos = ipv6_key->ipv6_tclass;
1303 flow->nw_ttl = ipv6_key->ipv6_hlimit;
1304 if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) {
1308 } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
1309 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
1310 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
1311 const struct ovs_key_arp *arp_key;
1313 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
1314 flow->nw_src = arp_key->arp_sip;
1315 flow->nw_dst = arp_key->arp_tip;
1316 if (arp_key->arp_op & htons(0xff00)) {
1317 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
1318 "key", ntohs(arp_key->arp_op));
1321 flow->nw_proto = ntohs(arp_key->arp_op);
1322 memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN);
1323 memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN);
1327 if (flow->nw_proto == IPPROTO_TCP
1328 && (flow->dl_type == htons(ETH_TYPE_IP) ||
1329 flow->dl_type == htons(ETH_TYPE_IPV6))
1330 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1331 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
1332 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
1333 const struct ovs_key_tcp *tcp_key;
1335 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
1336 flow->tp_src = tcp_key->tcp_src;
1337 flow->tp_dst = tcp_key->tcp_dst;
1339 } else if (flow->nw_proto == IPPROTO_UDP
1340 && (flow->dl_type == htons(ETH_TYPE_IP) ||
1341 flow->dl_type == htons(ETH_TYPE_IPV6))
1342 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1343 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
1344 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
1345 const struct ovs_key_udp *udp_key;
1347 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
1348 flow->tp_src = udp_key->udp_src;
1349 flow->tp_dst = udp_key->udp_dst;
1351 } else if (flow->nw_proto == IPPROTO_ICMP
1352 && flow->dl_type == htons(ETH_TYPE_IP)
1353 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1354 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
1355 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
1356 const struct ovs_key_icmp *icmp_key;
1358 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
1359 flow->tp_src = htons(icmp_key->icmp_type);
1360 flow->tp_dst = htons(icmp_key->icmp_code);
1362 } else if (flow->nw_proto == IPPROTO_ICMPV6
1363 && flow->dl_type == htons(ETH_TYPE_IPV6)
1364 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1365 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
1366 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
1367 const struct ovs_key_icmpv6 *icmpv6_key;
1369 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
1370 flow->tp_src = htons(icmpv6_key->icmpv6_type);
1371 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
1373 if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
1374 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
1375 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
1376 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
1377 const struct ovs_key_nd *nd_key;
1379 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
1380 memcpy(&flow->nd_target, nd_key->nd_target,
1381 sizeof flow->nd_target);
1382 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
1383 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
1389 return check_expectations(present_attrs, expected_attrs, key, key_len);