]> git.proxmox.com Git - mirror_ovs.git/blob - lib/odp-util.c
odp-util: Reorder OVS_KEY_ATTR_* cases for consistency.
[mirror_ovs.git] / lib / odp-util.c
1 /*
2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <arpa/inet.h>
18 #include <config.h>
19 #include "odp-util.h"
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <netinet/in.h>
23 #include <netinet/icmp6.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include "byte-order.h"
27 #include "coverage.h"
28 #include "dynamic-string.h"
29 #include "flow.h"
30 #include "netlink.h"
31 #include "ofpbuf.h"
32 #include "openvswitch/tunnel.h"
33 #include "packets.h"
34 #include "timeval.h"
35 #include "util.h"
36 #include "vlog.h"
37
38 VLOG_DEFINE_THIS_MODULE(odp_util);
39
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.
44 */
45
46 static void format_odp_key_attr(const struct nlattr *a, struct ds *ds);
47
48 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
49 * 'type':
50 *
51 * - For an action whose argument has a fixed length, returned that
52 * nonnegative length in bytes.
53 *
54 * - For an action with a variable-length argument, returns -2.
55 *
56 * - For an invalid 'type', returns -1. */
57 static int
58 odp_action_len(uint16_t type)
59 {
60 if (type > OVS_ACTION_ATTR_MAX) {
61 return -1;
62 }
63
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;
71
72 case OVS_ACTION_ATTR_UNSPEC:
73 case __OVS_ACTION_ATTR_MAX:
74 return -1;
75 }
76
77 return -1;
78 }
79
80 static const char *
81 ovs_key_attr_to_string(enum ovs_key_attr attr)
82 {
83 static char unknown_attr[3 + INT_STRLEN(unsigned int) + 1];
84
85 switch (attr) {
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";
102
103 case __OVS_KEY_ATTR_MAX:
104 default:
105 snprintf(unknown_attr, sizeof unknown_attr, "key%u",
106 (unsigned int) attr);
107 return unknown_attr;
108 }
109 }
110
111 static void
112 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
113 {
114 size_t len = nl_attr_get_size(a);
115
116 ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
117 if (len) {
118 const uint8_t *unspec;
119 unsigned int i;
120
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]);
125 }
126 ds_put_char(ds, ')');
127 }
128 }
129
130 static void
131 format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
132 {
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 }
136 };
137 struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
138 double percentage;
139 const struct nlattr *nla_acts;
140 int len;
141
142 ds_put_cstr(ds, "sample");
143
144 if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
145 ds_put_cstr(ds, "(error)");
146 return;
147 }
148
149 percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
150 UINT32_MAX;
151
152 ds_put_format(ds, "(sample=%.1f%%,", percentage);
153
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, "))");
159 }
160
161 static void
162 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
163 {
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 },
167 };
168 struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
169
170 if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
171 ds_put_cstr(ds, "userspace(error)");
172 return;
173 }
174
175 ds_put_format(ds, "userspace(pid=%"PRIu32,
176 nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
177
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;
181
182 memcpy(&cookie, &userdata, sizeof cookie);
183
184 if (cookie.type == USER_ACTION_COOKIE_CONTROLLER) {
185 ds_put_format(ds, ",controller,length=%"PRIu32,
186 cookie.data);
187 } else if (cookie.type == USER_ACTION_COOKIE_SFLOW) {
188 ds_put_format(ds, ",sFlow,n_output=%"PRIu8","
189 "vid=%"PRIu16",pcp=%"PRIu8",ifindex=%"PRIu32,
190 cookie.n_output, vlan_tci_to_vid(cookie.vlan_tci),
191 vlan_tci_to_pcp(cookie.vlan_tci), cookie.data);
192 } else {
193 ds_put_format(ds, ",userdata=0x%"PRIx64, userdata);
194 }
195 }
196
197 ds_put_char(ds, ')');
198 }
199
200 static void
201 format_vlan_tci(struct ds *ds, ovs_be16 vlan_tci)
202 {
203 ds_put_format(ds, "vid=%"PRIu16",pcp=%d",
204 vlan_tci_to_vid(vlan_tci),
205 vlan_tci_to_pcp(vlan_tci));
206 if (!(vlan_tci & htons(VLAN_CFI))) {
207 ds_put_cstr(ds, ",cfi=0");
208 }
209 }
210
211 static void
212 format_odp_action(struct ds *ds, const struct nlattr *a)
213 {
214 int expected_len;
215 enum ovs_action_attr type = nl_attr_type(a);
216 const struct ovs_action_push_vlan *vlan;
217
218 expected_len = odp_action_len(nl_attr_type(a));
219 if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
220 ds_put_format(ds, "bad length %zu, expected %d for: ",
221 nl_attr_get_size(a), expected_len);
222 format_generic_odp_action(ds, a);
223 return;
224 }
225
226 switch (type) {
227 case OVS_ACTION_ATTR_OUTPUT:
228 ds_put_format(ds, "%"PRIu16, nl_attr_get_u32(a));
229 break;
230 case OVS_ACTION_ATTR_USERSPACE:
231 format_odp_userspace_action(ds, a);
232 break;
233 case OVS_ACTION_ATTR_SET:
234 ds_put_cstr(ds, "set(");
235 format_odp_key_attr(nl_attr_get(a), ds);
236 ds_put_cstr(ds, ")");
237 break;
238 case OVS_ACTION_ATTR_PUSH_VLAN:
239 vlan = nl_attr_get(a);
240 ds_put_cstr(ds, "push_vlan(");
241 if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
242 ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
243 }
244 format_vlan_tci(ds, vlan->vlan_tci);
245 ds_put_char(ds, ')');
246 break;
247 case OVS_ACTION_ATTR_POP_VLAN:
248 ds_put_cstr(ds, "pop_vlan");
249 break;
250 case OVS_ACTION_ATTR_SAMPLE:
251 format_odp_sample_action(ds, a);
252 break;
253 case OVS_ACTION_ATTR_UNSPEC:
254 case __OVS_ACTION_ATTR_MAX:
255 default:
256 format_generic_odp_action(ds, a);
257 break;
258 }
259 }
260
261 void
262 format_odp_actions(struct ds *ds, const struct nlattr *actions,
263 size_t actions_len)
264 {
265 if (actions_len) {
266 const struct nlattr *a;
267 unsigned int left;
268
269 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
270 if (a != actions) {
271 ds_put_char(ds, ',');
272 }
273 format_odp_action(ds, a);
274 }
275 if (left) {
276 if (left == actions_len) {
277 ds_put_cstr(ds, "<empty>");
278 }
279 ds_put_format(ds, ",***%u leftover bytes***", left);
280 }
281 } else {
282 ds_put_cstr(ds, "drop");
283 }
284 }
285 \f
286 /* Returns the correct length of the payload for a flow key attribute of the
287 * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
288 * is variable length. */
289 static int
290 odp_flow_key_attr_len(uint16_t type)
291 {
292 if (type > OVS_KEY_ATTR_MAX) {
293 return -1;
294 }
295
296 switch ((enum ovs_key_attr) type) {
297 case OVS_KEY_ATTR_ENCAP: return -2;
298 case OVS_KEY_ATTR_PRIORITY: return 4;
299 case OVS_KEY_ATTR_TUN_ID: return 8;
300 case OVS_KEY_ATTR_IN_PORT: return 4;
301 case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet);
302 case OVS_KEY_ATTR_VLAN: return sizeof(ovs_be16);
303 case OVS_KEY_ATTR_ETHERTYPE: return 2;
304 case OVS_KEY_ATTR_IPV4: return sizeof(struct ovs_key_ipv4);
305 case OVS_KEY_ATTR_IPV6: return sizeof(struct ovs_key_ipv6);
306 case OVS_KEY_ATTR_TCP: return sizeof(struct ovs_key_tcp);
307 case OVS_KEY_ATTR_UDP: return sizeof(struct ovs_key_udp);
308 case OVS_KEY_ATTR_ICMP: return sizeof(struct ovs_key_icmp);
309 case OVS_KEY_ATTR_ICMPV6: return sizeof(struct ovs_key_icmpv6);
310 case OVS_KEY_ATTR_ARP: return sizeof(struct ovs_key_arp);
311 case OVS_KEY_ATTR_ND: return sizeof(struct ovs_key_nd);
312
313 case OVS_KEY_ATTR_UNSPEC:
314 case __OVS_KEY_ATTR_MAX:
315 return -1;
316 }
317
318 return -1;
319 }
320
321 static void
322 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
323 {
324 size_t len = nl_attr_get_size(a);
325 if (len) {
326 const uint8_t *unspec;
327 unsigned int i;
328
329 unspec = nl_attr_get(a);
330 for (i = 0; i < len; i++) {
331 ds_put_char(ds, i ? ' ': '(');
332 ds_put_format(ds, "%02x", unspec[i]);
333 }
334 ds_put_char(ds, ')');
335 }
336 }
337
338 static const char *
339 ovs_frag_type_to_string(enum ovs_frag_type type)
340 {
341 switch (type) {
342 case OVS_FRAG_TYPE_NONE:
343 return "no";
344 case OVS_FRAG_TYPE_FIRST:
345 return "first";
346 case OVS_FRAG_TYPE_LATER:
347 return "later";
348 case __OVS_FRAG_TYPE_MAX:
349 default:
350 return "<error>";
351 }
352 }
353
354 static void
355 format_odp_key_attr(const struct nlattr *a, struct ds *ds)
356 {
357 const struct ovs_key_ethernet *eth_key;
358 const struct ovs_key_ipv4 *ipv4_key;
359 const struct ovs_key_ipv6 *ipv6_key;
360 const struct ovs_key_tcp *tcp_key;
361 const struct ovs_key_udp *udp_key;
362 const struct ovs_key_icmp *icmp_key;
363 const struct ovs_key_icmpv6 *icmpv6_key;
364 const struct ovs_key_arp *arp_key;
365 const struct ovs_key_nd *nd_key;
366 enum ovs_key_attr attr = nl_attr_type(a);
367 int expected_len;
368
369 ds_put_cstr(ds, ovs_key_attr_to_string(attr));
370 expected_len = odp_flow_key_attr_len(nl_attr_type(a));
371 if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
372 ds_put_format(ds, "(bad length %zu, expected %d)",
373 nl_attr_get_size(a),
374 odp_flow_key_attr_len(nl_attr_type(a)));
375 format_generic_odp_key(a, ds);
376 return;
377 }
378
379 switch (attr) {
380 case OVS_KEY_ATTR_ENCAP:
381 ds_put_cstr(ds, "(");
382 if (nl_attr_get_size(a)) {
383 odp_flow_key_format(nl_attr_get(a), nl_attr_get_size(a), ds);
384 }
385 ds_put_char(ds, ')');
386 break;
387
388 case OVS_KEY_ATTR_PRIORITY:
389 ds_put_format(ds, "(%"PRIu32")", nl_attr_get_u32(a));
390 break;
391
392 case OVS_KEY_ATTR_TUN_ID:
393 ds_put_format(ds, "(%#"PRIx64")", ntohll(nl_attr_get_be64(a)));
394 break;
395
396 case OVS_KEY_ATTR_IN_PORT:
397 ds_put_format(ds, "(%"PRIu32")", nl_attr_get_u32(a));
398 break;
399
400 case OVS_KEY_ATTR_ETHERNET:
401 eth_key = nl_attr_get(a);
402 ds_put_format(ds, "(src="ETH_ADDR_FMT",dst="ETH_ADDR_FMT")",
403 ETH_ADDR_ARGS(eth_key->eth_src),
404 ETH_ADDR_ARGS(eth_key->eth_dst));
405 break;
406
407 case OVS_KEY_ATTR_VLAN:
408 ds_put_char(ds, '(');
409 format_vlan_tci(ds, nl_attr_get_be16(a));
410 ds_put_char(ds, ')');
411 break;
412
413 case OVS_KEY_ATTR_ETHERTYPE:
414 ds_put_format(ds, "(0x%04"PRIx16")",
415 ntohs(nl_attr_get_be16(a)));
416 break;
417
418 case OVS_KEY_ATTR_IPV4:
419 ipv4_key = nl_attr_get(a);
420 ds_put_format(ds, "(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
421 ",tos=%#"PRIx8",ttl=%"PRIu8",frag=%s)",
422 IP_ARGS(&ipv4_key->ipv4_src),
423 IP_ARGS(&ipv4_key->ipv4_dst),
424 ipv4_key->ipv4_proto, ipv4_key->ipv4_tos,
425 ipv4_key->ipv4_ttl,
426 ovs_frag_type_to_string(ipv4_key->ipv4_frag));
427 break;
428
429 case OVS_KEY_ATTR_IPV6: {
430 char src_str[INET6_ADDRSTRLEN];
431 char dst_str[INET6_ADDRSTRLEN];
432
433 ipv6_key = nl_attr_get(a);
434 inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
435 inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
436
437 ds_put_format(ds, "(src=%s,dst=%s,label=%#"PRIx32",proto=%"PRIu8
438 ",tclass=%#"PRIx8",hlimit=%"PRIu8",frag=%s)",
439 src_str, dst_str, ntohl(ipv6_key->ipv6_label),
440 ipv6_key->ipv6_proto, ipv6_key->ipv6_tclass,
441 ipv6_key->ipv6_hlimit,
442 ovs_frag_type_to_string(ipv6_key->ipv6_frag));
443 break;
444 }
445
446 case OVS_KEY_ATTR_TCP:
447 tcp_key = nl_attr_get(a);
448 ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")",
449 ntohs(tcp_key->tcp_src), ntohs(tcp_key->tcp_dst));
450 break;
451
452 case OVS_KEY_ATTR_UDP:
453 udp_key = nl_attr_get(a);
454 ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")",
455 ntohs(udp_key->udp_src), ntohs(udp_key->udp_dst));
456 break;
457
458 case OVS_KEY_ATTR_ICMP:
459 icmp_key = nl_attr_get(a);
460 ds_put_format(ds, "(type=%"PRIu8",code=%"PRIu8")",
461 icmp_key->icmp_type, icmp_key->icmp_code);
462 break;
463
464 case OVS_KEY_ATTR_ICMPV6:
465 icmpv6_key = nl_attr_get(a);
466 ds_put_format(ds, "(type=%"PRIu8",code=%"PRIu8")",
467 icmpv6_key->icmpv6_type, icmpv6_key->icmpv6_code);
468 break;
469
470 case OVS_KEY_ATTR_ARP:
471 arp_key = nl_attr_get(a);
472 ds_put_format(ds, "(sip="IP_FMT",tip="IP_FMT",op=%"PRIu16","
473 "sha="ETH_ADDR_FMT",tha="ETH_ADDR_FMT")",
474 IP_ARGS(&arp_key->arp_sip), IP_ARGS(&arp_key->arp_tip),
475 ntohs(arp_key->arp_op), ETH_ADDR_ARGS(arp_key->arp_sha),
476 ETH_ADDR_ARGS(arp_key->arp_tha));
477 break;
478
479 case OVS_KEY_ATTR_ND: {
480 char target[INET6_ADDRSTRLEN];
481
482 nd_key = nl_attr_get(a);
483 inet_ntop(AF_INET6, nd_key->nd_target, target, sizeof target);
484
485 ds_put_format(ds, "(target=%s", target);
486 if (!eth_addr_is_zero(nd_key->nd_sll)) {
487 ds_put_format(ds, ",sll="ETH_ADDR_FMT,
488 ETH_ADDR_ARGS(nd_key->nd_sll));
489 }
490 if (!eth_addr_is_zero(nd_key->nd_tll)) {
491 ds_put_format(ds, ",tll="ETH_ADDR_FMT,
492 ETH_ADDR_ARGS(nd_key->nd_tll));
493 }
494 ds_put_char(ds, ')');
495 break;
496 }
497
498 case OVS_KEY_ATTR_UNSPEC:
499 case __OVS_KEY_ATTR_MAX:
500 default:
501 format_generic_odp_key(a, ds);
502 break;
503 }
504 }
505
506 /* Appends to 'ds' a string representation of the 'key_len' bytes of
507 * OVS_KEY_ATTR_* attributes in 'key'. */
508 void
509 odp_flow_key_format(const struct nlattr *key, size_t key_len, struct ds *ds)
510 {
511 if (key_len) {
512 const struct nlattr *a;
513 unsigned int left;
514
515 NL_ATTR_FOR_EACH (a, left, key, key_len) {
516 if (a != key) {
517 ds_put_char(ds, ',');
518 }
519 format_odp_key_attr(a, ds);
520 }
521 if (left) {
522 if (left == key_len) {
523 ds_put_cstr(ds, "<empty>");
524 }
525 ds_put_format(ds, ",***%u leftover bytes***", left);
526 }
527 } else {
528 ds_put_cstr(ds, "<empty>");
529 }
530 }
531
532 static int
533 put_nd_key(int n, const char *nd_target_s,
534 const uint8_t *nd_sll, const uint8_t *nd_tll, struct ofpbuf *key)
535 {
536 struct ovs_key_nd nd_key;
537
538 memset(&nd_key, 0, sizeof nd_key);
539 if (inet_pton(AF_INET6, nd_target_s, nd_key.nd_target) != 1) {
540 return -EINVAL;
541 }
542 if (nd_sll) {
543 memcpy(nd_key.nd_sll, nd_sll, ETH_ADDR_LEN);
544 }
545 if (nd_tll) {
546 memcpy(nd_key.nd_tll, nd_tll, ETH_ADDR_LEN);
547 }
548 nl_msg_put_unspec(key, OVS_KEY_ATTR_ND, &nd_key, sizeof nd_key);
549 return n;
550 }
551
552 static bool
553 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
554 {
555 if (!strcasecmp(s, "no")) {
556 *type = OVS_FRAG_TYPE_NONE;
557 } else if (!strcasecmp(s, "first")) {
558 *type = OVS_FRAG_TYPE_FIRST;
559 } else if (!strcasecmp(s, "later")) {
560 *type = OVS_FRAG_TYPE_LATER;
561 } else {
562 return false;
563 }
564 return true;
565 }
566
567 static int
568 parse_odp_key_attr(const char *s, struct ofpbuf *key)
569 {
570 /* Many of the sscanf calls in this function use oversized destination
571 * fields because some sscanf() implementations truncate the range of %i
572 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
573 * value of 0x7fff. The other alternatives are to allow only a single
574 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
575 * parsers.
576 *
577 * The tun_id parser has to use an alternative approach because there is no
578 * type larger than 64 bits. */
579
580 {
581 unsigned long long int priority;
582 int n = -1;
583
584 if (sscanf(s, "priority(%lli)%n", &priority, &n) > 0 && n > 0) {
585 nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority);
586 return n;
587 }
588 }
589
590 {
591 char tun_id_s[32];
592 int n = -1;
593
594 if (sscanf(s, "tun_id(%31[x0123456789abcdefABCDEF])%n",
595 tun_id_s, &n) > 0 && n > 0) {
596 uint64_t tun_id = strtoull(tun_id_s, NULL, 0);
597 nl_msg_put_be64(key, OVS_KEY_ATTR_TUN_ID, htonll(tun_id));
598 return n;
599 }
600 }
601
602 {
603 unsigned long long int in_port;
604 int n = -1;
605
606 if (sscanf(s, "in_port(%lli)%n", &in_port, &n) > 0 && n > 0) {
607 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port);
608 return n;
609 }
610 }
611
612 {
613 struct ovs_key_ethernet eth_key;
614 int n = -1;
615
616 if (sscanf(s,
617 "eth(src="ETH_ADDR_SCAN_FMT",dst="ETH_ADDR_SCAN_FMT")%n",
618 ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
619 ETH_ADDR_SCAN_ARGS(eth_key.eth_dst), &n) > 0 && n > 0) {
620 nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET,
621 &eth_key, sizeof eth_key);
622 return n;
623 }
624 }
625
626 {
627 uint16_t vid;
628 int pcp;
629 int cfi;
630 int n = -1;
631
632 if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i)%n", &vid, &pcp, &n) > 0
633 && n > 0)) {
634 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
635 htons((vid << VLAN_VID_SHIFT) |
636 (pcp << VLAN_PCP_SHIFT) |
637 VLAN_CFI));
638 return n;
639 } else if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i,cfi=%i)%n",
640 &vid, &pcp, &cfi, &n) > 0
641 && n > 0)) {
642 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
643 htons((vid << VLAN_VID_SHIFT) |
644 (pcp << VLAN_PCP_SHIFT) |
645 (cfi ? VLAN_CFI : 0)));
646 return n;
647 }
648 }
649
650 {
651 int eth_type;
652 int n = -1;
653
654 if (sscanf(s, "eth_type(%i)%n", &eth_type, &n) > 0 && n > 0) {
655 nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type));
656 return n;
657 }
658 }
659
660 {
661 ovs_be32 ipv4_src;
662 ovs_be32 ipv4_dst;
663 int ipv4_proto;
664 int ipv4_tos;
665 int ipv4_ttl;
666 char frag[8];
667 enum ovs_frag_type ipv4_frag;
668 int n = -1;
669
670 if (sscanf(s, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT","
671 "proto=%i,tos=%i,ttl=%i,frag=%7[a-z])%n",
672 IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_dst),
673 &ipv4_proto, &ipv4_tos, &ipv4_ttl, frag, &n) > 0
674 && n > 0
675 && ovs_frag_type_from_string(frag, &ipv4_frag)) {
676 struct ovs_key_ipv4 ipv4_key;
677
678 ipv4_key.ipv4_src = ipv4_src;
679 ipv4_key.ipv4_dst = ipv4_dst;
680 ipv4_key.ipv4_proto = ipv4_proto;
681 ipv4_key.ipv4_tos = ipv4_tos;
682 ipv4_key.ipv4_ttl = ipv4_ttl;
683 ipv4_key.ipv4_frag = ipv4_frag;
684 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
685 &ipv4_key, sizeof ipv4_key);
686 return n;
687 }
688 }
689
690 {
691 char ipv6_src_s[IPV6_SCAN_LEN + 1];
692 char ipv6_dst_s[IPV6_SCAN_LEN + 1];
693 int ipv6_label;
694 int ipv6_proto;
695 int ipv6_tclass;
696 int ipv6_hlimit;
697 char frag[8];
698 enum ovs_frag_type ipv6_frag;
699 int n = -1;
700
701 if (sscanf(s, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT","
702 "label=%i,proto=%i,tclass=%i,hlimit=%i,frag=%7[a-z])%n",
703 ipv6_src_s, ipv6_dst_s, &ipv6_label,
704 &ipv6_proto, &ipv6_tclass, &ipv6_hlimit, frag, &n) > 0
705 && n > 0
706 && ovs_frag_type_from_string(frag, &ipv6_frag)) {
707 struct ovs_key_ipv6 ipv6_key;
708
709 if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
710 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1) {
711 return -EINVAL;
712 }
713 ipv6_key.ipv6_label = htonl(ipv6_label);
714 ipv6_key.ipv6_proto = ipv6_proto;
715 ipv6_key.ipv6_tclass = ipv6_tclass;
716 ipv6_key.ipv6_hlimit = ipv6_hlimit;
717 ipv6_key.ipv6_frag = ipv6_frag;
718 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
719 &ipv6_key, sizeof ipv6_key);
720 return n;
721 }
722 }
723
724 {
725 int tcp_src;
726 int tcp_dst;
727 int n = -1;
728
729 if (sscanf(s, "tcp(src=%i,dst=%i)%n",&tcp_src, &tcp_dst, &n) > 0
730 && n > 0) {
731 struct ovs_key_tcp tcp_key;
732
733 tcp_key.tcp_src = htons(tcp_src);
734 tcp_key.tcp_dst = htons(tcp_dst);
735 nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
736 return n;
737 }
738 }
739
740 {
741 int udp_src;
742 int udp_dst;
743 int n = -1;
744
745 if (sscanf(s, "udp(src=%i,dst=%i)%n", &udp_src, &udp_dst, &n) > 0
746 && n > 0) {
747 struct ovs_key_udp udp_key;
748
749 udp_key.udp_src = htons(udp_src);
750 udp_key.udp_dst = htons(udp_dst);
751 nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
752 return n;
753 }
754 }
755
756 {
757 int icmp_type;
758 int icmp_code;
759 int n = -1;
760
761 if (sscanf(s, "icmp(type=%i,code=%i)%n",
762 &icmp_type, &icmp_code, &n) > 0
763 && n > 0) {
764 struct ovs_key_icmp icmp_key;
765
766 icmp_key.icmp_type = icmp_type;
767 icmp_key.icmp_code = icmp_code;
768 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
769 &icmp_key, sizeof icmp_key);
770 return n;
771 }
772 }
773
774 {
775 struct ovs_key_icmpv6 icmpv6_key;
776 int n = -1;
777
778 if (sscanf(s, "icmpv6(type=%"SCNi8",code=%"SCNi8")%n",
779 &icmpv6_key.icmpv6_type, &icmpv6_key.icmpv6_code,&n) > 0
780 && n > 0) {
781 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
782 &icmpv6_key, sizeof icmpv6_key);
783 return n;
784 }
785 }
786
787 {
788 ovs_be32 arp_sip;
789 ovs_be32 arp_tip;
790 int arp_op;
791 uint8_t arp_sha[ETH_ADDR_LEN];
792 uint8_t arp_tha[ETH_ADDR_LEN];
793 int n = -1;
794
795 if (sscanf(s, "arp(sip="IP_SCAN_FMT",tip="IP_SCAN_FMT","
796 "op=%i,sha="ETH_ADDR_SCAN_FMT",tha="ETH_ADDR_SCAN_FMT")%n",
797 IP_SCAN_ARGS(&arp_sip),
798 IP_SCAN_ARGS(&arp_tip),
799 &arp_op,
800 ETH_ADDR_SCAN_ARGS(arp_sha),
801 ETH_ADDR_SCAN_ARGS(arp_tha), &n) > 0 && n > 0) {
802 struct ovs_key_arp arp_key;
803
804 memset(&arp_key, 0, sizeof arp_key);
805 arp_key.arp_sip = arp_sip;
806 arp_key.arp_tip = arp_tip;
807 arp_key.arp_op = htons(arp_op);
808 memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN);
809 memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN);
810 nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
811 return n;
812 }
813 }
814
815 {
816 char nd_target_s[IPV6_SCAN_LEN + 1];
817 uint8_t nd_sll[ETH_ADDR_LEN];
818 uint8_t nd_tll[ETH_ADDR_LEN];
819 int n = -1;
820
821 if (sscanf(s, "nd(target="IPV6_SCAN_FMT")%n",
822 nd_target_s, &n) > 0 && n > 0) {
823 return put_nd_key(n, nd_target_s, NULL, NULL, key);
824 }
825 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT")%n",
826 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), &n) > 0
827 && n > 0) {
828 return put_nd_key(n, nd_target_s, nd_sll, NULL, key);
829 }
830 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",tll="ETH_ADDR_SCAN_FMT")%n",
831 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
832 && n > 0) {
833 return put_nd_key(n, nd_target_s, NULL, nd_tll, key);
834 }
835 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT","
836 "tll="ETH_ADDR_SCAN_FMT")%n",
837 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll),
838 ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
839 && n > 0) {
840 return put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
841 }
842 }
843
844 if (!strncmp(s, "encap(", 6)) {
845 const char *start = s;
846 size_t encap;
847
848 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
849
850 s += 6;
851 for (;;) {
852 int retval;
853
854 s += strspn(s, ", \t\r\n");
855 if (!*s) {
856 return -EINVAL;
857 } else if (*s == ')') {
858 break;
859 }
860
861 retval = parse_odp_key_attr(s, key);
862 if (retval < 0) {
863 return retval;
864 }
865 s += retval;
866 }
867 s++;
868
869 nl_msg_end_nested(key, encap);
870
871 return s - start;
872 }
873
874 return -EINVAL;
875 }
876
877 /* Parses the string representation of a datapath flow key, in the
878 * format output by odp_flow_key_format(). Returns 0 if successful,
879 * otherwise a positive errno value. On success, the flow key is
880 * appended to 'key' as a series of Netlink attributes. On failure, no
881 * data is appended to 'key'. Either way, 'key''s data might be
882 * reallocated.
883 *
884 * On success, the attributes appended to 'key' are individually syntactically
885 * valid, but they may not be valid as a sequence. 'key' might, for example,
886 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
887 int
888 odp_flow_key_from_string(const char *s, struct ofpbuf *key)
889 {
890 const size_t old_size = key->size;
891 for (;;) {
892 int retval;
893
894 s += strspn(s, ", \t\r\n");
895 if (!*s) {
896 return 0;
897 }
898
899 retval = parse_odp_key_attr(s, key);
900 if (retval < 0) {
901 key->size = old_size;
902 return -retval;
903 }
904 s += retval;
905 }
906
907 return 0;
908 }
909
910 static uint8_t
911 ovs_to_odp_frag(uint8_t ovs_frag)
912 {
913 return (ovs_frag & FLOW_NW_FRAG_LATER ? OVS_FRAG_TYPE_LATER
914 : ovs_frag & FLOW_NW_FRAG_ANY ? OVS_FRAG_TYPE_FIRST
915 : OVS_FRAG_TYPE_NONE);
916 }
917
918 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'. */
919 void
920 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow)
921 {
922 struct ovs_key_ethernet *eth_key;
923 size_t encap;
924
925 if (flow->priority) {
926 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, flow->priority);
927 }
928
929 if (flow->tun_id != htonll(0)) {
930 nl_msg_put_be64(buf, OVS_KEY_ATTR_TUN_ID, flow->tun_id);
931 }
932
933 if (flow->in_port != OFPP_NONE) {
934 nl_msg_put_u32(buf, OVS_KEY_ATTR_IN_PORT,
935 ofp_port_to_odp_port(flow->in_port));
936 }
937
938 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
939 sizeof *eth_key);
940 memcpy(eth_key->eth_src, flow->dl_src, ETH_ADDR_LEN);
941 memcpy(eth_key->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
942
943 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
944 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
945 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, flow->vlan_tci);
946 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
947 if (flow->vlan_tci == htons(0)) {
948 goto unencap;
949 }
950 } else {
951 encap = 0;
952 }
953
954 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
955 goto unencap;
956 }
957
958 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, flow->dl_type);
959
960 if (flow->dl_type == htons(ETH_TYPE_IP)) {
961 struct ovs_key_ipv4 *ipv4_key;
962
963 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
964 sizeof *ipv4_key);
965 ipv4_key->ipv4_src = flow->nw_src;
966 ipv4_key->ipv4_dst = flow->nw_dst;
967 ipv4_key->ipv4_proto = flow->nw_proto;
968 ipv4_key->ipv4_tos = flow->nw_tos;
969 ipv4_key->ipv4_ttl = flow->nw_ttl;
970 ipv4_key->ipv4_frag = ovs_to_odp_frag(flow->nw_frag);
971 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
972 struct ovs_key_ipv6 *ipv6_key;
973
974 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
975 sizeof *ipv6_key);
976 memcpy(ipv6_key->ipv6_src, &flow->ipv6_src, sizeof ipv6_key->ipv6_src);
977 memcpy(ipv6_key->ipv6_dst, &flow->ipv6_dst, sizeof ipv6_key->ipv6_dst);
978 ipv6_key->ipv6_label = flow->ipv6_label;
979 ipv6_key->ipv6_proto = flow->nw_proto;
980 ipv6_key->ipv6_tclass = flow->nw_tos;
981 ipv6_key->ipv6_hlimit = flow->nw_ttl;
982 ipv6_key->ipv6_frag = ovs_to_odp_frag(flow->nw_frag);
983 } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
984 struct ovs_key_arp *arp_key;
985
986 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
987 sizeof *arp_key);
988 memset(arp_key, 0, sizeof *arp_key);
989 arp_key->arp_sip = flow->nw_src;
990 arp_key->arp_tip = flow->nw_dst;
991 arp_key->arp_op = htons(flow->nw_proto);
992 memcpy(arp_key->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
993 memcpy(arp_key->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
994 }
995
996 if ((flow->dl_type == htons(ETH_TYPE_IP)
997 || flow->dl_type == htons(ETH_TYPE_IPV6))
998 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
999
1000 if (flow->nw_proto == IPPROTO_TCP) {
1001 struct ovs_key_tcp *tcp_key;
1002
1003 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
1004 sizeof *tcp_key);
1005 tcp_key->tcp_src = flow->tp_src;
1006 tcp_key->tcp_dst = flow->tp_dst;
1007 } else if (flow->nw_proto == IPPROTO_UDP) {
1008 struct ovs_key_udp *udp_key;
1009
1010 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
1011 sizeof *udp_key);
1012 udp_key->udp_src = flow->tp_src;
1013 udp_key->udp_dst = flow->tp_dst;
1014 } else if (flow->dl_type == htons(ETH_TYPE_IP)
1015 && flow->nw_proto == IPPROTO_ICMP) {
1016 struct ovs_key_icmp *icmp_key;
1017
1018 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
1019 sizeof *icmp_key);
1020 icmp_key->icmp_type = ntohs(flow->tp_src);
1021 icmp_key->icmp_code = ntohs(flow->tp_dst);
1022 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
1023 && flow->nw_proto == IPPROTO_ICMPV6) {
1024 struct ovs_key_icmpv6 *icmpv6_key;
1025
1026 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
1027 sizeof *icmpv6_key);
1028 icmpv6_key->icmpv6_type = ntohs(flow->tp_src);
1029 icmpv6_key->icmpv6_code = ntohs(flow->tp_dst);
1030
1031 if (icmpv6_key->icmpv6_type == ND_NEIGHBOR_SOLICIT
1032 || icmpv6_key->icmpv6_type == ND_NEIGHBOR_ADVERT) {
1033 struct ovs_key_nd *nd_key;
1034
1035 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
1036 sizeof *nd_key);
1037 memcpy(nd_key->nd_target, &flow->nd_target,
1038 sizeof nd_key->nd_target);
1039 memcpy(nd_key->nd_sll, flow->arp_sha, ETH_ADDR_LEN);
1040 memcpy(nd_key->nd_tll, flow->arp_tha, ETH_ADDR_LEN);
1041 }
1042 }
1043 }
1044
1045 unencap:
1046 if (encap) {
1047 nl_msg_end_nested(buf, encap);
1048 }
1049 }
1050
1051 static void
1052 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
1053 uint32_t attrs,
1054 const struct nlattr *key, size_t key_len)
1055 {
1056 struct ds s;
1057 int i;
1058
1059 if (VLOG_DROP_WARN(rl)) {
1060 return;
1061 }
1062
1063 ds_init(&s);
1064 ds_put_format(&s, "%s:", title);
1065 for (i = 0; i < 32; i++) {
1066 if (attrs & (1u << i)) {
1067 ds_put_format(&s, " %s", ovs_key_attr_to_string(i));
1068 }
1069 }
1070
1071 ds_put_cstr(&s, ": ");
1072 odp_flow_key_format(key, key_len, &s);
1073
1074 VLOG_WARN("%s", ds_cstr(&s));
1075 ds_destroy(&s);
1076 }
1077
1078 static bool
1079 odp_to_ovs_frag(uint8_t odp_frag, struct flow *flow)
1080 {
1081 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1082
1083 if (odp_frag > OVS_FRAG_TYPE_LATER) {
1084 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key",
1085 odp_frag);
1086 return false;
1087 }
1088
1089 if (odp_frag != OVS_FRAG_TYPE_NONE) {
1090 flow->nw_frag |= FLOW_NW_FRAG_ANY;
1091 if (odp_frag == OVS_FRAG_TYPE_LATER) {
1092 flow->nw_frag |= FLOW_NW_FRAG_LATER;
1093 }
1094 }
1095 return true;
1096 }
1097
1098 static int
1099 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
1100 const struct nlattr *attrs[], uint64_t *present_attrsp)
1101 {
1102 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1103 const struct nlattr *nla;
1104 uint64_t present_attrs;
1105 size_t left;
1106
1107 present_attrs = 0;
1108 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
1109 uint16_t type = nl_attr_type(nla);
1110 size_t len = nl_attr_get_size(nla);
1111 int expected_len = odp_flow_key_attr_len(type);
1112
1113 if (len != expected_len && expected_len != -2) {
1114 if (expected_len == -1) {
1115 VLOG_ERR_RL(&rl, "unknown attribute %"PRIu16" in flow key",
1116 type);
1117 } else {
1118 VLOG_ERR_RL(&rl, "attribute %s has length %zu but should have "
1119 "length %d", ovs_key_attr_to_string(type),
1120 len, expected_len);
1121 }
1122 return EINVAL;
1123 } else if (present_attrs & (UINT64_C(1) << type)) {
1124 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
1125 ovs_key_attr_to_string(type));
1126 return EINVAL;
1127 }
1128
1129 present_attrs |= UINT64_C(1) << type;
1130 attrs[type] = nla;
1131 }
1132 if (left) {
1133 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
1134 return EINVAL;
1135 }
1136
1137 *present_attrsp = present_attrs;
1138 return 0;
1139 }
1140
1141 static int
1142 check_expectations(uint64_t present_attrs, uint64_t expected_attrs,
1143 const struct nlattr *key, size_t key_len)
1144 {
1145 uint64_t missing_attrs;
1146 uint64_t extra_attrs;
1147
1148 missing_attrs = expected_attrs & ~present_attrs;
1149 if (missing_attrs) {
1150 static struct vlog_rate_limit miss_rl = VLOG_RATE_LIMIT_INIT(10, 10);
1151 log_odp_key_attributes(&miss_rl, "expected but not present",
1152 missing_attrs, key, key_len);
1153 return EINVAL;
1154 }
1155
1156 extra_attrs = present_attrs & ~expected_attrs;
1157 if (extra_attrs) {
1158 static struct vlog_rate_limit extra_rl = VLOG_RATE_LIMIT_INIT(10, 10);
1159 log_odp_key_attributes(&extra_rl, "present but not expected",
1160 extra_attrs, key, key_len);
1161 return EINVAL;
1162 }
1163
1164 return 0;
1165 }
1166
1167 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
1168 * structure in 'flow'. Returns 0 if successful, otherwise EINVAL. */
1169 int
1170 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
1171 struct flow *flow)
1172 {
1173 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1174 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
1175 uint64_t expected_attrs;
1176 uint64_t present_attrs;
1177 int error;
1178
1179 memset(flow, 0, sizeof *flow);
1180
1181 error = parse_flow_nlattrs(key, key_len, attrs, &present_attrs);
1182 if (error) {
1183 return error;
1184 }
1185
1186 expected_attrs = 0;
1187
1188 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
1189 flow->priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
1190 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
1191 }
1192
1193 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUN_ID)) {
1194 flow->tun_id = nl_attr_get_be64(attrs[OVS_KEY_ATTR_TUN_ID]);
1195 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUN_ID;
1196 }
1197
1198 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
1199 uint32_t in_port = nl_attr_get_u32(attrs[OVS_KEY_ATTR_IN_PORT]);
1200 if (in_port >= UINT16_MAX || in_port >= OFPP_MAX) {
1201 VLOG_ERR_RL(&rl, "in_port %"PRIu32" out of supported range",
1202 in_port);
1203 return EINVAL;
1204 }
1205 flow->in_port = odp_port_to_ofp_port(in_port);
1206 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
1207 } else {
1208 flow->in_port = OFPP_NONE;
1209 }
1210
1211 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
1212 const struct ovs_key_ethernet *eth_key;
1213
1214 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
1215 memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN);
1216 memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN);
1217 } else {
1218 VLOG_ERR_RL(&rl, "missing Ethernet attribute in flow key");
1219 return EINVAL;
1220 }
1221 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
1222
1223 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)
1224 && (nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE])
1225 == htons(ETH_TYPE_VLAN))) {
1226 /* The Ethernet type is 0x8100 so there must be a VLAN tag
1227 * and encapsulated protocol information. */
1228 const struct nlattr *encap;
1229 __be16 tci;
1230 int error;
1231
1232 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE) |
1233 (UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
1234 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
1235 error = check_expectations(present_attrs, expected_attrs,
1236 key, key_len);
1237 if (error) {
1238 return error;
1239 }
1240
1241 encap = attrs[OVS_KEY_ATTR_ENCAP];
1242 tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
1243 if (tci & htons(VLAN_CFI)) {
1244 flow->vlan_tci = tci;
1245
1246 error = parse_flow_nlattrs(nl_attr_get(encap),
1247 nl_attr_get_size(encap),
1248 attrs, &present_attrs);
1249 if (error) {
1250 return error;
1251 }
1252 expected_attrs = 0;
1253 } else if (tci == htons(0)) {
1254 /* Corner case for a truncated 802.1Q header. */
1255 if (nl_attr_get_size(encap)) {
1256 return EINVAL;
1257 }
1258
1259 flow->dl_type = htons(ETH_TYPE_VLAN);
1260 return 0;
1261 } else {
1262 return EINVAL;
1263 }
1264 }
1265
1266 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
1267 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
1268 if (ntohs(flow->dl_type) < 1536) {
1269 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
1270 ntohs(flow->dl_type));
1271 return EINVAL;
1272 }
1273 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
1274 } else {
1275 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
1276 }
1277
1278 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1279 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
1280 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
1281 const struct ovs_key_ipv4 *ipv4_key;
1282
1283 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
1284 flow->nw_src = ipv4_key->ipv4_src;
1285 flow->nw_dst = ipv4_key->ipv4_dst;
1286 flow->nw_proto = ipv4_key->ipv4_proto;
1287 flow->nw_tos = ipv4_key->ipv4_tos;
1288 flow->nw_ttl = ipv4_key->ipv4_ttl;
1289 if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) {
1290 return EINVAL;
1291 }
1292 }
1293 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1294 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
1295 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
1296 const struct ovs_key_ipv6 *ipv6_key;
1297
1298 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
1299 memcpy(&flow->ipv6_src, ipv6_key->ipv6_src, sizeof flow->ipv6_src);
1300 memcpy(&flow->ipv6_dst, ipv6_key->ipv6_dst, sizeof flow->ipv6_dst);
1301 flow->ipv6_label = ipv6_key->ipv6_label;
1302 flow->nw_proto = ipv6_key->ipv6_proto;
1303 flow->nw_tos = ipv6_key->ipv6_tclass;
1304 flow->nw_ttl = ipv6_key->ipv6_hlimit;
1305 if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) {
1306 return EINVAL;
1307 }
1308 }
1309 } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
1310 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
1311 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
1312 const struct ovs_key_arp *arp_key;
1313
1314 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
1315 flow->nw_src = arp_key->arp_sip;
1316 flow->nw_dst = arp_key->arp_tip;
1317 if (arp_key->arp_op & htons(0xff00)) {
1318 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
1319 "key", ntohs(arp_key->arp_op));
1320 return EINVAL;
1321 }
1322 flow->nw_proto = ntohs(arp_key->arp_op);
1323 memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN);
1324 memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN);
1325 }
1326 }
1327
1328 if (flow->nw_proto == IPPROTO_TCP
1329 && (flow->dl_type == htons(ETH_TYPE_IP) ||
1330 flow->dl_type == htons(ETH_TYPE_IPV6))
1331 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1332 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
1333 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
1334 const struct ovs_key_tcp *tcp_key;
1335
1336 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
1337 flow->tp_src = tcp_key->tcp_src;
1338 flow->tp_dst = tcp_key->tcp_dst;
1339 }
1340 } else if (flow->nw_proto == IPPROTO_UDP
1341 && (flow->dl_type == htons(ETH_TYPE_IP) ||
1342 flow->dl_type == htons(ETH_TYPE_IPV6))
1343 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1344 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
1345 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
1346 const struct ovs_key_udp *udp_key;
1347
1348 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
1349 flow->tp_src = udp_key->udp_src;
1350 flow->tp_dst = udp_key->udp_dst;
1351 }
1352 } else if (flow->nw_proto == IPPROTO_ICMP
1353 && flow->dl_type == htons(ETH_TYPE_IP)
1354 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1355 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
1356 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
1357 const struct ovs_key_icmp *icmp_key;
1358
1359 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
1360 flow->tp_src = htons(icmp_key->icmp_type);
1361 flow->tp_dst = htons(icmp_key->icmp_code);
1362 }
1363 } else if (flow->nw_proto == IPPROTO_ICMPV6
1364 && flow->dl_type == htons(ETH_TYPE_IPV6)
1365 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1366 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
1367 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
1368 const struct ovs_key_icmpv6 *icmpv6_key;
1369
1370 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
1371 flow->tp_src = htons(icmpv6_key->icmpv6_type);
1372 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
1373
1374 if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
1375 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
1376 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
1377 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
1378 const struct ovs_key_nd *nd_key;
1379
1380 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
1381 memcpy(&flow->nd_target, nd_key->nd_target,
1382 sizeof flow->nd_target);
1383 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
1384 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
1385 }
1386 }
1387 }
1388 }
1389
1390 return check_expectations(present_attrs, expected_attrs, key, key_len);
1391 }