]> git.proxmox.com Git - ovs.git/blame - lib/odp-util.c
socket-util: Remove get_socket_error().
[ovs.git] / lib / odp-util.c
CommitLineData
064af421 1/*
e0edde6f 2 * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
064af421 3 *
a14bc59f
BP
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:
064af421 7 *
a14bc59f
BP
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.
064af421
BP
15 */
16
17#include <config.h>
bed69b3e 18#include <arpa/inet.h>
064af421 19#include "odp-util.h"
36956a7d 20#include <errno.h>
064af421 21#include <inttypes.h>
7202cbe5 22#include <math.h>
6ca00f6f 23#include <netinet/in.h>
685a51a5 24#include <netinet/icmp6.h>
064af421
BP
25#include <stdlib.h>
26#include <string.h>
cdee00fd 27#include "byte-order.h"
064af421
BP
28#include "coverage.h"
29#include "dynamic-string.h"
30#include "flow.h"
cdee00fd 31#include "netlink.h"
3bffc610 32#include "ofpbuf.h"
064af421 33#include "packets.h"
44bac24b 34#include "simap.h"
064af421
BP
35#include "timeval.h"
36#include "util.h"
34118cae
BP
37#include "vlog.h"
38
39VLOG_DEFINE_THIS_MODULE(odp_util);
064af421 40
df2c07f4
JP
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.
45 */
46
7202cbe5
BP
47/* The set of characters that may separate one action or one key attribute
48 * from another. */
49static const char *delimiters = ", \t\r\n";
50
44bac24b 51static int parse_odp_key_attr(const char *, const struct simap *port_names,
7202cbe5 52 struct ofpbuf *);
a052b51b
BP
53static void format_odp_key_attr(const struct nlattr *a, struct ds *ds);
54
98403001
BP
55/* Returns one the following for the action with the given OVS_ACTION_ATTR_*
56 * 'type':
57 *
58 * - For an action whose argument has a fixed length, returned that
59 * nonnegative length in bytes.
60 *
61 * - For an action with a variable-length argument, returns -2.
62 *
63 * - For an invalid 'type', returns -1. */
4edb9ae9 64static int
cdee00fd
BP
65odp_action_len(uint16_t type)
66{
df2c07f4 67 if (type > OVS_ACTION_ATTR_MAX) {
cdee00fd
BP
68 return -1;
69 }
70
09ded0ad 71 switch ((enum ovs_action_attr) type) {
fea393b1 72 case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
98403001 73 case OVS_ACTION_ATTR_USERSPACE: return -2;
fea393b1
BP
74 case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
75 case OVS_ACTION_ATTR_POP_VLAN: return 0;
4edb9ae9 76 case OVS_ACTION_ATTR_SET: return -2;
98403001 77 case OVS_ACTION_ATTR_SAMPLE: return -2;
df2c07f4
JP
78
79 case OVS_ACTION_ATTR_UNSPEC:
80 case __OVS_ACTION_ATTR_MAX:
cdee00fd
BP
81 return -1;
82 }
83
84 return -1;
85}
86
744791b5
BP
87static const char *
88ovs_key_attr_to_string(enum ovs_key_attr attr)
89{
90 static char unknown_attr[3 + INT_STRLEN(unsigned int) + 1];
91
92 switch (attr) {
93 case OVS_KEY_ATTR_UNSPEC: return "unspec";
fea393b1 94 case OVS_KEY_ATTR_ENCAP: return "encap";
744791b5 95 case OVS_KEY_ATTR_PRIORITY: return "priority";
356af50b
KM
96 case OVS_KEY_ATTR_TUN_ID: return "tun_id";
97 case OVS_KEY_ATTR_IPV4_TUNNEL: return "ipv4_tunnel";
744791b5
BP
98 case OVS_KEY_ATTR_IN_PORT: return "in_port";
99 case OVS_KEY_ATTR_ETHERNET: return "eth";
fea393b1 100 case OVS_KEY_ATTR_VLAN: return "vlan";
744791b5
BP
101 case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
102 case OVS_KEY_ATTR_IPV4: return "ipv4";
103 case OVS_KEY_ATTR_IPV6: return "ipv6";
104 case OVS_KEY_ATTR_TCP: return "tcp";
105 case OVS_KEY_ATTR_UDP: return "udp";
106 case OVS_KEY_ATTR_ICMP: return "icmp";
107 case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
108 case OVS_KEY_ATTR_ARP: return "arp";
109 case OVS_KEY_ATTR_ND: return "nd";
110
111 case __OVS_KEY_ATTR_MAX:
112 default:
113 snprintf(unknown_attr, sizeof unknown_attr, "key%u",
114 (unsigned int) attr);
115 return unknown_attr;
116 }
117}
118
cdee00fd
BP
119static void
120format_generic_odp_action(struct ds *ds, const struct nlattr *a)
121{
8ba43fbd
BP
122 size_t len = nl_attr_get_size(a);
123
cdee00fd 124 ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
8ba43fbd 125 if (len) {
cdee00fd
BP
126 const uint8_t *unspec;
127 unsigned int i;
128
129 unspec = nl_attr_get(a);
8ba43fbd 130 for (i = 0; i < len; i++) {
cdee00fd
BP
131 ds_put_char(ds, i ? ' ': '(');
132 ds_put_format(ds, "%02x", unspec[i]);
133 }
134 ds_put_char(ds, ')');
135 }
136}
137
6ff686f2
PS
138static void
139format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
140{
141 static const struct nl_policy ovs_sample_policy[] = {
142 [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 },
143 [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED }
144 };
145 struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
6ff686f2
PS
146 double percentage;
147 const struct nlattr *nla_acts;
148 int len;
149
150 ds_put_cstr(ds, "sample");
151
5621afff 152 if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
6ff686f2
PS
153 ds_put_cstr(ds, "(error)");
154 return;
155 }
156
157 percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
158 UINT32_MAX;
159
160 ds_put_format(ds, "(sample=%.1f%%,", percentage);
161
162 ds_put_cstr(ds, "actions(");
163 nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
164 len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
165 format_odp_actions(ds, nla_acts, len);
166 ds_put_format(ds, "))");
167}
168
6a7e895f 169static const char *
ec956fc7 170slow_path_reason_to_string(uint32_t data)
6a7e895f 171{
ec956fc7
PS
172 enum slow_path_reason bit = (enum slow_path_reason) data;
173
6a7e895f
BP
174 switch (bit) {
175 case SLOW_CFM:
176 return "cfm";
177 case SLOW_LACP:
178 return "lacp";
179 case SLOW_STP:
180 return "stp";
181 case SLOW_IN_BAND:
182 return "in_band";
183 case SLOW_CONTROLLER:
184 return "controller";
185 case SLOW_MATCH:
186 return "match";
187 default:
188 return NULL;
189 }
190}
191
192static void
ec956fc7
PS
193format_flags(struct ds *ds, const char *(*bit_to_string)(uint32_t),
194 uint32_t flags)
6a7e895f
BP
195{
196 uint32_t bad = 0;
197
ec956fc7
PS
198 ds_put_format(ds, "(");
199 if (!flags) {
200 goto out;
201 }
202 while (flags) {
203 uint32_t bit = rightmost_1bit(flags);
6a7e895f
BP
204 const char *s;
205
ec956fc7 206 s = bit_to_string(bit);
6a7e895f
BP
207 if (s) {
208 ds_put_format(ds, "%s,", s);
209 } else {
210 bad |= bit;
211 }
212
ec956fc7 213 flags &= ~bit;
6a7e895f
BP
214 }
215
216 if (bad) {
217 ds_put_format(ds, "0x%"PRIx32",", bad);
218 }
219 ds_chomp(ds, ',');
ec956fc7
PS
220out:
221 ds_put_format(ds, ")");
222}
223
224static int
225parse_flags(const char *s, const char *(*bit_to_string)(uint32_t),
226 uint32_t *res)
227{
228 uint32_t result = 0;
229 int n = 0;
230
231 if (s[n] != '(') {
232 return -EINVAL;
233 }
234 n++;
235
236 while (s[n] != ')') {
237 unsigned long long int flags;
238 uint32_t bit;
239 int n0;
240
241 if (sscanf(&s[n], "%lli%n", &flags, &n0) > 0 && n0 > 0) {
242 n += n0 + (s[n + n0] == ',');
243 result |= flags;
244 continue;
245 }
246
247 for (bit = 1; bit; bit <<= 1) {
248 const char *name = bit_to_string(bit);
249 size_t len;
250
251 if (!name) {
252 continue;
253 }
254
255 len = strlen(name);
256 if (!strncmp(s + n, name, len) &&
257 (s[n + len] == ',' || s[n + len] == ')')) {
258 result |= bit;
259 n += len + (s[n + len] == ',');
260 break;
261 }
262 }
263
264 if (!bit) {
265 return -EINVAL;
266 }
267 }
268 n++;
269
270 *res = result;
271 return n;
6a7e895f
BP
272}
273
98403001
BP
274static void
275format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
276{
277 static const struct nl_policy ovs_userspace_policy[] = {
278 [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 },
279 [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
280 };
281 struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
282
283 if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
284 ds_put_cstr(ds, "userspace(error)");
285 return;
286 }
287
288 ds_put_format(ds, "userspace(pid=%"PRIu32,
289 nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
290
291 if (a[OVS_USERSPACE_ATTR_USERDATA]) {
292 uint64_t userdata = nl_attr_get_u64(a[OVS_USERSPACE_ATTR_USERDATA]);
1673e0e4 293 union user_action_cookie cookie;
98403001
BP
294
295 memcpy(&cookie, &userdata, sizeof cookie);
296
21ae82ff
BP
297 switch (cookie.type) {
298 case USER_ACTION_COOKIE_SFLOW:
6a7e895f
BP
299 ds_put_format(ds, ",sFlow("
300 "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")",
1673e0e4
BP
301 vlan_tci_to_vid(cookie.sflow.vlan_tci),
302 vlan_tci_to_pcp(cookie.sflow.vlan_tci),
303 cookie.sflow.output);
21ae82ff
BP
304 break;
305
6a7e895f 306 case USER_ACTION_COOKIE_SLOW_PATH:
ec956fc7
PS
307 ds_put_cstr(ds, ",slow_path");
308 format_flags(ds, slow_path_reason_to_string, cookie.slow_path.reason);
6a7e895f
BP
309 break;
310
21ae82ff
BP
311 case USER_ACTION_COOKIE_UNSPEC:
312 default:
98403001 313 ds_put_format(ds, ",userdata=0x%"PRIx64, userdata);
36fc5f18 314 break;
98403001
BP
315 }
316 }
317
318 ds_put_char(ds, ')');
319}
320
8ddc056d
BP
321static void
322format_vlan_tci(struct ds *ds, ovs_be16 vlan_tci)
323{
324 ds_put_format(ds, "vid=%"PRIu16",pcp=%d",
325 vlan_tci_to_vid(vlan_tci),
326 vlan_tci_to_pcp(vlan_tci));
327 if (!(vlan_tci & htons(VLAN_CFI))) {
328 ds_put_cstr(ds, ",cfi=0");
329 }
330}
331
4edb9ae9 332static void
cdee00fd 333format_odp_action(struct ds *ds, const struct nlattr *a)
064af421 334{
98403001 335 int expected_len;
4edb9ae9 336 enum ovs_action_attr type = nl_attr_type(a);
fea393b1 337 const struct ovs_action_push_vlan *vlan;
cdee00fd 338
98403001
BP
339 expected_len = odp_action_len(nl_attr_type(a));
340 if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
8ba43fbd 341 ds_put_format(ds, "bad length %zu, expected %d for: ",
98403001 342 nl_attr_get_size(a), expected_len);
cdee00fd
BP
343 format_generic_odp_action(ds, a);
344 return;
345 }
346
4edb9ae9 347 switch (type) {
df2c07f4 348 case OVS_ACTION_ATTR_OUTPUT:
cdee00fd 349 ds_put_format(ds, "%"PRIu16, nl_attr_get_u32(a));
064af421 350 break;
df2c07f4 351 case OVS_ACTION_ATTR_USERSPACE:
98403001 352 format_odp_userspace_action(ds, a);
064af421 353 break;
4edb9ae9
PS
354 case OVS_ACTION_ATTR_SET:
355 ds_put_cstr(ds, "set(");
356 format_odp_key_attr(nl_attr_get(a), ds);
357 ds_put_cstr(ds, ")");
064af421 358 break;
fea393b1
BP
359 case OVS_ACTION_ATTR_PUSH_VLAN:
360 vlan = nl_attr_get(a);
361 ds_put_cstr(ds, "push_vlan(");
362 if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
363 ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
364 }
8ddc056d
BP
365 format_vlan_tci(ds, vlan->vlan_tci);
366 ds_put_char(ds, ')');
064af421 367 break;
fea393b1
BP
368 case OVS_ACTION_ATTR_POP_VLAN:
369 ds_put_cstr(ds, "pop_vlan");
064af421 370 break;
6ff686f2
PS
371 case OVS_ACTION_ATTR_SAMPLE:
372 format_odp_sample_action(ds, a);
373 break;
4edb9ae9
PS
374 case OVS_ACTION_ATTR_UNSPEC:
375 case __OVS_ACTION_ATTR_MAX:
064af421 376 default:
cdee00fd 377 format_generic_odp_action(ds, a);
064af421
BP
378 break;
379 }
380}
381
382void
cdee00fd 383format_odp_actions(struct ds *ds, const struct nlattr *actions,
cf22f8cb 384 size_t actions_len)
064af421 385{
cdee00fd
BP
386 if (actions_len) {
387 const struct nlattr *a;
388 unsigned int left;
389
390 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
391 if (a != actions) {
392 ds_put_char(ds, ',');
393 }
394 format_odp_action(ds, a);
064af421 395 }
cdee00fd 396 if (left) {
3a8cfc50
BP
397 int i;
398
3476fce3
BP
399 if (left == actions_len) {
400 ds_put_cstr(ds, "<empty>");
401 }
3a8cfc50
BP
402 ds_put_format(ds, ",***%u leftover bytes*** (", left);
403 for (i = 0; i < left; i++) {
404 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
405 }
406 ds_put_char(ds, ')');
cdee00fd
BP
407 }
408 } else {
064af421
BP
409 ds_put_cstr(ds, "drop");
410 }
411}
7202cbe5
BP
412
413static int
44bac24b 414parse_odp_action(const char *s, const struct simap *port_names,
7202cbe5
BP
415 struct ofpbuf *actions)
416{
417 /* Many of the sscanf calls in this function use oversized destination
418 * fields because some sscanf() implementations truncate the range of %i
419 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
420 * value of 0x7fff. The other alternatives are to allow only a single
421 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
422 * parsers.
423 *
424 * The tun_id parser has to use an alternative approach because there is no
425 * type larger than 64 bits. */
426
427 {
428 unsigned long long int port;
429 int n = -1;
430
431 if (sscanf(s, "%lli%n", &port, &n) > 0 && n > 0) {
432 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
433 return n;
434 }
435 }
436
437 if (port_names) {
438 int len = strcspn(s, delimiters);
44bac24b 439 struct simap_node *node;
7202cbe5 440
44bac24b 441 node = simap_find_len(port_names, s, len);
7202cbe5 442 if (node) {
44bac24b 443 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
7202cbe5
BP
444 return len;
445 }
446 }
447
448 {
449 unsigned long long int pid;
36fc5f18 450 unsigned long long int output;
7202cbe5 451 char userdata_s[32];
7202cbe5
BP
452 int vid, pcp;
453 int n = -1;
454
455 if (sscanf(s, "userspace(pid=%lli)%n", &pid, &n) > 0 && n > 0) {
456 odp_put_userspace_action(pid, NULL, actions);
457 return n;
6a7e895f
BP
458 } else if (sscanf(s, "userspace(pid=%lli,sFlow(vid=%i,"
459 "pcp=%i,output=%lli))%n",
36fc5f18 460 &pid, &vid, &pcp, &output, &n) > 0 && n > 0) {
1673e0e4 461 union user_action_cookie cookie;
7202cbe5
BP
462 uint16_t tci;
463
464 tci = vid | (pcp << VLAN_PCP_SHIFT);
465 if (tci) {
466 tci |= VLAN_CFI;
467 }
468
469 cookie.type = USER_ACTION_COOKIE_SFLOW;
1673e0e4
BP
470 cookie.sflow.vlan_tci = htons(tci);
471 cookie.sflow.output = output;
6a7e895f
BP
472 odp_put_userspace_action(pid, &cookie, actions);
473 return n;
ec956fc7 474 } else if (sscanf(s, "userspace(pid=%lli,slow_path%n", &pid, &n) > 0
6a7e895f
BP
475 && n > 0) {
476 union user_action_cookie cookie;
ec956fc7 477 int res;
6a7e895f
BP
478
479 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
480 cookie.slow_path.unused = 0;
481 cookie.slow_path.reason = 0;
482
ec956fc7
PS
483 res = parse_flags(&s[n], slow_path_reason_to_string,
484 &cookie.slow_path.reason);
485 if (res < 0) {
486 return res;
6a7e895f 487 }
ec956fc7
PS
488 n += res;
489 if (s[n] != ')') {
6a7e895f
BP
490 return -EINVAL;
491 }
ec956fc7 492 n++;
6a7e895f 493
7202cbe5
BP
494 odp_put_userspace_action(pid, &cookie, actions);
495 return n;
496 } else if (sscanf(s, "userspace(pid=%lli,userdata="
497 "%31[x0123456789abcdefABCDEF])%n", &pid, userdata_s,
498 &n) > 0 && n > 0) {
1673e0e4 499 union user_action_cookie cookie;
7202cbe5
BP
500 uint64_t userdata;
501
502 userdata = strtoull(userdata_s, NULL, 0);
503 memcpy(&cookie, &userdata, sizeof cookie);
504 odp_put_userspace_action(pid, &cookie, actions);
505 return n;
506 }
507 }
508
509 if (!strncmp(s, "set(", 4)) {
510 size_t start_ofs;
511 int retval;
512
513 start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
514 retval = parse_odp_key_attr(s + 4, port_names, actions);
515 if (retval < 0) {
516 return retval;
517 }
518 if (s[retval + 4] != ')') {
519 return -EINVAL;
520 }
521 nl_msg_end_nested(actions, start_ofs);
522 return retval + 5;
523 }
524
becffb86
BP
525 {
526 struct ovs_action_push_vlan push;
527 int tpid = ETH_TYPE_VLAN;
528 int vid, pcp;
529 int cfi = 1;
530 int n = -1;
7202cbe5 531
becffb86
BP
532 if ((sscanf(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n) > 0
533 && n > 0)
534 || (sscanf(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
535 &vid, &pcp, &cfi, &n) > 0 && n > 0)
536 || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
537 &tpid, &vid, &pcp, &n) > 0 && n > 0)
538 || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
539 &tpid, &vid, &pcp, &cfi, &n) > 0 && n > 0)) {
540 push.vlan_tpid = htons(tpid);
541 push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
542 | (pcp << VLAN_PCP_SHIFT)
543 | (cfi ? VLAN_CFI : 0));
544 nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
545 &push, sizeof push);
546
547 return n;
7202cbe5 548 }
7202cbe5
BP
549 }
550
becffb86
BP
551 if (!strncmp(s, "pop_vlan", 8)) {
552 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
553 return 8;
7202cbe5
BP
554 }
555
556 {
557 double percentage;
558 int n = -1;
559
560 if (sscanf(s, "sample(sample=%lf%%,actions(%n", &percentage, &n) > 0
561 && percentage >= 0. && percentage <= 100.0
562 && n > 0) {
563 size_t sample_ofs, actions_ofs;
564 double probability;
565
566 probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
567 sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
568 nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
569 (probability <= 0 ? 0
570 : probability >= UINT32_MAX ? UINT32_MAX
571 : probability));
572
573 actions_ofs = nl_msg_start_nested(actions,
574 OVS_SAMPLE_ATTR_ACTIONS);
575 for (;;) {
576 int retval;
577
79d4ffe2 578 n += strspn(s + n, delimiters);
7202cbe5
BP
579 if (s[n] == ')') {
580 break;
581 }
582
583 retval = parse_odp_action(s + n, port_names, actions);
584 if (retval < 0) {
585 return retval;
586 }
587 n += retval;
7202cbe5
BP
588 }
589 nl_msg_end_nested(actions, actions_ofs);
590 nl_msg_end_nested(actions, sample_ofs);
591
592 return s[n + 1] == ')' ? n + 2 : -EINVAL;
593 }
594 }
595
596 return -EINVAL;
597}
598
599/* Parses the string representation of datapath actions, in the format output
600 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
601 * value. On success, the ODP actions are appended to 'actions' as a series of
602 * Netlink attributes. On failure, no data is appended to 'actions'. Either
603 * way, 'actions''s data might be reallocated. */
604int
44bac24b 605odp_actions_from_string(const char *s, const struct simap *port_names,
7202cbe5
BP
606 struct ofpbuf *actions)
607{
608 size_t old_size;
609
610 if (!strcasecmp(s, "drop")) {
611 return 0;
612 }
613
614 old_size = actions->size;
615 for (;;) {
616 int retval;
617
618 s += strspn(s, delimiters);
619 if (!*s) {
620 return 0;
621 }
622
623 retval = parse_odp_action(s, port_names, actions);
624 if (retval < 0 || !strchr(delimiters, s[retval])) {
625 actions->size = old_size;
626 return -retval;
627 }
628 s += retval;
629 }
630
631 return 0;
632}
14608a15 633\f
36956a7d 634/* Returns the correct length of the payload for a flow key attribute of the
fea393b1
BP
635 * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
636 * is variable length. */
36956a7d
BP
637static int
638odp_flow_key_attr_len(uint16_t type)
639{
df2c07f4 640 if (type > OVS_KEY_ATTR_MAX) {
36956a7d
BP
641 return -1;
642 }
643
09ded0ad 644 switch ((enum ovs_key_attr) type) {
fea393b1 645 case OVS_KEY_ATTR_ENCAP: return -2;
abff858b 646 case OVS_KEY_ATTR_PRIORITY: return 4;
df2c07f4 647 case OVS_KEY_ATTR_TUN_ID: return 8;
356af50b 648 case OVS_KEY_ATTR_IPV4_TUNNEL: return sizeof(struct ovs_key_ipv4_tunnel);
df2c07f4
JP
649 case OVS_KEY_ATTR_IN_PORT: return 4;
650 case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet);
fea393b1 651 case OVS_KEY_ATTR_VLAN: return sizeof(ovs_be16);
df2c07f4
JP
652 case OVS_KEY_ATTR_ETHERTYPE: return 2;
653 case OVS_KEY_ATTR_IPV4: return sizeof(struct ovs_key_ipv4);
654 case OVS_KEY_ATTR_IPV6: return sizeof(struct ovs_key_ipv6);
655 case OVS_KEY_ATTR_TCP: return sizeof(struct ovs_key_tcp);
656 case OVS_KEY_ATTR_UDP: return sizeof(struct ovs_key_udp);
657 case OVS_KEY_ATTR_ICMP: return sizeof(struct ovs_key_icmp);
658 case OVS_KEY_ATTR_ICMPV6: return sizeof(struct ovs_key_icmpv6);
659 case OVS_KEY_ATTR_ARP: return sizeof(struct ovs_key_arp);
660 case OVS_KEY_ATTR_ND: return sizeof(struct ovs_key_nd);
661
662 case OVS_KEY_ATTR_UNSPEC:
663 case __OVS_KEY_ATTR_MAX:
36956a7d
BP
664 return -1;
665 }
666
667 return -1;
668}
669
36956a7d
BP
670static void
671format_generic_odp_key(const struct nlattr *a, struct ds *ds)
672{
673 size_t len = nl_attr_get_size(a);
36956a7d
BP
674 if (len) {
675 const uint8_t *unspec;
676 unsigned int i;
677
678 unspec = nl_attr_get(a);
679 for (i = 0; i < len; i++) {
680 ds_put_char(ds, i ? ' ': '(');
681 ds_put_format(ds, "%02x", unspec[i]);
682 }
683 ds_put_char(ds, ')');
684 }
685}
686
7257b535
BP
687static const char *
688ovs_frag_type_to_string(enum ovs_frag_type type)
689{
690 switch (type) {
691 case OVS_FRAG_TYPE_NONE:
692 return "no";
693 case OVS_FRAG_TYPE_FIRST:
694 return "first";
695 case OVS_FRAG_TYPE_LATER:
696 return "later";
697 case __OVS_FRAG_TYPE_MAX:
698 default:
699 return "<error>";
700 }
701}
702
29c7a2b2
PS
703static const char *
704tun_flag_to_string(uint32_t flags)
705{
706 switch (flags) {
707 case OVS_TNL_F_DONT_FRAGMENT:
708 return "df";
709 case OVS_TNL_F_CSUM:
710 return "csum";
711 case OVS_TNL_F_KEY:
712 return "key";
713 default:
714 return NULL;
715 }
716}
717
36956a7d
BP
718static void
719format_odp_key_attr(const struct nlattr *a, struct ds *ds)
720{
df2c07f4 721 const struct ovs_key_ethernet *eth_key;
df2c07f4
JP
722 const struct ovs_key_ipv4 *ipv4_key;
723 const struct ovs_key_ipv6 *ipv6_key;
724 const struct ovs_key_tcp *tcp_key;
725 const struct ovs_key_udp *udp_key;
726 const struct ovs_key_icmp *icmp_key;
727 const struct ovs_key_icmpv6 *icmpv6_key;
728 const struct ovs_key_arp *arp_key;
729 const struct ovs_key_nd *nd_key;
356af50b 730 const struct ovs_key_ipv4_tunnel *ipv4_tun_key;
7a8e9ed2 731 enum ovs_key_attr attr = nl_attr_type(a);
fea393b1 732 int expected_len;
36956a7d 733
b3b5bf02 734 ds_put_cstr(ds, ovs_key_attr_to_string(attr));
fea393b1
BP
735 expected_len = odp_flow_key_attr_len(nl_attr_type(a));
736 if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
b3b5bf02 737 ds_put_format(ds, "(bad length %zu, expected %d)",
36956a7d
BP
738 nl_attr_get_size(a),
739 odp_flow_key_attr_len(nl_attr_type(a)));
740 format_generic_odp_key(a, ds);
741 return;
742 }
743
7a8e9ed2 744 switch (attr) {
fea393b1
BP
745 case OVS_KEY_ATTR_ENCAP:
746 ds_put_cstr(ds, "(");
747 if (nl_attr_get_size(a)) {
748 odp_flow_key_format(nl_attr_get(a), nl_attr_get_size(a), ds);
749 }
750 ds_put_char(ds, ')');
751 break;
752
abff858b 753 case OVS_KEY_ATTR_PRIORITY:
b3b5bf02 754 ds_put_format(ds, "(%"PRIu32")", nl_attr_get_u32(a));
abff858b
PS
755 break;
756
df2c07f4 757 case OVS_KEY_ATTR_TUN_ID:
b3b5bf02 758 ds_put_format(ds, "(%#"PRIx64")", ntohll(nl_attr_get_be64(a)));
36956a7d
BP
759 break;
760
356af50b
KM
761 case OVS_KEY_ATTR_IPV4_TUNNEL:
762 ipv4_tun_key = nl_attr_get(a);
29c7a2b2
PS
763 ds_put_format(ds, "(tun_id=0x%"PRIx64",src="IP_FMT",dst="IP_FMT","
764 "tos=0x%"PRIx8",ttl=%"PRIu8",flags",
765 ntohll(ipv4_tun_key->tun_id),
356af50b
KM
766 IP_ARGS(&ipv4_tun_key->ipv4_src),
767 IP_ARGS(&ipv4_tun_key->ipv4_dst),
768 ipv4_tun_key->ipv4_tos, ipv4_tun_key->ipv4_ttl);
29c7a2b2
PS
769
770 format_flags(ds, tun_flag_to_string, ipv4_tun_key->tun_flags);
771 ds_put_format(ds, ")");
356af50b
KM
772 break;
773
df2c07f4 774 case OVS_KEY_ATTR_IN_PORT:
b3b5bf02 775 ds_put_format(ds, "(%"PRIu32")", nl_attr_get_u32(a));
36956a7d
BP
776 break;
777
df2c07f4 778 case OVS_KEY_ATTR_ETHERNET:
36956a7d 779 eth_key = nl_attr_get(a);
b3b5bf02 780 ds_put_format(ds, "(src="ETH_ADDR_FMT",dst="ETH_ADDR_FMT")",
36956a7d
BP
781 ETH_ADDR_ARGS(eth_key->eth_src),
782 ETH_ADDR_ARGS(eth_key->eth_dst));
783 break;
784
fea393b1 785 case OVS_KEY_ATTR_VLAN:
8ddc056d
BP
786 ds_put_char(ds, '(');
787 format_vlan_tci(ds, nl_attr_get_be16(a));
788 ds_put_char(ds, ')');
36956a7d
BP
789 break;
790
df2c07f4 791 case OVS_KEY_ATTR_ETHERTYPE:
b3b5bf02 792 ds_put_format(ds, "(0x%04"PRIx16")",
36956a7d
BP
793 ntohs(nl_attr_get_be16(a)));
794 break;
795
df2c07f4 796 case OVS_KEY_ATTR_IPV4:
36956a7d 797 ipv4_key = nl_attr_get(a);
b3b5bf02 798 ds_put_format(ds, "(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
a61680c6 799 ",tos=%#"PRIx8",ttl=%"PRIu8",frag=%s)",
36956a7d
BP
800 IP_ARGS(&ipv4_key->ipv4_src),
801 IP_ARGS(&ipv4_key->ipv4_dst),
7257b535 802 ipv4_key->ipv4_proto, ipv4_key->ipv4_tos,
a61680c6 803 ipv4_key->ipv4_ttl,
7257b535 804 ovs_frag_type_to_string(ipv4_key->ipv4_frag));
36956a7d
BP
805 break;
806
df2c07f4 807 case OVS_KEY_ATTR_IPV6: {
d31f1109
JP
808 char src_str[INET6_ADDRSTRLEN];
809 char dst_str[INET6_ADDRSTRLEN];
810
811 ipv6_key = nl_attr_get(a);
812 inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
813 inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
814
b3b5bf02 815 ds_put_format(ds, "(src=%s,dst=%s,label=%#"PRIx32",proto=%"PRIu8
60258dcb 816 ",tclass=%#"PRIx8",hlimit=%"PRIu8",frag=%s)",
fa8223b7 817 src_str, dst_str, ntohl(ipv6_key->ipv6_label),
60258dcb 818 ipv6_key->ipv6_proto, ipv6_key->ipv6_tclass,
a61680c6 819 ipv6_key->ipv6_hlimit,
7257b535 820 ovs_frag_type_to_string(ipv6_key->ipv6_frag));
d31f1109
JP
821 break;
822 }
823
df2c07f4 824 case OVS_KEY_ATTR_TCP:
36956a7d 825 tcp_key = nl_attr_get(a);
b3b5bf02 826 ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")",
36956a7d
BP
827 ntohs(tcp_key->tcp_src), ntohs(tcp_key->tcp_dst));
828 break;
829
df2c07f4 830 case OVS_KEY_ATTR_UDP:
36956a7d 831 udp_key = nl_attr_get(a);
b3b5bf02 832 ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")",
36956a7d
BP
833 ntohs(udp_key->udp_src), ntohs(udp_key->udp_dst));
834 break;
835
df2c07f4 836 case OVS_KEY_ATTR_ICMP:
36956a7d 837 icmp_key = nl_attr_get(a);
b3b5bf02 838 ds_put_format(ds, "(type=%"PRIu8",code=%"PRIu8")",
36956a7d
BP
839 icmp_key->icmp_type, icmp_key->icmp_code);
840 break;
841
df2c07f4 842 case OVS_KEY_ATTR_ICMPV6:
d31f1109 843 icmpv6_key = nl_attr_get(a);
b3b5bf02 844 ds_put_format(ds, "(type=%"PRIu8",code=%"PRIu8")",
d31f1109
JP
845 icmpv6_key->icmpv6_type, icmpv6_key->icmpv6_code);
846 break;
847
df2c07f4 848 case OVS_KEY_ATTR_ARP:
36956a7d 849 arp_key = nl_attr_get(a);
b3b5bf02 850 ds_put_format(ds, "(sip="IP_FMT",tip="IP_FMT",op=%"PRIu16","
bad68a99 851 "sha="ETH_ADDR_FMT",tha="ETH_ADDR_FMT")",
36956a7d 852 IP_ARGS(&arp_key->arp_sip), IP_ARGS(&arp_key->arp_tip),
bad68a99
JP
853 ntohs(arp_key->arp_op), ETH_ADDR_ARGS(arp_key->arp_sha),
854 ETH_ADDR_ARGS(arp_key->arp_tha));
36956a7d
BP
855 break;
856
df2c07f4 857 case OVS_KEY_ATTR_ND: {
685a51a5
JP
858 char target[INET6_ADDRSTRLEN];
859
860 nd_key = nl_attr_get(a);
861 inet_ntop(AF_INET6, nd_key->nd_target, target, sizeof target);
862
b3b5bf02 863 ds_put_format(ds, "(target=%s", target);
685a51a5
JP
864 if (!eth_addr_is_zero(nd_key->nd_sll)) {
865 ds_put_format(ds, ",sll="ETH_ADDR_FMT,
866 ETH_ADDR_ARGS(nd_key->nd_sll));
867 }
868 if (!eth_addr_is_zero(nd_key->nd_tll)) {
869 ds_put_format(ds, ",tll="ETH_ADDR_FMT,
870 ETH_ADDR_ARGS(nd_key->nd_tll));
871 }
872 ds_put_char(ds, ')');
873 break;
874 }
875
7a8e9ed2
BP
876 case OVS_KEY_ATTR_UNSPEC:
877 case __OVS_KEY_ATTR_MAX:
36956a7d
BP
878 default:
879 format_generic_odp_key(a, ds);
880 break;
881 }
882}
883
884/* Appends to 'ds' a string representation of the 'key_len' bytes of
df2c07f4 885 * OVS_KEY_ATTR_* attributes in 'key'. */
14608a15 886void
36956a7d 887odp_flow_key_format(const struct nlattr *key, size_t key_len, struct ds *ds)
14608a15 888{
36956a7d
BP
889 if (key_len) {
890 const struct nlattr *a;
891 unsigned int left;
892
893 NL_ATTR_FOR_EACH (a, left, key, key_len) {
894 if (a != key) {
895 ds_put_char(ds, ',');
896 }
897 format_odp_key_attr(a, ds);
898 }
899 if (left) {
3a8cfc50
BP
900 int i;
901
36956a7d
BP
902 if (left == key_len) {
903 ds_put_cstr(ds, "<empty>");
904 }
3a8cfc50
BP
905 ds_put_format(ds, ",***%u leftover bytes*** (", left);
906 for (i = 0; i < left; i++) {
907 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
908 }
909 ds_put_char(ds, ')');
36956a7d
BP
910 }
911 } else {
912 ds_put_cstr(ds, "<empty>");
913 }
14608a15 914}
064af421 915
3bffc610
BP
916static int
917put_nd_key(int n, const char *nd_target_s,
918 const uint8_t *nd_sll, const uint8_t *nd_tll, struct ofpbuf *key)
919{
df2c07f4 920 struct ovs_key_nd nd_key;
3bffc610
BP
921
922 memset(&nd_key, 0, sizeof nd_key);
923 if (inet_pton(AF_INET6, nd_target_s, nd_key.nd_target) != 1) {
924 return -EINVAL;
925 }
926 if (nd_sll) {
927 memcpy(nd_key.nd_sll, nd_sll, ETH_ADDR_LEN);
928 }
929 if (nd_tll) {
930 memcpy(nd_key.nd_tll, nd_tll, ETH_ADDR_LEN);
931 }
df2c07f4 932 nl_msg_put_unspec(key, OVS_KEY_ATTR_ND, &nd_key, sizeof nd_key);
3bffc610
BP
933 return n;
934}
935
7257b535
BP
936static bool
937ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
938{
939 if (!strcasecmp(s, "no")) {
940 *type = OVS_FRAG_TYPE_NONE;
941 } else if (!strcasecmp(s, "first")) {
942 *type = OVS_FRAG_TYPE_FIRST;
943 } else if (!strcasecmp(s, "later")) {
944 *type = OVS_FRAG_TYPE_LATER;
945 } else {
946 return false;
947 }
948 return true;
949}
950
3bffc610 951static int
44bac24b 952parse_odp_key_attr(const char *s, const struct simap *port_names,
b2a60db8 953 struct ofpbuf *key)
3bffc610
BP
954{
955 /* Many of the sscanf calls in this function use oversized destination
956 * fields because some sscanf() implementations truncate the range of %i
957 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
958 * value of 0x7fff. The other alternatives are to allow only a single
959 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
960 * parsers.
961 *
962 * The tun_id parser has to use an alternative approach because there is no
963 * type larger than 64 bits. */
964
abff858b
PS
965 {
966 unsigned long long int priority;
967 int n = -1;
968
969 if (sscanf(s, "priority(%lli)%n", &priority, &n) > 0 && n > 0) {
970 nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority);
971 return n;
972 }
973 }
974
3bffc610
BP
975 {
976 char tun_id_s[32];
977 int n = -1;
978
979 if (sscanf(s, "tun_id(%31[x0123456789abcdefABCDEF])%n",
980 tun_id_s, &n) > 0 && n > 0) {
981 uint64_t tun_id = strtoull(tun_id_s, NULL, 0);
df2c07f4 982 nl_msg_put_be64(key, OVS_KEY_ATTR_TUN_ID, htonll(tun_id));
3bffc610
BP
983 return n;
984 }
985 }
986
35651d6a
JG
987 {
988 char tun_id_s[32];
35651d6a
JG
989 int tos, ttl;
990 struct ovs_key_ipv4_tunnel tun_key;
991 int n = -1;
992
993 if (sscanf(s, "ipv4_tunnel(tun_id=%31[x0123456789abcdefABCDEF],"
29c7a2b2
PS
994 "src="IP_SCAN_FMT",dst="IP_SCAN_FMT
995 ",tos=%i,ttl=%i,flags%n", tun_id_s,
35651d6a
JG
996 IP_SCAN_ARGS(&tun_key.ipv4_src),
997 IP_SCAN_ARGS(&tun_key.ipv4_dst), &tos, &ttl,
998 &n) > 0 && n > 0) {
29c7a2b2
PS
999 int res;
1000
35651d6a 1001 tun_key.tun_id = htonll(strtoull(tun_id_s, NULL, 0));
35651d6a
JG
1002 tun_key.ipv4_tos = tos;
1003 tun_key.ipv4_ttl = ttl;
29c7a2b2
PS
1004
1005 res = parse_flags(&s[n], tun_flag_to_string, &tun_key.tun_flags);
1006 if (res < 0) {
1007 return res;
1008 }
1009 n += res;
1010 if (s[n] != ')') {
1011 return -EINVAL;
1012 }
1013 n++;
1014
35651d6a
JG
1015 memset(&tun_key.pad, 0, sizeof tun_key.pad);
1016 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4_TUNNEL, &tun_key,
1017 sizeof tun_key);
1018 return n;
1019 }
1020 }
1021
3bffc610
BP
1022 {
1023 unsigned long long int in_port;
1024 int n = -1;
1025
1026 if (sscanf(s, "in_port(%lli)%n", &in_port, &n) > 0 && n > 0) {
df2c07f4 1027 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port);
3bffc610
BP
1028 return n;
1029 }
1030 }
1031
b2a60db8
BP
1032 if (port_names && !strncmp(s, "in_port(", 8)) {
1033 const char *name;
44bac24b 1034 const struct simap_node *node;
b2a60db8
BP
1035 int name_len;
1036
1037 name = s + 8;
1038 name_len = strcspn(s, ")");
44bac24b 1039 node = simap_find_len(port_names, name, name_len);
b2a60db8 1040 if (node) {
44bac24b 1041 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, node->data);
b2a60db8
BP
1042 return 8 + name_len + 1;
1043 }
1044 }
1045
3bffc610 1046 {
df2c07f4 1047 struct ovs_key_ethernet eth_key;
3bffc610
BP
1048 int n = -1;
1049
1050 if (sscanf(s,
1051 "eth(src="ETH_ADDR_SCAN_FMT",dst="ETH_ADDR_SCAN_FMT")%n",
1052 ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
1053 ETH_ADDR_SCAN_ARGS(eth_key.eth_dst), &n) > 0 && n > 0) {
df2c07f4 1054 nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET,
3bffc610
BP
1055 &eth_key, sizeof eth_key);
1056 return n;
1057 }
1058 }
1059
1060 {
3bffc610
BP
1061 uint16_t vid;
1062 int pcp;
8ddc056d 1063 int cfi;
3bffc610
BP
1064 int n = -1;
1065
fea393b1
BP
1066 if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i)%n", &vid, &pcp, &n) > 0
1067 && n > 0)) {
1068 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1069 htons((vid << VLAN_VID_SHIFT) |
8ddc056d
BP
1070 (pcp << VLAN_PCP_SHIFT) |
1071 VLAN_CFI));
1072 return n;
1073 } else if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i,cfi=%i)%n",
1074 &vid, &pcp, &cfi, &n) > 0
1075 && n > 0)) {
1076 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1077 htons((vid << VLAN_VID_SHIFT) |
1078 (pcp << VLAN_PCP_SHIFT) |
1079 (cfi ? VLAN_CFI : 0)));
3bffc610
BP
1080 return n;
1081 }
1082 }
1083
1084 {
fac2e516 1085 int eth_type;
3bffc610
BP
1086 int n = -1;
1087
fac2e516 1088 if (sscanf(s, "eth_type(%i)%n", &eth_type, &n) > 0 && n > 0) {
df2c07f4 1089 nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type));
3bffc610
BP
1090 return n;
1091 }
1092 }
1093
1094 {
1095 ovs_be32 ipv4_src;
1096 ovs_be32 ipv4_dst;
1097 int ipv4_proto;
1098 int ipv4_tos;
a61680c6 1099 int ipv4_ttl;
7257b535
BP
1100 char frag[8];
1101 enum ovs_frag_type ipv4_frag;
3bffc610
BP
1102 int n = -1;
1103
1104 if (sscanf(s, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT","
a61680c6 1105 "proto=%i,tos=%i,ttl=%i,frag=%7[a-z])%n",
7257b535 1106 IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_dst),
a61680c6 1107 &ipv4_proto, &ipv4_tos, &ipv4_ttl, frag, &n) > 0
7257b535
BP
1108 && n > 0
1109 && ovs_frag_type_from_string(frag, &ipv4_frag)) {
df2c07f4 1110 struct ovs_key_ipv4 ipv4_key;
3bffc610 1111
3bffc610
BP
1112 ipv4_key.ipv4_src = ipv4_src;
1113 ipv4_key.ipv4_dst = ipv4_dst;
1114 ipv4_key.ipv4_proto = ipv4_proto;
1115 ipv4_key.ipv4_tos = ipv4_tos;
a61680c6 1116 ipv4_key.ipv4_ttl = ipv4_ttl;
7257b535 1117 ipv4_key.ipv4_frag = ipv4_frag;
df2c07f4 1118 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
3bffc610
BP
1119 &ipv4_key, sizeof ipv4_key);
1120 return n;
1121 }
1122 }
1123
1124 {
1125 char ipv6_src_s[IPV6_SCAN_LEN + 1];
1126 char ipv6_dst_s[IPV6_SCAN_LEN + 1];
fa8223b7 1127 int ipv6_label;
3bffc610 1128 int ipv6_proto;
60258dcb 1129 int ipv6_tclass;
a61680c6 1130 int ipv6_hlimit;
7257b535
BP
1131 char frag[8];
1132 enum ovs_frag_type ipv6_frag;
3bffc610
BP
1133 int n = -1;
1134
1135 if (sscanf(s, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT","
60258dcb 1136 "label=%i,proto=%i,tclass=%i,hlimit=%i,frag=%7[a-z])%n",
fa8223b7 1137 ipv6_src_s, ipv6_dst_s, &ipv6_label,
60258dcb 1138 &ipv6_proto, &ipv6_tclass, &ipv6_hlimit, frag, &n) > 0
7257b535
BP
1139 && n > 0
1140 && ovs_frag_type_from_string(frag, &ipv6_frag)) {
df2c07f4 1141 struct ovs_key_ipv6 ipv6_key;
3bffc610 1142
3bffc610
BP
1143 if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
1144 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1) {
1145 return -EINVAL;
1146 }
fa8223b7 1147 ipv6_key.ipv6_label = htonl(ipv6_label);
3bffc610 1148 ipv6_key.ipv6_proto = ipv6_proto;
60258dcb 1149 ipv6_key.ipv6_tclass = ipv6_tclass;
a61680c6 1150 ipv6_key.ipv6_hlimit = ipv6_hlimit;
7257b535 1151 ipv6_key.ipv6_frag = ipv6_frag;
df2c07f4 1152 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
3bffc610
BP
1153 &ipv6_key, sizeof ipv6_key);
1154 return n;
1155 }
1156 }
1157
1158 {
1159 int tcp_src;
1160 int tcp_dst;
1161 int n = -1;
1162
1163 if (sscanf(s, "tcp(src=%i,dst=%i)%n",&tcp_src, &tcp_dst, &n) > 0
1164 && n > 0) {
df2c07f4 1165 struct ovs_key_tcp tcp_key;
3bffc610
BP
1166
1167 tcp_key.tcp_src = htons(tcp_src);
1168 tcp_key.tcp_dst = htons(tcp_dst);
df2c07f4 1169 nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
3bffc610
BP
1170 return n;
1171 }
1172 }
1173
1174 {
1175 int udp_src;
1176 int udp_dst;
1177 int n = -1;
1178
1179 if (sscanf(s, "udp(src=%i,dst=%i)%n", &udp_src, &udp_dst, &n) > 0
1180 && n > 0) {
df2c07f4 1181 struct ovs_key_udp udp_key;
3bffc610
BP
1182
1183 udp_key.udp_src = htons(udp_src);
1184 udp_key.udp_dst = htons(udp_dst);
df2c07f4 1185 nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
3bffc610
BP
1186 return n;
1187 }
1188 }
1189
1190 {
1191 int icmp_type;
1192 int icmp_code;
1193 int n = -1;
1194
1195 if (sscanf(s, "icmp(type=%i,code=%i)%n",
1196 &icmp_type, &icmp_code, &n) > 0
1197 && n > 0) {
df2c07f4 1198 struct ovs_key_icmp icmp_key;
3bffc610
BP
1199
1200 icmp_key.icmp_type = icmp_type;
1201 icmp_key.icmp_code = icmp_code;
df2c07f4 1202 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
3bffc610
BP
1203 &icmp_key, sizeof icmp_key);
1204 return n;
1205 }
1206 }
1207
1208 {
df2c07f4 1209 struct ovs_key_icmpv6 icmpv6_key;
3bffc610
BP
1210 int n = -1;
1211
1212 if (sscanf(s, "icmpv6(type=%"SCNi8",code=%"SCNi8")%n",
1213 &icmpv6_key.icmpv6_type, &icmpv6_key.icmpv6_code,&n) > 0
1214 && n > 0) {
df2c07f4 1215 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
3bffc610
BP
1216 &icmpv6_key, sizeof icmpv6_key);
1217 return n;
1218 }
1219 }
1220
1221 {
1222 ovs_be32 arp_sip;
1223 ovs_be32 arp_tip;
1224 int arp_op;
1225 uint8_t arp_sha[ETH_ADDR_LEN];
1226 uint8_t arp_tha[ETH_ADDR_LEN];
1227 int n = -1;
1228
1229 if (sscanf(s, "arp(sip="IP_SCAN_FMT",tip="IP_SCAN_FMT","
1230 "op=%i,sha="ETH_ADDR_SCAN_FMT",tha="ETH_ADDR_SCAN_FMT")%n",
1231 IP_SCAN_ARGS(&arp_sip),
1232 IP_SCAN_ARGS(&arp_tip),
1233 &arp_op,
1234 ETH_ADDR_SCAN_ARGS(arp_sha),
1235 ETH_ADDR_SCAN_ARGS(arp_tha), &n) > 0 && n > 0) {
df2c07f4 1236 struct ovs_key_arp arp_key;
3bffc610
BP
1237
1238 memset(&arp_key, 0, sizeof arp_key);
1239 arp_key.arp_sip = arp_sip;
1240 arp_key.arp_tip = arp_tip;
1241 arp_key.arp_op = htons(arp_op);
1242 memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN);
1243 memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN);
df2c07f4 1244 nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
3bffc610
BP
1245 return n;
1246 }
1247 }
1248
1249 {
1250 char nd_target_s[IPV6_SCAN_LEN + 1];
1251 uint8_t nd_sll[ETH_ADDR_LEN];
1252 uint8_t nd_tll[ETH_ADDR_LEN];
1253 int n = -1;
1254
1255 if (sscanf(s, "nd(target="IPV6_SCAN_FMT")%n",
1256 nd_target_s, &n) > 0 && n > 0) {
1257 return put_nd_key(n, nd_target_s, NULL, NULL, key);
1258 }
1259 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT")%n",
1260 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), &n) > 0
1261 && n > 0) {
1262 return put_nd_key(n, nd_target_s, nd_sll, NULL, key);
1263 }
1264 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",tll="ETH_ADDR_SCAN_FMT")%n",
1265 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
1266 && n > 0) {
1267 return put_nd_key(n, nd_target_s, NULL, nd_tll, key);
1268 }
1269 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT","
1270 "tll="ETH_ADDR_SCAN_FMT")%n",
1271 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll),
1272 ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
1273 && n > 0) {
1274 return put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
1275 }
1276 }
1277
fea393b1
BP
1278 if (!strncmp(s, "encap(", 6)) {
1279 const char *start = s;
1280 size_t encap;
1281
1282 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
1283
1284 s += 6;
1285 for (;;) {
1286 int retval;
1287
1288 s += strspn(s, ", \t\r\n");
1289 if (!*s) {
1290 return -EINVAL;
1291 } else if (*s == ')') {
1292 break;
1293 }
1294
becffb86 1295 retval = parse_odp_key_attr(s, port_names, key);
fea393b1
BP
1296 if (retval < 0) {
1297 return retval;
1298 }
1299 s += retval;
1300 }
1301 s++;
1302
1303 nl_msg_end_nested(key, encap);
1304
1305 return s - start;
1306 }
1307
3bffc610
BP
1308 return -EINVAL;
1309}
1310
df2c07f4
JP
1311/* Parses the string representation of a datapath flow key, in the
1312 * format output by odp_flow_key_format(). Returns 0 if successful,
1313 * otherwise a positive errno value. On success, the flow key is
1314 * appended to 'key' as a series of Netlink attributes. On failure, no
1315 * data is appended to 'key'. Either way, 'key''s data might be
1316 * reallocated.
3bffc610 1317 *
44bac24b
BP
1318 * If 'port_names' is nonnull, it points to an simap that maps from a port name
1319 * to a port number. (Port names may be used instead of port numbers in
1320 * in_port.)
b2a60db8 1321 *
3bffc610
BP
1322 * On success, the attributes appended to 'key' are individually syntactically
1323 * valid, but they may not be valid as a sequence. 'key' might, for example,
34118cae 1324 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
3bffc610 1325int
44bac24b 1326odp_flow_key_from_string(const char *s, const struct simap *port_names,
b2a60db8 1327 struct ofpbuf *key)
3bffc610
BP
1328{
1329 const size_t old_size = key->size;
1330 for (;;) {
1331 int retval;
1332
7202cbe5 1333 s += strspn(s, delimiters);
3bffc610
BP
1334 if (!*s) {
1335 return 0;
1336 }
1337
b2a60db8 1338 retval = parse_odp_key_attr(s, port_names, key);
3bffc610
BP
1339 if (retval < 0) {
1340 key->size = old_size;
1341 return -retval;
1342 }
1343 s += retval;
1344 }
1345
1346 return 0;
1347}
1348
7257b535 1349static uint8_t
c4f2731d 1350ovs_to_odp_frag(uint8_t nw_frag)
7257b535 1351{
c4f2731d
PS
1352 return (nw_frag == 0 ? OVS_FRAG_TYPE_NONE
1353 : nw_frag == FLOW_NW_FRAG_ANY ? OVS_FRAG_TYPE_FIRST
1354 : OVS_FRAG_TYPE_LATER);
7257b535
BP
1355}
1356
2508ac16 1357/* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
ddbfda84
JP
1358 * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
1359 * number rather than a datapath port number). Instead, if 'odp_in_port'
1360 * is anything other than OVSP_NONE, it is included in 'buf' as the input
1361 * port.
2508ac16
BP
1362 *
1363 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
1364 * capable of being expanded to allow for that much space. */
14608a15 1365void
ddbfda84
JP
1366odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow,
1367 uint32_t odp_in_port)
14608a15 1368{
df2c07f4 1369 struct ovs_key_ethernet *eth_key;
fea393b1 1370 size_t encap;
36956a7d 1371
deedf7e7
BP
1372 if (flow->skb_priority) {
1373 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, flow->skb_priority);
abff858b
PS
1374 }
1375
296e07ac
JG
1376 if (flow->tunnel.tun_id != htonll(0)) {
1377 nl_msg_put_be64(buf, OVS_KEY_ATTR_TUN_ID, flow->tunnel.tun_id);
36956a7d
BP
1378 }
1379
ddbfda84
JP
1380 if (odp_in_port != OVSP_NONE) {
1381 nl_msg_put_u32(buf, OVS_KEY_ATTR_IN_PORT, odp_in_port);
18886b60 1382 }
36956a7d 1383
df2c07f4 1384 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
36956a7d
BP
1385 sizeof *eth_key);
1386 memcpy(eth_key->eth_src, flow->dl_src, ETH_ADDR_LEN);
1387 memcpy(eth_key->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
1388
8ddc056d 1389 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
fea393b1 1390 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
8ddc056d 1391 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, flow->vlan_tci);
fea393b1 1392 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
8ddc056d
BP
1393 if (flow->vlan_tci == htons(0)) {
1394 goto unencap;
1395 }
fea393b1
BP
1396 } else {
1397 encap = 0;
36956a7d
BP
1398 }
1399
1400 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
fea393b1 1401 goto unencap;
36956a7d
BP
1402 }
1403
df2c07f4 1404 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, flow->dl_type);
36956a7d
BP
1405
1406 if (flow->dl_type == htons(ETH_TYPE_IP)) {
df2c07f4 1407 struct ovs_key_ipv4 *ipv4_key;
36956a7d 1408
df2c07f4 1409 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
36956a7d
BP
1410 sizeof *ipv4_key);
1411 ipv4_key->ipv4_src = flow->nw_src;
1412 ipv4_key->ipv4_dst = flow->nw_dst;
1413 ipv4_key->ipv4_proto = flow->nw_proto;
eadef313 1414 ipv4_key->ipv4_tos = flow->nw_tos;
a61680c6 1415 ipv4_key->ipv4_ttl = flow->nw_ttl;
eadef313 1416 ipv4_key->ipv4_frag = ovs_to_odp_frag(flow->nw_frag);
d31f1109 1417 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
df2c07f4 1418 struct ovs_key_ipv6 *ipv6_key;
d31f1109 1419
df2c07f4 1420 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
d31f1109
JP
1421 sizeof *ipv6_key);
1422 memcpy(ipv6_key->ipv6_src, &flow->ipv6_src, sizeof ipv6_key->ipv6_src);
1423 memcpy(ipv6_key->ipv6_dst, &flow->ipv6_dst, sizeof ipv6_key->ipv6_dst);
fa8223b7 1424 ipv6_key->ipv6_label = flow->ipv6_label;
d31f1109 1425 ipv6_key->ipv6_proto = flow->nw_proto;
eadef313 1426 ipv6_key->ipv6_tclass = flow->nw_tos;
a61680c6 1427 ipv6_key->ipv6_hlimit = flow->nw_ttl;
eadef313 1428 ipv6_key->ipv6_frag = ovs_to_odp_frag(flow->nw_frag);
8087f5ff
MM
1429 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
1430 flow->dl_type == htons(ETH_TYPE_RARP)) {
df2c07f4 1431 struct ovs_key_arp *arp_key;
d31f1109 1432
df2c07f4 1433 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
d31f1109 1434 sizeof *arp_key);
535d6987 1435 memset(arp_key, 0, sizeof *arp_key);
d31f1109
JP
1436 arp_key->arp_sip = flow->nw_src;
1437 arp_key->arp_tip = flow->nw_dst;
1438 arp_key->arp_op = htons(flow->nw_proto);
1439 memcpy(arp_key->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
1440 memcpy(arp_key->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
1441 }
b53055f4 1442
7257b535
BP
1443 if ((flow->dl_type == htons(ETH_TYPE_IP)
1444 || flow->dl_type == htons(ETH_TYPE_IPV6))
eadef313 1445 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
36956a7d 1446
6767a2cc 1447 if (flow->nw_proto == IPPROTO_TCP) {
df2c07f4 1448 struct ovs_key_tcp *tcp_key;
36956a7d 1449
df2c07f4 1450 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
36956a7d
BP
1451 sizeof *tcp_key);
1452 tcp_key->tcp_src = flow->tp_src;
1453 tcp_key->tcp_dst = flow->tp_dst;
6767a2cc 1454 } else if (flow->nw_proto == IPPROTO_UDP) {
df2c07f4 1455 struct ovs_key_udp *udp_key;
36956a7d 1456
df2c07f4 1457 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
36956a7d
BP
1458 sizeof *udp_key);
1459 udp_key->udp_src = flow->tp_src;
1460 udp_key->udp_dst = flow->tp_dst;
d31f1109
JP
1461 } else if (flow->dl_type == htons(ETH_TYPE_IP)
1462 && flow->nw_proto == IPPROTO_ICMP) {
df2c07f4 1463 struct ovs_key_icmp *icmp_key;
36956a7d 1464
df2c07f4 1465 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
36956a7d
BP
1466 sizeof *icmp_key);
1467 icmp_key->icmp_type = ntohs(flow->tp_src);
1468 icmp_key->icmp_code = ntohs(flow->tp_dst);
d31f1109
JP
1469 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
1470 && flow->nw_proto == IPPROTO_ICMPV6) {
df2c07f4 1471 struct ovs_key_icmpv6 *icmpv6_key;
d31f1109 1472
df2c07f4 1473 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
d31f1109
JP
1474 sizeof *icmpv6_key);
1475 icmpv6_key->icmpv6_type = ntohs(flow->tp_src);
1476 icmpv6_key->icmpv6_code = ntohs(flow->tp_dst);
685a51a5
JP
1477
1478 if (icmpv6_key->icmpv6_type == ND_NEIGHBOR_SOLICIT
1479 || icmpv6_key->icmpv6_type == ND_NEIGHBOR_ADVERT) {
df2c07f4 1480 struct ovs_key_nd *nd_key;
685a51a5 1481
df2c07f4 1482 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
685a51a5
JP
1483 sizeof *nd_key);
1484 memcpy(nd_key->nd_target, &flow->nd_target,
1485 sizeof nd_key->nd_target);
1486 memcpy(nd_key->nd_sll, flow->arp_sha, ETH_ADDR_LEN);
1487 memcpy(nd_key->nd_tll, flow->arp_tha, ETH_ADDR_LEN);
1488 }
36956a7d 1489 }
36956a7d 1490 }
fea393b1
BP
1491
1492unencap:
1493 if (encap) {
1494 nl_msg_end_nested(buf, encap);
1495 }
36956a7d
BP
1496}
1497
b0f7b9b5
BP
1498uint32_t
1499odp_flow_key_hash(const struct nlattr *key, size_t key_len)
1500{
1501 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
1502 return hash_words((const uint32_t *) key, key_len / sizeof(uint32_t), 0);
1503}
1504
34118cae
BP
1505static void
1506log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
b0f7b9b5 1507 uint64_t attrs, int out_of_range_attr,
34118cae
BP
1508 const struct nlattr *key, size_t key_len)
1509{
1510 struct ds s;
1511 int i;
1512
b0f7b9b5 1513 if (VLOG_DROP_DBG(rl)) {
34118cae
BP
1514 return;
1515 }
1516
1517 ds_init(&s);
b0f7b9b5
BP
1518 for (i = 0; i < 64; i++) {
1519 if (attrs & (UINT64_C(1) << i)) {
34118cae
BP
1520 ds_put_format(&s, " %s", ovs_key_attr_to_string(i));
1521 }
1522 }
b0f7b9b5
BP
1523 if (out_of_range_attr) {
1524 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
1525 }
34118cae
BP
1526
1527 ds_put_cstr(&s, ": ");
1528 odp_flow_key_format(key, key_len, &s);
1529
b0f7b9b5 1530 VLOG_DBG("%s:%s", title, ds_cstr(&s));
34118cae
BP
1531 ds_destroy(&s);
1532}
1533
7257b535 1534static bool
9e44d715 1535odp_to_ovs_frag(uint8_t odp_frag, struct flow *flow)
7257b535 1536{
34118cae
BP
1537 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1538
9e44d715 1539 if (odp_frag > OVS_FRAG_TYPE_LATER) {
b0f7b9b5 1540 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
7257b535
BP
1541 return false;
1542 }
1543
7257b535 1544 if (odp_frag != OVS_FRAG_TYPE_NONE) {
eadef313 1545 flow->nw_frag |= FLOW_NW_FRAG_ANY;
7257b535 1546 if (odp_frag == OVS_FRAG_TYPE_LATER) {
eadef313 1547 flow->nw_frag |= FLOW_NW_FRAG_LATER;
7257b535
BP
1548 }
1549 }
1550 return true;
1551}
1552
b0f7b9b5 1553static bool
fea393b1 1554parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
b0f7b9b5
BP
1555 const struct nlattr *attrs[], uint64_t *present_attrsp,
1556 int *out_of_range_attrp)
36956a7d 1557{
b0f7b9b5 1558 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
36956a7d 1559 const struct nlattr *nla;
34118cae 1560 uint64_t present_attrs;
36956a7d
BP
1561 size_t left;
1562
34118cae 1563 present_attrs = 0;
b0f7b9b5 1564 *out_of_range_attrp = 0;
36956a7d 1565 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
34118cae
BP
1566 uint16_t type = nl_attr_type(nla);
1567 size_t len = nl_attr_get_size(nla);
1568 int expected_len = odp_flow_key_attr_len(type);
1569
b0f7b9b5
BP
1570 if (len != expected_len && expected_len >= 0) {
1571 VLOG_ERR_RL(&rl, "attribute %s has length %zu but should have "
1572 "length %d", ovs_key_attr_to_string(type),
1573 len, expected_len);
1574 return false;
34118cae
BP
1575 }
1576
b0f7b9b5
BP
1577 if (type >= CHAR_BIT * sizeof present_attrs) {
1578 *out_of_range_attrp = type;
1579 } else {
1580 if (present_attrs & (UINT64_C(1) << type)) {
1581 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
1582 ovs_key_attr_to_string(type));
1583 return false;
1584 }
1585
1586 present_attrs |= UINT64_C(1) << type;
1587 attrs[type] = nla;
1588 }
34118cae
BP
1589 }
1590 if (left) {
1591 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
b0f7b9b5 1592 return false;
34118cae
BP
1593 }
1594
fea393b1 1595 *present_attrsp = present_attrs;
b0f7b9b5 1596 return true;
fea393b1
BP
1597}
1598
b0f7b9b5
BP
1599static enum odp_key_fitness
1600check_expectations(uint64_t present_attrs, int out_of_range_attr,
1601 uint64_t expected_attrs,
fea393b1
BP
1602 const struct nlattr *key, size_t key_len)
1603{
1604 uint64_t missing_attrs;
1605 uint64_t extra_attrs;
1606
1607 missing_attrs = expected_attrs & ~present_attrs;
1608 if (missing_attrs) {
b0f7b9b5
BP
1609 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
1610 log_odp_key_attributes(&rl, "expected but not present",
1611 missing_attrs, 0, key, key_len);
1612 return ODP_FIT_TOO_LITTLE;
fea393b1
BP
1613 }
1614
1615 extra_attrs = present_attrs & ~expected_attrs;
b0f7b9b5
BP
1616 if (extra_attrs || out_of_range_attr) {
1617 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
1618 log_odp_key_attributes(&rl, "present but not expected",
1619 extra_attrs, out_of_range_attr, key, key_len);
1620 return ODP_FIT_TOO_MUCH;
fea393b1
BP
1621 }
1622
b0f7b9b5 1623 return ODP_FIT_PERFECT;
fea393b1
BP
1624}
1625
b0f7b9b5
BP
1626static bool
1627parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
1628 uint64_t present_attrs, uint64_t *expected_attrs,
1629 struct flow *flow)
fea393b1
BP
1630{
1631 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
36956a7d 1632
fea393b1 1633 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
34118cae
BP
1634 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
1635 if (ntohs(flow->dl_type) < 1536) {
1636 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
1637 ntohs(flow->dl_type));
b0f7b9b5 1638 return false;
34118cae 1639 }
b0f7b9b5 1640 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
34118cae
BP
1641 } else {
1642 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
1643 }
b0f7b9b5
BP
1644 return true;
1645}
1646
1647static enum odp_key_fitness
1648parse_l3_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
1649 uint64_t present_attrs, int out_of_range_attr,
1650 uint64_t expected_attrs, struct flow *flow,
1651 const struct nlattr *key, size_t key_len)
1652{
1653 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
36956a7d 1654
34118cae
BP
1655 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1656 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
fea393b1 1657 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
34118cae 1658 const struct ovs_key_ipv4 *ipv4_key;
36956a7d 1659
34118cae 1660 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
36956a7d
BP
1661 flow->nw_src = ipv4_key->ipv4_src;
1662 flow->nw_dst = ipv4_key->ipv4_dst;
1663 flow->nw_proto = ipv4_key->ipv4_proto;
eadef313 1664 flow->nw_tos = ipv4_key->ipv4_tos;
a61680c6 1665 flow->nw_ttl = ipv4_key->ipv4_ttl;
9e44d715 1666 if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) {
b0f7b9b5 1667 return ODP_FIT_ERROR;
36956a7d 1668 }
34118cae
BP
1669 }
1670 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1671 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
fea393b1 1672 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
34118cae 1673 const struct ovs_key_ipv6 *ipv6_key;
36956a7d 1674
34118cae 1675 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
d31f1109
JP
1676 memcpy(&flow->ipv6_src, ipv6_key->ipv6_src, sizeof flow->ipv6_src);
1677 memcpy(&flow->ipv6_dst, ipv6_key->ipv6_dst, sizeof flow->ipv6_dst);
fa8223b7 1678 flow->ipv6_label = ipv6_key->ipv6_label;
d31f1109 1679 flow->nw_proto = ipv6_key->ipv6_proto;
eadef313 1680 flow->nw_tos = ipv6_key->ipv6_tclass;
a61680c6 1681 flow->nw_ttl = ipv6_key->ipv6_hlimit;
9e44d715 1682 if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) {
b0f7b9b5 1683 return ODP_FIT_ERROR;
d31f1109 1684 }
34118cae 1685 }
8087f5ff
MM
1686 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
1687 flow->dl_type == htons(ETH_TYPE_RARP)) {
34118cae 1688 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
fea393b1 1689 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
34118cae 1690 const struct ovs_key_arp *arp_key;
d31f1109 1691
34118cae 1692 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
36956a7d
BP
1693 flow->nw_src = arp_key->arp_sip;
1694 flow->nw_dst = arp_key->arp_tip;
1695 if (arp_key->arp_op & htons(0xff00)) {
34118cae
BP
1696 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
1697 "key", ntohs(arp_key->arp_op));
b0f7b9b5 1698 return ODP_FIT_ERROR;
36956a7d
BP
1699 }
1700 flow->nw_proto = ntohs(arp_key->arp_op);
bad68a99
JP
1701 memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN);
1702 memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN);
36956a7d 1703 }
36956a7d 1704 }
36956a7d 1705
34118cae
BP
1706 if (flow->nw_proto == IPPROTO_TCP
1707 && (flow->dl_type == htons(ETH_TYPE_IP) ||
1708 flow->dl_type == htons(ETH_TYPE_IPV6))
1709 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1710 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
fea393b1 1711 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
34118cae 1712 const struct ovs_key_tcp *tcp_key;
36956a7d 1713
34118cae
BP
1714 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
1715 flow->tp_src = tcp_key->tcp_src;
1716 flow->tp_dst = tcp_key->tcp_dst;
7257b535 1717 }
34118cae
BP
1718 } else if (flow->nw_proto == IPPROTO_UDP
1719 && (flow->dl_type == htons(ETH_TYPE_IP) ||
1720 flow->dl_type == htons(ETH_TYPE_IPV6))
1721 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1722 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
fea393b1 1723 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
34118cae
BP
1724 const struct ovs_key_udp *udp_key;
1725
1726 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
1727 flow->tp_src = udp_key->udp_src;
1728 flow->tp_dst = udp_key->udp_dst;
d31f1109 1729 }
34118cae
BP
1730 } else if (flow->nw_proto == IPPROTO_ICMP
1731 && flow->dl_type == htons(ETH_TYPE_IP)
1732 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1733 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
fea393b1 1734 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
34118cae
BP
1735 const struct ovs_key_icmp *icmp_key;
1736
1737 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
1738 flow->tp_src = htons(icmp_key->icmp_type);
1739 flow->tp_dst = htons(icmp_key->icmp_code);
685a51a5 1740 }
34118cae
BP
1741 } else if (flow->nw_proto == IPPROTO_ICMPV6
1742 && flow->dl_type == htons(ETH_TYPE_IPV6)
1743 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1744 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
fea393b1 1745 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
34118cae
BP
1746 const struct ovs_key_icmpv6 *icmpv6_key;
1747
1748 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
1749 flow->tp_src = htons(icmpv6_key->icmpv6_type);
1750 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
685a51a5 1751
34118cae
BP
1752 if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
1753 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
1754 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
fea393b1 1755 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
34118cae
BP
1756 const struct ovs_key_nd *nd_key;
1757
1758 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
1759 memcpy(&flow->nd_target, nd_key->nd_target,
1760 sizeof flow->nd_target);
1761 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
1762 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
1763 }
1764 }
7257b535 1765 }
34118cae 1766 }
7257b535 1767
b0f7b9b5
BP
1768 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
1769 key, key_len);
1770}
1771
1772/* Parse 802.1Q header then encapsulated L3 attributes. */
1773static enum odp_key_fitness
1774parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
1775 uint64_t present_attrs, int out_of_range_attr,
1776 uint64_t expected_attrs, struct flow *flow,
1777 const struct nlattr *key, size_t key_len)
1778{
1779 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1780
1781 const struct nlattr *encap
1782 = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
1783 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
1784 enum odp_key_fitness encap_fitness;
1785 enum odp_key_fitness fitness;
1786 ovs_be16 tci;
1787
1788 /* Calulate fitness of outer attributes. */
1789 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
1790 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
1791 fitness = check_expectations(present_attrs, out_of_range_attr,
1792 expected_attrs, key, key_len);
1793
1794 /* Get the VLAN TCI value. */
1795 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
1796 return ODP_FIT_TOO_LITTLE;
1797 }
1798 tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
1799 if (tci == htons(0)) {
1800 /* Corner case for a truncated 802.1Q header. */
1801 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
1802 return ODP_FIT_TOO_MUCH;
1803 }
1804 return fitness;
1805 } else if (!(tci & htons(VLAN_CFI))) {
1806 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
1807 "but CFI bit is not set", ntohs(tci));
1808 return ODP_FIT_ERROR;
1809 }
1810
1811 /* Set vlan_tci.
1812 * Remove the TPID from dl_type since it's not the real Ethertype. */
1813 flow->vlan_tci = tci;
1814 flow->dl_type = htons(0);
1815
1816 /* Now parse the encapsulated attributes. */
1817 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
1818 attrs, &present_attrs, &out_of_range_attr)) {
1819 return ODP_FIT_ERROR;
1820 }
1821 expected_attrs = 0;
1822
1823 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
1824 return ODP_FIT_ERROR;
1825 }
1826 encap_fitness = parse_l3_onward(attrs, present_attrs, out_of_range_attr,
1827 expected_attrs, flow, key, key_len);
1828
1829 /* The overall fitness is the worse of the outer and inner attributes. */
1830 return MAX(fitness, encap_fitness);
1831}
1832
1833/* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
1834 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
1835 * 'key' fits our expectations for what a flow key should contain.
1836 *
ddbfda84
JP
1837 * The 'in_port' will be the datapath's understanding of the port. The
1838 * caller will need to translate with odp_port_to_ofp_port() if the
1839 * OpenFlow port is needed.
1840 *
b0f7b9b5
BP
1841 * This function doesn't take the packet itself as an argument because none of
1842 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
1843 * it is always possible to infer which additional attribute(s) should appear
1844 * by looking at the attributes for lower-level protocols, e.g. if the network
1845 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
1846 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
1847 * must be absent. */
1848enum odp_key_fitness
1849odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
1850 struct flow *flow)
1851{
b0f7b9b5
BP
1852 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
1853 uint64_t expected_attrs;
1854 uint64_t present_attrs;
1855 int out_of_range_attr;
1856
1857 memset(flow, 0, sizeof *flow);
1858
1859 /* Parse attributes. */
1860 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
1861 &out_of_range_attr)) {
1862 return ODP_FIT_ERROR;
1863 }
1864 expected_attrs = 0;
1865
1866 /* Metadata. */
1867 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
deedf7e7 1868 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
b0f7b9b5
BP
1869 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
1870 }
1871
1872 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUN_ID)) {
296e07ac 1873 flow->tunnel.tun_id = nl_attr_get_be64(attrs[OVS_KEY_ATTR_TUN_ID]);
b0f7b9b5
BP
1874 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUN_ID;
1875 }
1876
1877 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
ddbfda84 1878 flow->in_port = nl_attr_get_u32(attrs[OVS_KEY_ATTR_IN_PORT]);
b0f7b9b5
BP
1879 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
1880 } else {
ddbfda84 1881 flow->in_port = OVSP_NONE;
b0f7b9b5
BP
1882 }
1883
1884 /* Ethernet header. */
1885 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
1886 const struct ovs_key_ethernet *eth_key;
1887
1888 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
1889 memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN);
1890 memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN);
1891 }
1892 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
1893
1894 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
1895 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
1896 return ODP_FIT_ERROR;
1897 }
1898
1899 if (flow->dl_type == htons(ETH_TYPE_VLAN)) {
1900 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
1901 expected_attrs, flow, key, key_len);
1902 }
1903 return parse_l3_onward(attrs, present_attrs, out_of_range_attr,
1904 expected_attrs, flow, key, key_len);
14608a15 1905}
39db78a0 1906
6814e51f
BP
1907/* Returns 'fitness' as a string, for use in debug messages. */
1908const char *
1909odp_key_fitness_to_string(enum odp_key_fitness fitness)
1910{
1911 switch (fitness) {
1912 case ODP_FIT_PERFECT:
1913 return "OK";
1914 case ODP_FIT_TOO_MUCH:
1915 return "too_much";
1916 case ODP_FIT_TOO_LITTLE:
1917 return "too_little";
1918 case ODP_FIT_ERROR:
1919 return "error";
1920 default:
1921 return "<unknown>";
1922 }
1923}
1924
39db78a0
BP
1925/* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
1926 * Netlink PID 'pid'. If 'cookie' is nonnull, adds a userdata attribute whose
1927 * contents contains 'cookie' and returns the offset within 'odp_actions' of
1928 * the start of the cookie. (If 'cookie' is null, then the return value is not
1929 * meaningful.) */
1930size_t
1673e0e4 1931odp_put_userspace_action(uint32_t pid, const union user_action_cookie *cookie,
39db78a0
BP
1932 struct ofpbuf *odp_actions)
1933{
1934 size_t offset;
1935
1936 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
1937 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
1938 if (cookie) {
1939 nl_msg_put_unspec(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
1940 cookie, sizeof *cookie);
1941 }
1942 nl_msg_end_nested(odp_actions, offset);
1943
1944 return cookie ? odp_actions->size - NLA_ALIGN(sizeof *cookie) : 0;
1945}
5bbda0aa
EJ
1946\f
1947/* The commit_odp_actions() function and its helpers. */
1948
1949static void
1950commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
1951 const void *key, size_t key_size)
1952{
1953 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
1954 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
1955 nl_msg_end_nested(odp_actions, offset);
1956}
1957
1958static void
1959commit_set_tun_id_action(const struct flow *flow, struct flow *base,
1960 struct ofpbuf *odp_actions)
1961{
296e07ac 1962 if (base->tunnel.tun_id == flow->tunnel.tun_id) {
5bbda0aa
EJ
1963 return;
1964 }
296e07ac 1965 base->tunnel.tun_id = flow->tunnel.tun_id;
5bbda0aa
EJ
1966
1967 commit_set_action(odp_actions, OVS_KEY_ATTR_TUN_ID,
296e07ac 1968 &base->tunnel.tun_id, sizeof(base->tunnel.tun_id));
5bbda0aa
EJ
1969}
1970
1971static void
1972commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
1973 struct ofpbuf *odp_actions)
1974{
1975 struct ovs_key_ethernet eth_key;
1976
1977 if (eth_addr_equals(base->dl_src, flow->dl_src) &&
1978 eth_addr_equals(base->dl_dst, flow->dl_dst)) {
1979 return;
1980 }
1981
1982 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
1983 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
1984
1985 memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
1986 memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
1987
1988 commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET,
1989 &eth_key, sizeof(eth_key));
1990}
1991
1992static void
1993commit_vlan_action(const struct flow *flow, struct flow *base,
1994 struct ofpbuf *odp_actions)
1995{
1996 if (base->vlan_tci == flow->vlan_tci) {
1997 return;
1998 }
1999
2000 if (base->vlan_tci & htons(VLAN_CFI)) {
2001 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
2002 }
2003
2004 if (flow->vlan_tci & htons(VLAN_CFI)) {
2005 struct ovs_action_push_vlan vlan;
2006
2007 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
2008 vlan.vlan_tci = flow->vlan_tci;
2009 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
2010 &vlan, sizeof vlan);
2011 }
2012 base->vlan_tci = flow->vlan_tci;
2013}
2014
2015static void
c4f2731d 2016commit_set_ipv4_action(const struct flow *flow, struct flow *base,
5bbda0aa
EJ
2017 struct ofpbuf *odp_actions)
2018{
2019 struct ovs_key_ipv4 ipv4_key;
2020
5bbda0aa
EJ
2021 if (base->nw_src == flow->nw_src &&
2022 base->nw_dst == flow->nw_dst &&
2023 base->nw_tos == flow->nw_tos &&
2024 base->nw_ttl == flow->nw_ttl &&
2025 base->nw_frag == flow->nw_frag) {
2026 return;
2027 }
2028
2029 ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
2030 ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
2031 ipv4_key.ipv4_tos = base->nw_tos = flow->nw_tos;
2032 ipv4_key.ipv4_ttl = base->nw_ttl = flow->nw_ttl;
2033 ipv4_key.ipv4_proto = base->nw_proto;
c4f2731d 2034 ipv4_key.ipv4_frag = ovs_to_odp_frag(base->nw_frag);
5bbda0aa
EJ
2035
2036 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4,
2037 &ipv4_key, sizeof(ipv4_key));
2038}
2039
c4f2731d
PS
2040static void
2041commit_set_ipv6_action(const struct flow *flow, struct flow *base,
2042 struct ofpbuf *odp_actions)
2043{
2044 struct ovs_key_ipv6 ipv6_key;
2045
2046 if (ipv6_addr_equals(&base->ipv6_src, &flow->ipv6_src) &&
2047 ipv6_addr_equals(&base->ipv6_dst, &flow->ipv6_dst) &&
2048 base->ipv6_label == flow->ipv6_label &&
2049 base->nw_tos == flow->nw_tos &&
2050 base->nw_ttl == flow->nw_ttl &&
2051 base->nw_frag == flow->nw_frag) {
2052 return;
2053 }
2054
2055 base->ipv6_src = flow->ipv6_src;
2056 memcpy(&ipv6_key.ipv6_src, &base->ipv6_src, sizeof(ipv6_key.ipv6_src));
2057 base->ipv6_dst = flow->ipv6_dst;
2058 memcpy(&ipv6_key.ipv6_dst, &base->ipv6_dst, sizeof(ipv6_key.ipv6_dst));
2059
2060 ipv6_key.ipv6_label = base->ipv6_label = flow->ipv6_label;
2061 ipv6_key.ipv6_tclass = base->nw_tos = flow->nw_tos;
2062 ipv6_key.ipv6_hlimit = base->nw_ttl = flow->nw_ttl;
2063 ipv6_key.ipv6_proto = base->nw_proto;
2064 ipv6_key.ipv6_frag = ovs_to_odp_frag(base->nw_frag);
2065
2066 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV6,
2067 &ipv6_key, sizeof(ipv6_key));
2068}
2069
2070static void
2071commit_set_nw_action(const struct flow *flow, struct flow *base,
2072 struct ofpbuf *odp_actions)
2073{
2074 /* Check if flow really have an IP header. */
2075 if (!flow->nw_proto) {
2076 return;
2077 }
2078
2079 if (base->dl_type == htons(ETH_TYPE_IP)) {
2080 commit_set_ipv4_action(flow, base, odp_actions);
2081 } else if (base->dl_type == htons(ETH_TYPE_IPV6)) {
2082 commit_set_ipv6_action(flow, base, odp_actions);
2083 }
2084}
2085
5bbda0aa
EJ
2086static void
2087commit_set_port_action(const struct flow *flow, struct flow *base,
2088 struct ofpbuf *odp_actions)
2089{
6e9bea4d 2090 if (!base->tp_src && !base->tp_dst) {
5bbda0aa
EJ
2091 return;
2092 }
2093
2094 if (base->tp_src == flow->tp_src &&
2095 base->tp_dst == flow->tp_dst) {
2096 return;
2097 }
2098
2099 if (flow->nw_proto == IPPROTO_TCP) {
2100 struct ovs_key_tcp port_key;
2101
2102 port_key.tcp_src = base->tp_src = flow->tp_src;
2103 port_key.tcp_dst = base->tp_dst = flow->tp_dst;
2104
2105 commit_set_action(odp_actions, OVS_KEY_ATTR_TCP,
2106 &port_key, sizeof(port_key));
2107
2108 } else if (flow->nw_proto == IPPROTO_UDP) {
2109 struct ovs_key_udp port_key;
2110
2111 port_key.udp_src = base->tp_src = flow->tp_src;
2112 port_key.udp_dst = base->tp_dst = flow->tp_dst;
2113
2114 commit_set_action(odp_actions, OVS_KEY_ATTR_UDP,
2115 &port_key, sizeof(port_key));
2116 }
2117}
2118
2119static void
2120commit_set_priority_action(const struct flow *flow, struct flow *base,
2121 struct ofpbuf *odp_actions)
2122{
deedf7e7 2123 if (base->skb_priority == flow->skb_priority) {
5bbda0aa
EJ
2124 return;
2125 }
deedf7e7 2126 base->skb_priority = flow->skb_priority;
5bbda0aa
EJ
2127
2128 commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY,
deedf7e7 2129 &base->skb_priority, sizeof(base->skb_priority));
5bbda0aa
EJ
2130}
2131
2132/* If any of the flow key data that ODP actions can modify are different in
2133 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
2134 * key from 'base' into 'flow', and then changes 'base' the same way. */
2135void
2136commit_odp_actions(const struct flow *flow, struct flow *base,
2137 struct ofpbuf *odp_actions)
2138{
2139 commit_set_tun_id_action(flow, base, odp_actions);
2140 commit_set_ether_addr_action(flow, base, odp_actions);
2141 commit_vlan_action(flow, base, odp_actions);
2142 commit_set_nw_action(flow, base, odp_actions);
2143 commit_set_port_action(flow, base, odp_actions);
2144 commit_set_priority_action(flow, base, odp_actions);
2145}