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