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