]>
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 | ||
e6cc0bab AZ |
51 | static int parse_odp_key_mask_attr(const char *, const struct simap *port_names, |
52 | struct ofpbuf *, struct ofpbuf *); | |
53 | static void format_odp_key_attr(const struct nlattr *a, | |
54 | const struct nlattr *ma, struct ds *ds); | |
a052b51b | 55 | |
98403001 BP |
56 | /* Returns one the following for the action with the given OVS_ACTION_ATTR_* |
57 | * 'type': | |
58 | * | |
59 | * - For an action whose argument has a fixed length, returned that | |
60 | * nonnegative length in bytes. | |
61 | * | |
62 | * - For an action with a variable-length argument, returns -2. | |
63 | * | |
64 | * - For an invalid 'type', returns -1. */ | |
4edb9ae9 | 65 | static int |
cdee00fd BP |
66 | odp_action_len(uint16_t type) |
67 | { | |
df2c07f4 | 68 | if (type > OVS_ACTION_ATTR_MAX) { |
cdee00fd BP |
69 | return -1; |
70 | } | |
71 | ||
09ded0ad | 72 | switch ((enum ovs_action_attr) type) { |
fea393b1 | 73 | case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t); |
98403001 | 74 | case OVS_ACTION_ATTR_USERSPACE: return -2; |
fea393b1 BP |
75 | case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan); |
76 | case OVS_ACTION_ATTR_POP_VLAN: return 0; | |
b02475c5 SH |
77 | case OVS_ACTION_ATTR_PUSH_MPLS: return sizeof(struct ovs_action_push_mpls); |
78 | case OVS_ACTION_ATTR_POP_MPLS: return sizeof(ovs_be16); | |
4edb9ae9 | 79 | case OVS_ACTION_ATTR_SET: return -2; |
98403001 | 80 | case OVS_ACTION_ATTR_SAMPLE: return -2; |
df2c07f4 JP |
81 | |
82 | case OVS_ACTION_ATTR_UNSPEC: | |
83 | case __OVS_ACTION_ATTR_MAX: | |
cdee00fd BP |
84 | return -1; |
85 | } | |
86 | ||
87 | return -1; | |
88 | } | |
89 | ||
e6603631 BP |
90 | /* Returns a string form of 'attr'. The return value is either a statically |
91 | * allocated constant string or the 'bufsize'-byte buffer 'namebuf'. 'bufsize' | |
92 | * should be at least OVS_KEY_ATTR_BUFSIZE. */ | |
93 | enum { OVS_KEY_ATTR_BUFSIZE = 3 + INT_STRLEN(unsigned int) + 1 }; | |
744791b5 | 94 | static const char * |
e6603631 | 95 | ovs_key_attr_to_string(enum ovs_key_attr attr, char *namebuf, size_t bufsize) |
744791b5 | 96 | { |
744791b5 BP |
97 | switch (attr) { |
98 | case OVS_KEY_ATTR_UNSPEC: return "unspec"; | |
fea393b1 | 99 | case OVS_KEY_ATTR_ENCAP: return "encap"; |
1b567fb9 | 100 | case OVS_KEY_ATTR_PRIORITY: return "skb_priority"; |
72e8bf28 | 101 | case OVS_KEY_ATTR_SKB_MARK: return "skb_mark"; |
9b405f1a | 102 | case OVS_KEY_ATTR_TUNNEL: return "tunnel"; |
744791b5 BP |
103 | case OVS_KEY_ATTR_IN_PORT: return "in_port"; |
104 | case OVS_KEY_ATTR_ETHERNET: return "eth"; | |
fea393b1 | 105 | case OVS_KEY_ATTR_VLAN: return "vlan"; |
744791b5 BP |
106 | case OVS_KEY_ATTR_ETHERTYPE: return "eth_type"; |
107 | case OVS_KEY_ATTR_IPV4: return "ipv4"; | |
108 | case OVS_KEY_ATTR_IPV6: return "ipv6"; | |
109 | case OVS_KEY_ATTR_TCP: return "tcp"; | |
110 | case OVS_KEY_ATTR_UDP: return "udp"; | |
111 | case OVS_KEY_ATTR_ICMP: return "icmp"; | |
112 | case OVS_KEY_ATTR_ICMPV6: return "icmpv6"; | |
113 | case OVS_KEY_ATTR_ARP: return "arp"; | |
114 | case OVS_KEY_ATTR_ND: return "nd"; | |
b02475c5 | 115 | case OVS_KEY_ATTR_MPLS: return "mpls"; |
744791b5 BP |
116 | |
117 | case __OVS_KEY_ATTR_MAX: | |
118 | default: | |
e6603631 BP |
119 | snprintf(namebuf, bufsize, "key%u", (unsigned int) attr); |
120 | return namebuf; | |
744791b5 BP |
121 | } |
122 | } | |
123 | ||
cdee00fd BP |
124 | static void |
125 | format_generic_odp_action(struct ds *ds, const struct nlattr *a) | |
126 | { | |
8ba43fbd BP |
127 | size_t len = nl_attr_get_size(a); |
128 | ||
cdee00fd | 129 | ds_put_format(ds, "action%"PRId16, nl_attr_type(a)); |
8ba43fbd | 130 | if (len) { |
cdee00fd BP |
131 | const uint8_t *unspec; |
132 | unsigned int i; | |
133 | ||
134 | unspec = nl_attr_get(a); | |
8ba43fbd | 135 | for (i = 0; i < len; i++) { |
cdee00fd BP |
136 | ds_put_char(ds, i ? ' ': '('); |
137 | ds_put_format(ds, "%02x", unspec[i]); | |
138 | } | |
139 | ds_put_char(ds, ')'); | |
140 | } | |
141 | } | |
142 | ||
6ff686f2 PS |
143 | static void |
144 | format_odp_sample_action(struct ds *ds, const struct nlattr *attr) | |
145 | { | |
146 | static const struct nl_policy ovs_sample_policy[] = { | |
3815d6c2 LS |
147 | { NL_A_NO_ATTR, 0, 0, false }, /* OVS_SAMPLE_ATTR_UNSPEC */ |
148 | { NL_A_U32, 0, 0, false }, /* OVS_SAMPLE_ATTR_PROBABILITY */ | |
149 | { NL_A_NESTED, 0, 0, false }, /* OVS_SAMPLE_ATTR_ACTIONS */ | |
6ff686f2 PS |
150 | }; |
151 | struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)]; | |
6ff686f2 PS |
152 | double percentage; |
153 | const struct nlattr *nla_acts; | |
154 | int len; | |
155 | ||
156 | ds_put_cstr(ds, "sample"); | |
157 | ||
5621afff | 158 | if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) { |
6ff686f2 PS |
159 | ds_put_cstr(ds, "(error)"); |
160 | return; | |
161 | } | |
162 | ||
163 | percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) / | |
164 | UINT32_MAX; | |
165 | ||
166 | ds_put_format(ds, "(sample=%.1f%%,", percentage); | |
167 | ||
168 | ds_put_cstr(ds, "actions("); | |
169 | nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]); | |
170 | len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]); | |
171 | format_odp_actions(ds, nla_acts, len); | |
172 | ds_put_format(ds, "))"); | |
173 | } | |
174 | ||
6a7e895f | 175 | static const char * |
98f0520f | 176 | slow_path_reason_to_string(enum slow_path_reason reason) |
6a7e895f | 177 | { |
98f0520f | 178 | switch (reason) { |
6a7e895f BP |
179 | case SLOW_CFM: |
180 | return "cfm"; | |
181 | case SLOW_LACP: | |
182 | return "lacp"; | |
183 | case SLOW_STP: | |
184 | return "stp"; | |
ccc09689 EJ |
185 | case SLOW_BFD: |
186 | return "bfd"; | |
6a7e895f BP |
187 | case SLOW_CONTROLLER: |
188 | return "controller"; | |
98f0520f | 189 | case __SLOW_MAX: |
6a7e895f BP |
190 | default: |
191 | return NULL; | |
192 | } | |
193 | } | |
194 | ||
98f0520f EJ |
195 | static enum slow_path_reason |
196 | string_to_slow_path_reason(const char *string) | |
197 | { | |
198 | enum slow_path_reason i; | |
199 | ||
200 | for (i = 1; i < __SLOW_MAX; i++) { | |
201 | if (!strcmp(string, slow_path_reason_to_string(i))) { | |
202 | return i; | |
203 | } | |
204 | } | |
205 | ||
206 | return 0; | |
207 | } | |
208 | ||
ec956fc7 PS |
209 | static int |
210 | parse_flags(const char *s, const char *(*bit_to_string)(uint32_t), | |
211 | uint32_t *res) | |
212 | { | |
213 | uint32_t result = 0; | |
214 | int n = 0; | |
215 | ||
216 | if (s[n] != '(') { | |
217 | return -EINVAL; | |
218 | } | |
219 | n++; | |
220 | ||
221 | while (s[n] != ')') { | |
222 | unsigned long long int flags; | |
223 | uint32_t bit; | |
224 | int n0; | |
225 | ||
226 | if (sscanf(&s[n], "%lli%n", &flags, &n0) > 0 && n0 > 0) { | |
227 | n += n0 + (s[n + n0] == ','); | |
228 | result |= flags; | |
229 | continue; | |
230 | } | |
231 | ||
232 | for (bit = 1; bit; bit <<= 1) { | |
233 | const char *name = bit_to_string(bit); | |
234 | size_t len; | |
235 | ||
236 | if (!name) { | |
237 | continue; | |
238 | } | |
239 | ||
240 | len = strlen(name); | |
241 | if (!strncmp(s + n, name, len) && | |
242 | (s[n + len] == ',' || s[n + len] == ')')) { | |
243 | result |= bit; | |
244 | n += len + (s[n + len] == ','); | |
245 | break; | |
246 | } | |
247 | } | |
248 | ||
249 | if (!bit) { | |
250 | return -EINVAL; | |
251 | } | |
252 | } | |
253 | n++; | |
254 | ||
255 | *res = result; | |
256 | return n; | |
6a7e895f BP |
257 | } |
258 | ||
98403001 BP |
259 | static void |
260 | format_odp_userspace_action(struct ds *ds, const struct nlattr *attr) | |
261 | { | |
262 | static const struct nl_policy ovs_userspace_policy[] = { | |
3815d6c2 LS |
263 | { NL_A_NO_ATTR, 0, 0, false }, /* OVS_USERSPACE_ATTR_UNSPEC */ |
264 | { NL_A_U32, 0, 0, false }, /* OVS_USERSPACE_ATTR_PID */ | |
265 | { NL_A_UNSPEC, 0, 0, true }, /* OVS_USERSPACE_ATTR_USERDATA */ | |
98403001 BP |
266 | }; |
267 | struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)]; | |
e995e3df | 268 | const struct nlattr *userdata_attr; |
98403001 BP |
269 | |
270 | if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) { | |
271 | ds_put_cstr(ds, "userspace(error)"); | |
272 | return; | |
273 | } | |
274 | ||
275 | ds_put_format(ds, "userspace(pid=%"PRIu32, | |
276 | nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID])); | |
277 | ||
e995e3df | 278 | userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA]; |
29089a54 RL |
279 | |
280 | if (userdata_attr) { | |
281 | const uint8_t *userdata = nl_attr_get(userdata_attr); | |
282 | size_t userdata_len = nl_attr_get_size(userdata_attr); | |
283 | bool userdata_unspec = true; | |
1673e0e4 | 284 | union user_action_cookie cookie; |
98403001 | 285 | |
29089a54 RL |
286 | if (userdata_len >= sizeof cookie.type |
287 | && userdata_len <= sizeof cookie) { | |
288 | ||
289 | memset(&cookie, 0, sizeof cookie); | |
290 | memcpy(&cookie, userdata, userdata_len); | |
291 | ||
292 | userdata_unspec = false; | |
293 | ||
294 | if (userdata_len == sizeof cookie.sflow | |
295 | && cookie.type == USER_ACTION_COOKIE_SFLOW) { | |
296 | ds_put_format(ds, ",sFlow(" | |
297 | "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")", | |
298 | vlan_tci_to_vid(cookie.sflow.vlan_tci), | |
299 | vlan_tci_to_pcp(cookie.sflow.vlan_tci), | |
300 | cookie.sflow.output); | |
301 | } else if (userdata_len == sizeof cookie.slow_path | |
302 | && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) { | |
98f0520f EJ |
303 | const char *reason; |
304 | reason = slow_path_reason_to_string(cookie.slow_path.reason); | |
305 | reason = reason ? reason : ""; | |
306 | ds_put_format(ds, ",slow_path(%s)", reason); | |
29089a54 RL |
307 | } else if (userdata_len == sizeof cookie.flow_sample |
308 | && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) { | |
309 | ds_put_format(ds, ",flow_sample(probability=%"PRIu16 | |
310 | ",collector_set_id=%"PRIu32 | |
311 | ",obs_domain_id=%"PRIu32 | |
312 | ",obs_point_id=%"PRIu32")", | |
313 | cookie.flow_sample.probability, | |
314 | cookie.flow_sample.collector_set_id, | |
315 | cookie.flow_sample.obs_domain_id, | |
316 | cookie.flow_sample.obs_point_id); | |
317 | } else if (userdata_len == sizeof cookie.ipfix | |
318 | && cookie.type == USER_ACTION_COOKIE_IPFIX) { | |
319 | ds_put_format(ds, ",ipfix"); | |
320 | } else { | |
321 | userdata_unspec = true; | |
322 | } | |
98403001 | 323 | } |
e995e3df | 324 | |
29089a54 RL |
325 | if (userdata_unspec) { |
326 | size_t i; | |
327 | ds_put_format(ds, ",userdata("); | |
328 | for (i = 0; i < userdata_len; i++) { | |
329 | ds_put_format(ds, "%02x", userdata[i]); | |
330 | } | |
331 | ds_put_char(ds, ')'); | |
e995e3df | 332 | } |
98403001 BP |
333 | } |
334 | ||
335 | ds_put_char(ds, ')'); | |
336 | } | |
337 | ||
8ddc056d BP |
338 | static void |
339 | format_vlan_tci(struct ds *ds, ovs_be16 vlan_tci) | |
340 | { | |
341 | ds_put_format(ds, "vid=%"PRIu16",pcp=%d", | |
342 | vlan_tci_to_vid(vlan_tci), | |
343 | vlan_tci_to_pcp(vlan_tci)); | |
344 | if (!(vlan_tci & htons(VLAN_CFI))) { | |
345 | ds_put_cstr(ds, ",cfi=0"); | |
346 | } | |
347 | } | |
348 | ||
b02475c5 SH |
349 | static void |
350 | format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse) | |
351 | { | |
352 | ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d", | |
353 | mpls_lse_to_label(mpls_lse), | |
354 | mpls_lse_to_tc(mpls_lse), | |
355 | mpls_lse_to_ttl(mpls_lse), | |
356 | mpls_lse_to_bos(mpls_lse)); | |
357 | } | |
358 | ||
e6cc0bab AZ |
359 | static void |
360 | format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key, | |
361 | const struct ovs_key_mpls *mpls_mask) | |
362 | { | |
363 | ovs_be32 key = mpls_key->mpls_lse; | |
364 | ||
365 | if (mpls_mask == NULL) { | |
366 | format_mpls_lse(ds, key); | |
367 | } else { | |
368 | ovs_be32 mask = mpls_mask->mpls_lse; | |
369 | ||
370 | ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x", | |
371 | mpls_lse_to_label(key), mpls_lse_to_label(mask), | |
372 | mpls_lse_to_tc(key), mpls_lse_to_tc(mask), | |
373 | mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask), | |
374 | mpls_lse_to_bos(key), mpls_lse_to_bos(mask)); | |
375 | } | |
376 | } | |
377 | ||
4edb9ae9 | 378 | static void |
cdee00fd | 379 | format_odp_action(struct ds *ds, const struct nlattr *a) |
064af421 | 380 | { |
98403001 | 381 | int expected_len; |
4edb9ae9 | 382 | enum ovs_action_attr type = nl_attr_type(a); |
fea393b1 | 383 | const struct ovs_action_push_vlan *vlan; |
cdee00fd | 384 | |
98403001 BP |
385 | expected_len = odp_action_len(nl_attr_type(a)); |
386 | if (expected_len != -2 && nl_attr_get_size(a) != expected_len) { | |
8ba43fbd | 387 | ds_put_format(ds, "bad length %zu, expected %d for: ", |
98403001 | 388 | nl_attr_get_size(a), expected_len); |
cdee00fd BP |
389 | format_generic_odp_action(ds, a); |
390 | return; | |
391 | } | |
392 | ||
4edb9ae9 | 393 | switch (type) { |
df2c07f4 | 394 | case OVS_ACTION_ATTR_OUTPUT: |
26ce7705 | 395 | ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a)); |
064af421 | 396 | break; |
df2c07f4 | 397 | case OVS_ACTION_ATTR_USERSPACE: |
98403001 | 398 | format_odp_userspace_action(ds, a); |
064af421 | 399 | break; |
4edb9ae9 PS |
400 | case OVS_ACTION_ATTR_SET: |
401 | ds_put_cstr(ds, "set("); | |
e6cc0bab | 402 | format_odp_key_attr(nl_attr_get(a), NULL, ds); |
4edb9ae9 | 403 | ds_put_cstr(ds, ")"); |
064af421 | 404 | break; |
fea393b1 BP |
405 | case OVS_ACTION_ATTR_PUSH_VLAN: |
406 | vlan = nl_attr_get(a); | |
407 | ds_put_cstr(ds, "push_vlan("); | |
408 | if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) { | |
409 | ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid)); | |
410 | } | |
8ddc056d BP |
411 | format_vlan_tci(ds, vlan->vlan_tci); |
412 | ds_put_char(ds, ')'); | |
064af421 | 413 | break; |
fea393b1 BP |
414 | case OVS_ACTION_ATTR_POP_VLAN: |
415 | ds_put_cstr(ds, "pop_vlan"); | |
064af421 | 416 | break; |
b02475c5 SH |
417 | case OVS_ACTION_ATTR_PUSH_MPLS: { |
418 | const struct ovs_action_push_mpls *mpls = nl_attr_get(a); | |
419 | ds_put_cstr(ds, "push_mpls("); | |
420 | format_mpls_lse(ds, mpls->mpls_lse); | |
421 | ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype)); | |
422 | break; | |
423 | } | |
424 | case OVS_ACTION_ATTR_POP_MPLS: { | |
425 | ovs_be16 ethertype = nl_attr_get_be16(a); | |
426 | ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype)); | |
427 | break; | |
428 | } | |
6ff686f2 PS |
429 | case OVS_ACTION_ATTR_SAMPLE: |
430 | format_odp_sample_action(ds, a); | |
431 | break; | |
4edb9ae9 PS |
432 | case OVS_ACTION_ATTR_UNSPEC: |
433 | case __OVS_ACTION_ATTR_MAX: | |
064af421 | 434 | default: |
cdee00fd | 435 | format_generic_odp_action(ds, a); |
064af421 BP |
436 | break; |
437 | } | |
438 | } | |
439 | ||
440 | void | |
cdee00fd | 441 | format_odp_actions(struct ds *ds, const struct nlattr *actions, |
cf22f8cb | 442 | size_t actions_len) |
064af421 | 443 | { |
cdee00fd BP |
444 | if (actions_len) { |
445 | const struct nlattr *a; | |
446 | unsigned int left; | |
447 | ||
448 | NL_ATTR_FOR_EACH (a, left, actions, actions_len) { | |
449 | if (a != actions) { | |
450 | ds_put_char(ds, ','); | |
451 | } | |
452 | format_odp_action(ds, a); | |
064af421 | 453 | } |
cdee00fd | 454 | if (left) { |
3a8cfc50 BP |
455 | int i; |
456 | ||
3476fce3 BP |
457 | if (left == actions_len) { |
458 | ds_put_cstr(ds, "<empty>"); | |
459 | } | |
3a8cfc50 BP |
460 | ds_put_format(ds, ",***%u leftover bytes*** (", left); |
461 | for (i = 0; i < left; i++) { | |
462 | ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]); | |
463 | } | |
464 | ds_put_char(ds, ')'); | |
cdee00fd BP |
465 | } |
466 | } else { | |
064af421 BP |
467 | ds_put_cstr(ds, "drop"); |
468 | } | |
469 | } | |
7202cbe5 BP |
470 | |
471 | static int | |
44bac24b | 472 | parse_odp_action(const char *s, const struct simap *port_names, |
7202cbe5 BP |
473 | struct ofpbuf *actions) |
474 | { | |
475 | /* Many of the sscanf calls in this function use oversized destination | |
476 | * fields because some sscanf() implementations truncate the range of %i | |
477 | * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a | |
478 | * value of 0x7fff. The other alternatives are to allow only a single | |
479 | * radix (e.g. decimal or hexadecimal) or to write more sophisticated | |
480 | * parsers. | |
481 | * | |
482 | * The tun_id parser has to use an alternative approach because there is no | |
483 | * type larger than 64 bits. */ | |
484 | ||
485 | { | |
486 | unsigned long long int port; | |
487 | int n = -1; | |
488 | ||
489 | if (sscanf(s, "%lli%n", &port, &n) > 0 && n > 0) { | |
490 | nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port); | |
491 | return n; | |
492 | } | |
493 | } | |
494 | ||
495 | if (port_names) { | |
496 | int len = strcspn(s, delimiters); | |
44bac24b | 497 | struct simap_node *node; |
7202cbe5 | 498 | |
44bac24b | 499 | node = simap_find_len(port_names, s, len); |
7202cbe5 | 500 | if (node) { |
44bac24b | 501 | nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data); |
7202cbe5 BP |
502 | return len; |
503 | } | |
504 | } | |
505 | ||
506 | { | |
507 | unsigned long long int pid; | |
36fc5f18 | 508 | unsigned long long int output; |
29089a54 RL |
509 | unsigned long long int probability; |
510 | unsigned long long int collector_set_id; | |
511 | unsigned long long int obs_domain_id; | |
512 | unsigned long long int obs_point_id; | |
7202cbe5 BP |
513 | int vid, pcp; |
514 | int n = -1; | |
515 | ||
516 | if (sscanf(s, "userspace(pid=%lli)%n", &pid, &n) > 0 && n > 0) { | |
e995e3df | 517 | odp_put_userspace_action(pid, NULL, 0, actions); |
7202cbe5 | 518 | return n; |
6a7e895f BP |
519 | } else if (sscanf(s, "userspace(pid=%lli,sFlow(vid=%i," |
520 | "pcp=%i,output=%lli))%n", | |
36fc5f18 | 521 | &pid, &vid, &pcp, &output, &n) > 0 && n > 0) { |
1673e0e4 | 522 | union user_action_cookie cookie; |
7202cbe5 BP |
523 | uint16_t tci; |
524 | ||
525 | tci = vid | (pcp << VLAN_PCP_SHIFT); | |
526 | if (tci) { | |
527 | tci |= VLAN_CFI; | |
528 | } | |
529 | ||
530 | cookie.type = USER_ACTION_COOKIE_SFLOW; | |
1673e0e4 BP |
531 | cookie.sflow.vlan_tci = htons(tci); |
532 | cookie.sflow.output = output; | |
29089a54 RL |
533 | odp_put_userspace_action(pid, &cookie, sizeof cookie.sflow, |
534 | actions); | |
6a7e895f | 535 | return n; |
98f0520f | 536 | } else if (sscanf(s, "userspace(pid=%lli,slow_path(%n", &pid, &n) > 0 |
6a7e895f BP |
537 | && n > 0) { |
538 | union user_action_cookie cookie; | |
98f0520f EJ |
539 | char reason[32]; |
540 | ||
541 | if (s[n] == ')' && s[n + 1] == ')') { | |
542 | reason[0] = '\0'; | |
543 | n += 2; | |
544 | } else if (sscanf(s + n, "%31[^)]))", reason) > 0) { | |
545 | n += strlen(reason) + 2; | |
546 | } else { | |
547 | return -EINVAL; | |
548 | } | |
6a7e895f BP |
549 | |
550 | cookie.type = USER_ACTION_COOKIE_SLOW_PATH; | |
551 | cookie.slow_path.unused = 0; | |
98f0520f | 552 | cookie.slow_path.reason = string_to_slow_path_reason(reason); |
6a7e895f | 553 | |
98f0520f | 554 | if (reason[0] && !cookie.slow_path.reason) { |
6a7e895f BP |
555 | return -EINVAL; |
556 | } | |
6a7e895f | 557 | |
29089a54 RL |
558 | odp_put_userspace_action(pid, &cookie, sizeof cookie.slow_path, |
559 | actions); | |
560 | return n; | |
561 | } else if (sscanf(s, "userspace(pid=%lli,flow_sample(probability=%lli," | |
562 | "collector_set_id=%lli,obs_domain_id=%lli," | |
563 | "obs_point_id=%lli))%n", | |
564 | &pid, &probability, &collector_set_id, | |
565 | &obs_domain_id, &obs_point_id, &n) > 0 && n > 0) { | |
566 | union user_action_cookie cookie; | |
567 | ||
568 | cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE; | |
569 | cookie.flow_sample.probability = probability; | |
570 | cookie.flow_sample.collector_set_id = collector_set_id; | |
571 | cookie.flow_sample.obs_domain_id = obs_domain_id; | |
572 | cookie.flow_sample.obs_point_id = obs_point_id; | |
573 | odp_put_userspace_action(pid, &cookie, sizeof cookie.flow_sample, | |
574 | actions); | |
7202cbe5 | 575 | return n; |
29089a54 RL |
576 | } else if (sscanf(s, "userspace(pid=%lli,ipfix)%n", &pid, &n) > 0 |
577 | && n > 0) { | |
578 | union user_action_cookie cookie; | |
7202cbe5 | 579 | |
29089a54 RL |
580 | cookie.type = USER_ACTION_COOKIE_IPFIX; |
581 | odp_put_userspace_action(pid, &cookie, sizeof cookie.ipfix, | |
e995e3df | 582 | actions); |
7202cbe5 | 583 | return n; |
e995e3df BP |
584 | } else if (sscanf(s, "userspace(pid=%lli,userdata(%n", &pid, &n) > 0 |
585 | && n > 0) { | |
586 | struct ofpbuf buf; | |
587 | char *end; | |
588 | ||
589 | ofpbuf_init(&buf, 16); | |
590 | end = ofpbuf_put_hex(&buf, &s[n], NULL); | |
591 | if (end[0] == ')' && end[1] == ')') { | |
592 | odp_put_userspace_action(pid, buf.data, buf.size, actions); | |
593 | ofpbuf_uninit(&buf); | |
594 | return (end + 2) - s; | |
595 | } | |
7202cbe5 BP |
596 | } |
597 | } | |
598 | ||
599 | if (!strncmp(s, "set(", 4)) { | |
600 | size_t start_ofs; | |
601 | int retval; | |
602 | ||
603 | start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET); | |
e6cc0bab | 604 | retval = parse_odp_key_mask_attr(s + 4, port_names, actions, NULL); |
7202cbe5 BP |
605 | if (retval < 0) { |
606 | return retval; | |
607 | } | |
608 | if (s[retval + 4] != ')') { | |
609 | return -EINVAL; | |
610 | } | |
611 | nl_msg_end_nested(actions, start_ofs); | |
612 | return retval + 5; | |
613 | } | |
614 | ||
becffb86 BP |
615 | { |
616 | struct ovs_action_push_vlan push; | |
617 | int tpid = ETH_TYPE_VLAN; | |
618 | int vid, pcp; | |
619 | int cfi = 1; | |
620 | int n = -1; | |
7202cbe5 | 621 | |
becffb86 BP |
622 | if ((sscanf(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n) > 0 |
623 | && n > 0) | |
624 | || (sscanf(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n", | |
625 | &vid, &pcp, &cfi, &n) > 0 && n > 0) | |
626 | || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n", | |
627 | &tpid, &vid, &pcp, &n) > 0 && n > 0) | |
628 | || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n", | |
629 | &tpid, &vid, &pcp, &cfi, &n) > 0 && n > 0)) { | |
630 | push.vlan_tpid = htons(tpid); | |
631 | push.vlan_tci = htons((vid << VLAN_VID_SHIFT) | |
632 | | (pcp << VLAN_PCP_SHIFT) | |
633 | | (cfi ? VLAN_CFI : 0)); | |
634 | nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN, | |
635 | &push, sizeof push); | |
636 | ||
637 | return n; | |
7202cbe5 | 638 | } |
7202cbe5 BP |
639 | } |
640 | ||
becffb86 BP |
641 | if (!strncmp(s, "pop_vlan", 8)) { |
642 | nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN); | |
643 | return 8; | |
7202cbe5 BP |
644 | } |
645 | ||
646 | { | |
647 | double percentage; | |
648 | int n = -1; | |
649 | ||
650 | if (sscanf(s, "sample(sample=%lf%%,actions(%n", &percentage, &n) > 0 | |
651 | && percentage >= 0. && percentage <= 100.0 | |
652 | && n > 0) { | |
653 | size_t sample_ofs, actions_ofs; | |
654 | double probability; | |
655 | ||
656 | probability = floor(UINT32_MAX * (percentage / 100.0) + .5); | |
657 | sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE); | |
658 | nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY, | |
659 | (probability <= 0 ? 0 | |
660 | : probability >= UINT32_MAX ? UINT32_MAX | |
661 | : probability)); | |
662 | ||
663 | actions_ofs = nl_msg_start_nested(actions, | |
664 | OVS_SAMPLE_ATTR_ACTIONS); | |
665 | for (;;) { | |
666 | int retval; | |
667 | ||
79d4ffe2 | 668 | n += strspn(s + n, delimiters); |
7202cbe5 BP |
669 | if (s[n] == ')') { |
670 | break; | |
671 | } | |
672 | ||
673 | retval = parse_odp_action(s + n, port_names, actions); | |
674 | if (retval < 0) { | |
675 | return retval; | |
676 | } | |
677 | n += retval; | |
7202cbe5 BP |
678 | } |
679 | nl_msg_end_nested(actions, actions_ofs); | |
680 | nl_msg_end_nested(actions, sample_ofs); | |
681 | ||
682 | return s[n + 1] == ')' ? n + 2 : -EINVAL; | |
683 | } | |
684 | } | |
685 | ||
686 | return -EINVAL; | |
687 | } | |
688 | ||
689 | /* Parses the string representation of datapath actions, in the format output | |
690 | * by format_odp_action(). Returns 0 if successful, otherwise a positive errno | |
691 | * value. On success, the ODP actions are appended to 'actions' as a series of | |
692 | * Netlink attributes. On failure, no data is appended to 'actions'. Either | |
693 | * way, 'actions''s data might be reallocated. */ | |
694 | int | |
44bac24b | 695 | odp_actions_from_string(const char *s, const struct simap *port_names, |
7202cbe5 BP |
696 | struct ofpbuf *actions) |
697 | { | |
698 | size_t old_size; | |
699 | ||
700 | if (!strcasecmp(s, "drop")) { | |
701 | return 0; | |
702 | } | |
703 | ||
704 | old_size = actions->size; | |
705 | for (;;) { | |
706 | int retval; | |
707 | ||
708 | s += strspn(s, delimiters); | |
709 | if (!*s) { | |
710 | return 0; | |
711 | } | |
712 | ||
713 | retval = parse_odp_action(s, port_names, actions); | |
714 | if (retval < 0 || !strchr(delimiters, s[retval])) { | |
715 | actions->size = old_size; | |
716 | return -retval; | |
717 | } | |
718 | s += retval; | |
719 | } | |
720 | ||
721 | return 0; | |
722 | } | |
14608a15 | 723 | \f |
36956a7d | 724 | /* Returns the correct length of the payload for a flow key attribute of the |
fea393b1 BP |
725 | * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload |
726 | * is variable length. */ | |
36956a7d BP |
727 | static int |
728 | odp_flow_key_attr_len(uint16_t type) | |
729 | { | |
df2c07f4 | 730 | if (type > OVS_KEY_ATTR_MAX) { |
36956a7d BP |
731 | return -1; |
732 | } | |
733 | ||
09ded0ad | 734 | switch ((enum ovs_key_attr) type) { |
fea393b1 | 735 | case OVS_KEY_ATTR_ENCAP: return -2; |
abff858b | 736 | case OVS_KEY_ATTR_PRIORITY: return 4; |
72e8bf28 | 737 | case OVS_KEY_ATTR_SKB_MARK: return 4; |
9b405f1a | 738 | case OVS_KEY_ATTR_TUNNEL: return -2; |
df2c07f4 JP |
739 | case OVS_KEY_ATTR_IN_PORT: return 4; |
740 | case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet); | |
fea393b1 | 741 | case OVS_KEY_ATTR_VLAN: return sizeof(ovs_be16); |
df2c07f4 | 742 | case OVS_KEY_ATTR_ETHERTYPE: return 2; |
b02475c5 | 743 | case OVS_KEY_ATTR_MPLS: return sizeof(struct ovs_key_mpls); |
df2c07f4 JP |
744 | case OVS_KEY_ATTR_IPV4: return sizeof(struct ovs_key_ipv4); |
745 | case OVS_KEY_ATTR_IPV6: return sizeof(struct ovs_key_ipv6); | |
746 | case OVS_KEY_ATTR_TCP: return sizeof(struct ovs_key_tcp); | |
747 | case OVS_KEY_ATTR_UDP: return sizeof(struct ovs_key_udp); | |
748 | case OVS_KEY_ATTR_ICMP: return sizeof(struct ovs_key_icmp); | |
749 | case OVS_KEY_ATTR_ICMPV6: return sizeof(struct ovs_key_icmpv6); | |
750 | case OVS_KEY_ATTR_ARP: return sizeof(struct ovs_key_arp); | |
751 | case OVS_KEY_ATTR_ND: return sizeof(struct ovs_key_nd); | |
752 | ||
753 | case OVS_KEY_ATTR_UNSPEC: | |
754 | case __OVS_KEY_ATTR_MAX: | |
36956a7d BP |
755 | return -1; |
756 | } | |
757 | ||
758 | return -1; | |
759 | } | |
760 | ||
36956a7d BP |
761 | static void |
762 | format_generic_odp_key(const struct nlattr *a, struct ds *ds) | |
763 | { | |
764 | size_t len = nl_attr_get_size(a); | |
36956a7d BP |
765 | if (len) { |
766 | const uint8_t *unspec; | |
767 | unsigned int i; | |
768 | ||
769 | unspec = nl_attr_get(a); | |
770 | for (i = 0; i < len; i++) { | |
e6cc0bab AZ |
771 | if (i) { |
772 | ds_put_char(ds, ' '); | |
773 | } | |
36956a7d BP |
774 | ds_put_format(ds, "%02x", unspec[i]); |
775 | } | |
36956a7d BP |
776 | } |
777 | } | |
778 | ||
7257b535 BP |
779 | static const char * |
780 | ovs_frag_type_to_string(enum ovs_frag_type type) | |
781 | { | |
782 | switch (type) { | |
783 | case OVS_FRAG_TYPE_NONE: | |
784 | return "no"; | |
785 | case OVS_FRAG_TYPE_FIRST: | |
786 | return "first"; | |
787 | case OVS_FRAG_TYPE_LATER: | |
788 | return "later"; | |
789 | case __OVS_FRAG_TYPE_MAX: | |
790 | default: | |
791 | return "<error>"; | |
792 | } | |
793 | } | |
794 | ||
9b405f1a PS |
795 | static int |
796 | tunnel_key_attr_len(int type) | |
29c7a2b2 | 797 | { |
9b405f1a PS |
798 | switch (type) { |
799 | case OVS_TUNNEL_KEY_ATTR_ID: return 8; | |
800 | case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: return 4; | |
801 | case OVS_TUNNEL_KEY_ATTR_IPV4_DST: return 4; | |
802 | case OVS_TUNNEL_KEY_ATTR_TOS: return 1; | |
803 | case OVS_TUNNEL_KEY_ATTR_TTL: return 1; | |
804 | case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: return 0; | |
805 | case OVS_TUNNEL_KEY_ATTR_CSUM: return 0; | |
806 | case __OVS_TUNNEL_KEY_ATTR_MAX: | |
807 | return -1; | |
29c7a2b2 | 808 | } |
9b405f1a PS |
809 | return -1; |
810 | } | |
811 | ||
617e10e7 SH |
812 | enum odp_key_fitness |
813 | odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun) | |
9b405f1a PS |
814 | { |
815 | unsigned int left; | |
816 | const struct nlattr *a; | |
817 | bool ttl = false; | |
818 | bool unknown = false; | |
819 | ||
820 | NL_NESTED_FOR_EACH(a, left, attr) { | |
821 | uint16_t type = nl_attr_type(a); | |
822 | size_t len = nl_attr_get_size(a); | |
823 | int expected_len = tunnel_key_attr_len(type); | |
824 | ||
825 | if (len != expected_len && expected_len >= 0) { | |
826 | return ODP_FIT_ERROR; | |
827 | } | |
828 | ||
829 | switch (type) { | |
830 | case OVS_TUNNEL_KEY_ATTR_ID: | |
831 | tun->tun_id = nl_attr_get_be64(a); | |
832 | tun->flags |= FLOW_TNL_F_KEY; | |
833 | break; | |
834 | case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: | |
835 | tun->ip_src = nl_attr_get_be32(a); | |
836 | break; | |
837 | case OVS_TUNNEL_KEY_ATTR_IPV4_DST: | |
838 | tun->ip_dst = nl_attr_get_be32(a); | |
839 | break; | |
840 | case OVS_TUNNEL_KEY_ATTR_TOS: | |
841 | tun->ip_tos = nl_attr_get_u8(a); | |
842 | break; | |
843 | case OVS_TUNNEL_KEY_ATTR_TTL: | |
844 | tun->ip_ttl = nl_attr_get_u8(a); | |
845 | ttl = true; | |
846 | break; | |
847 | case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: | |
848 | tun->flags |= FLOW_TNL_F_DONT_FRAGMENT; | |
849 | break; | |
850 | case OVS_TUNNEL_KEY_ATTR_CSUM: | |
851 | tun->flags |= FLOW_TNL_F_CSUM; | |
852 | break; | |
853 | default: | |
854 | /* Allow this to show up as unexpected, if there are unknown | |
855 | * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */ | |
856 | unknown = true; | |
857 | break; | |
858 | } | |
859 | } | |
860 | ||
861 | if (!ttl) { | |
862 | return ODP_FIT_ERROR; | |
863 | } | |
864 | if (unknown) { | |
865 | return ODP_FIT_TOO_MUCH; | |
866 | } | |
867 | return ODP_FIT_PERFECT; | |
868 | } | |
869 | ||
870 | static void | |
871 | tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key) | |
872 | { | |
873 | size_t tun_key_ofs; | |
874 | ||
875 | tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL); | |
876 | ||
877 | if (tun_key->flags & FLOW_TNL_F_KEY) { | |
878 | nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id); | |
879 | } | |
880 | if (tun_key->ip_src) { | |
881 | nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src); | |
882 | } | |
883 | if (tun_key->ip_dst) { | |
884 | nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst); | |
885 | } | |
886 | if (tun_key->ip_tos) { | |
887 | nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos); | |
888 | } | |
889 | nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl); | |
890 | if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) { | |
891 | nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT); | |
892 | } | |
893 | if (tun_key->flags & FLOW_TNL_F_CSUM) { | |
894 | nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM); | |
895 | } | |
896 | ||
897 | nl_msg_end_nested(a, tun_key_ofs); | |
29c7a2b2 PS |
898 | } |
899 | ||
e6cc0bab AZ |
900 | static bool |
901 | odp_mask_attr_is_exact(const struct nlattr *ma) | |
902 | { | |
903 | bool is_exact = false; | |
904 | enum ovs_key_attr attr = nl_attr_type(ma); | |
905 | ||
906 | if (attr == OVS_KEY_ATTR_TUNNEL) { | |
907 | /* XXX this is a hack for now. Should change | |
908 | * the exact match dection to per field | |
909 | * instead of per attribute. | |
910 | */ | |
911 | struct flow_tnl tun_mask; | |
912 | memset(&tun_mask, 0, sizeof tun_mask); | |
913 | odp_tun_key_from_attr(ma, &tun_mask); | |
914 | if (tun_mask.flags == (FLOW_TNL_F_KEY | |
915 | | FLOW_TNL_F_DONT_FRAGMENT | |
916 | | FLOW_TNL_F_CSUM)) { | |
917 | /* The flags are exact match, check the remaining fields. */ | |
918 | tun_mask.flags = 0xffff; | |
919 | is_exact = is_all_ones((uint8_t *)&tun_mask, | |
920 | offsetof(struct flow_tnl, ip_ttl)); | |
921 | } | |
922 | } else { | |
923 | is_exact = is_all_ones(nl_attr_get(ma), nl_attr_get_size(ma)); | |
924 | } | |
925 | ||
926 | return is_exact; | |
927 | } | |
928 | ||
929 | ||
36956a7d | 930 | static void |
e6cc0bab AZ |
931 | format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma, |
932 | struct ds *ds) | |
36956a7d | 933 | { |
9b405f1a | 934 | struct flow_tnl tun_key; |
7a8e9ed2 | 935 | enum ovs_key_attr attr = nl_attr_type(a); |
e6603631 | 936 | char namebuf[OVS_KEY_ATTR_BUFSIZE]; |
fea393b1 | 937 | int expected_len; |
dc8c5408 | 938 | bool is_exact; |
36956a7d | 939 | |
dc8c5408 | 940 | is_exact = ma ? odp_mask_attr_is_exact(ma) : true; |
e6cc0bab | 941 | |
e6603631 | 942 | ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf)); |
e6cc0bab AZ |
943 | |
944 | { | |
945 | expected_len = odp_flow_key_attr_len(nl_attr_type(a)); | |
946 | if (expected_len != -2) { | |
947 | bool bad_key_len = nl_attr_get_size(a) != expected_len; | |
948 | bool bad_mask_len = ma && nl_attr_get_size(a) != expected_len; | |
949 | ||
950 | if (bad_key_len || bad_mask_len) { | |
951 | if (bad_key_len) { | |
952 | ds_put_format(ds, "(bad key length %zu, expected %d)(", | |
953 | nl_attr_get_size(a), | |
954 | odp_flow_key_attr_len(nl_attr_type(a))); | |
955 | } | |
956 | format_generic_odp_key(a, ds); | |
957 | if (bad_mask_len) { | |
958 | ds_put_char(ds, '/'); | |
959 | ds_put_format(ds, "(bad mask length %zu, expected %d)(", | |
960 | nl_attr_get_size(ma), | |
961 | odp_flow_key_attr_len(nl_attr_type(ma))); | |
962 | } | |
963 | format_generic_odp_key(ma, ds); | |
964 | ds_put_char(ds, ')'); | |
965 | return; | |
966 | } | |
967 | } | |
36956a7d BP |
968 | } |
969 | ||
e6cc0bab | 970 | ds_put_char(ds, '('); |
7a8e9ed2 | 971 | switch (attr) { |
fea393b1 | 972 | case OVS_KEY_ATTR_ENCAP: |
e6cc0bab AZ |
973 | if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) { |
974 | odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), | |
975 | nl_attr_get(ma), nl_attr_get_size(ma), ds); | |
976 | } else if (nl_attr_get_size(a)) { | |
977 | odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, ds); | |
fea393b1 | 978 | } |
fea393b1 BP |
979 | break; |
980 | ||
abff858b | 981 | case OVS_KEY_ATTR_PRIORITY: |
72e8bf28 | 982 | case OVS_KEY_ATTR_SKB_MARK: |
e6cc0bab | 983 | ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a)); |
dc8c5408 | 984 | if (!is_exact) { |
e6cc0bab AZ |
985 | ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma)); |
986 | } | |
72e8bf28 AA |
987 | break; |
988 | ||
9b405f1a PS |
989 | case OVS_KEY_ATTR_TUNNEL: |
990 | memset(&tun_key, 0, sizeof tun_key); | |
617e10e7 | 991 | if (odp_tun_key_from_attr(a, &tun_key) == ODP_FIT_ERROR) { |
e6cc0bab | 992 | ds_put_format(ds, "error"); |
dc8c5408 | 993 | } else if (!is_exact) { |
e6cc0bab AZ |
994 | struct flow_tnl tun_mask; |
995 | ||
996 | memset(&tun_mask, 0, sizeof tun_mask); | |
997 | odp_tun_key_from_attr(ma, &tun_mask); | |
998 | ds_put_format(ds, "tun_id=%#"PRIx64"/%#"PRIx64 | |
999 | ",src="IP_FMT"/"IP_FMT",dst="IP_FMT"/"IP_FMT | |
1000 | ",tos=%#"PRIx8"/%#"PRIx8",ttl=%"PRIu8"/%#"PRIx8 | |
1001 | ",flags(", | |
1002 | ntohll(tun_key.tun_id), ntohll(tun_mask.tun_id), | |
1003 | IP_ARGS(tun_key.ip_src), IP_ARGS(tun_mask.ip_src), | |
1004 | IP_ARGS(tun_key.ip_dst), IP_ARGS(tun_mask.ip_dst), | |
1005 | tun_key.ip_tos, tun_mask.ip_tos, | |
1006 | tun_key.ip_ttl, tun_mask.ip_ttl); | |
1007 | ||
1008 | format_flags(ds, flow_tun_flag_to_string, tun_key.flags, ','); | |
1009 | ||
1010 | /* XXX This code is correct, but enabling it would break the unit | |
1011 | test. Disable it for now until the input parser is fixed. | |
1012 | ||
1013 | ds_put_char(ds, '/'); | |
1014 | format_flags(ds, flow_tun_flag_to_string, tun_mask.flags, ','); | |
1015 | */ | |
1016 | ds_put_char(ds, ')'); | |
9b405f1a | 1017 | } else { |
e6cc0bab | 1018 | ds_put_format(ds, "tun_id=0x%"PRIx64",src="IP_FMT",dst="IP_FMT"," |
9b405f1a PS |
1019 | "tos=0x%"PRIx8",ttl=%"PRIu8",flags(", |
1020 | ntohll(tun_key.tun_id), | |
1021 | IP_ARGS(tun_key.ip_src), | |
1022 | IP_ARGS(tun_key.ip_dst), | |
1023 | tun_key.ip_tos, tun_key.ip_ttl); | |
1024 | ||
e6cc0bab AZ |
1025 | format_flags(ds, flow_tun_flag_to_string, tun_key.flags, ','); |
1026 | ds_put_char(ds, ')'); | |
9b405f1a | 1027 | } |
356af50b KM |
1028 | break; |
1029 | ||
df2c07f4 | 1030 | case OVS_KEY_ATTR_IN_PORT: |
e6cc0bab | 1031 | ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a)); |
dc8c5408 | 1032 | if (!is_exact) { |
e6cc0bab AZ |
1033 | ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma)); |
1034 | } | |
36956a7d BP |
1035 | break; |
1036 | ||
df2c07f4 | 1037 | case OVS_KEY_ATTR_ETHERNET: |
dc8c5408 | 1038 | if (!is_exact) { |
e6cc0bab AZ |
1039 | const struct ovs_key_ethernet *eth_mask = nl_attr_get(ma); |
1040 | const struct ovs_key_ethernet *eth_key = nl_attr_get(a); | |
1041 | ||
1042 | ds_put_format(ds, "src="ETH_ADDR_FMT"/"ETH_ADDR_FMT | |
1043 | ",dst="ETH_ADDR_FMT"/"ETH_ADDR_FMT, | |
1044 | ETH_ADDR_ARGS(eth_key->eth_src), | |
1045 | ETH_ADDR_ARGS(eth_mask->eth_src), | |
1046 | ETH_ADDR_ARGS(eth_key->eth_dst), | |
1047 | ETH_ADDR_ARGS(eth_mask->eth_dst)); | |
1048 | } else { | |
1049 | const struct ovs_key_ethernet *eth_key = nl_attr_get(a); | |
1050 | ||
1051 | ds_put_format(ds, "src="ETH_ADDR_FMT",dst="ETH_ADDR_FMT, | |
1052 | ETH_ADDR_ARGS(eth_key->eth_src), | |
1053 | ETH_ADDR_ARGS(eth_key->eth_dst)); | |
1054 | } | |
36956a7d BP |
1055 | break; |
1056 | ||
fea393b1 | 1057 | case OVS_KEY_ATTR_VLAN: |
e6cc0bab AZ |
1058 | { |
1059 | ovs_be16 vlan_tci = nl_attr_get_be16(a); | |
dc8c5408 | 1060 | if (!is_exact) { |
e6cc0bab | 1061 | ovs_be16 mask = nl_attr_get_be16(ma); |
2d39507c | 1062 | ds_put_format(ds, "vid=%"PRIu16"/0x%"PRIx16",pcp=%d/0x%x,cfi=%d/%d", |
e6cc0bab AZ |
1063 | vlan_tci_to_vid(vlan_tci), |
1064 | vlan_tci_to_vid(mask), | |
1065 | vlan_tci_to_pcp(vlan_tci), | |
b06b6a39 | 1066 | vlan_tci_to_pcp(mask), |
e6cc0bab AZ |
1067 | vlan_tci_to_cfi(vlan_tci), |
1068 | vlan_tci_to_cfi(mask)); | |
1069 | } else { | |
1070 | format_vlan_tci(ds, vlan_tci); | |
1071 | } | |
1072 | } | |
36956a7d BP |
1073 | break; |
1074 | ||
b02475c5 SH |
1075 | case OVS_KEY_ATTR_MPLS: { |
1076 | const struct ovs_key_mpls *mpls_key = nl_attr_get(a); | |
e6cc0bab | 1077 | const struct ovs_key_mpls *mpls_mask = NULL; |
dc8c5408 | 1078 | if (!is_exact) { |
e6cc0bab AZ |
1079 | mpls_mask = nl_attr_get(ma); |
1080 | } | |
1081 | format_mpls(ds, mpls_key, mpls_mask); | |
b02475c5 SH |
1082 | break; |
1083 | } | |
1084 | ||
df2c07f4 | 1085 | case OVS_KEY_ATTR_ETHERTYPE: |
e6cc0bab | 1086 | ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a))); |
dc8c5408 | 1087 | if (!is_exact) { |
e6cc0bab AZ |
1088 | ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma))); |
1089 | } | |
36956a7d BP |
1090 | break; |
1091 | ||
df2c07f4 | 1092 | case OVS_KEY_ATTR_IPV4: |
dc8c5408 | 1093 | if (!is_exact) { |
e6cc0bab AZ |
1094 | const struct ovs_key_ipv4 *ipv4_key = nl_attr_get(a); |
1095 | const struct ovs_key_ipv4 *ipv4_mask = nl_attr_get(ma); | |
1096 | ||
1097 | ds_put_format(ds, "src="IP_FMT"/"IP_FMT",dst="IP_FMT"/"IP_FMT | |
1098 | ",proto=%"PRIu8"/%#"PRIx8",tos=%#"PRIx8"/%#"PRIx8 | |
1099 | ",ttl=%"PRIu8"/%#"PRIx8",frag=%s/%#"PRIx8, | |
1100 | IP_ARGS(ipv4_key->ipv4_src), | |
1101 | IP_ARGS(ipv4_mask->ipv4_src), | |
1102 | IP_ARGS(ipv4_key->ipv4_dst), | |
1103 | IP_ARGS(ipv4_mask->ipv4_dst), | |
1104 | ipv4_key->ipv4_proto, ipv4_mask->ipv4_proto, | |
1105 | ipv4_key->ipv4_tos, ipv4_mask->ipv4_tos, | |
1106 | ipv4_key->ipv4_ttl, ipv4_mask->ipv4_ttl, | |
1107 | ovs_frag_type_to_string(ipv4_key->ipv4_frag), | |
1108 | ipv4_mask->ipv4_frag); | |
1109 | } else { | |
1110 | const struct ovs_key_ipv4 *ipv4_key = nl_attr_get(a); | |
1111 | ||
1112 | ds_put_format(ds, "src="IP_FMT",dst="IP_FMT",proto=%"PRIu8 | |
1113 | ",tos=%#"PRIx8",ttl=%"PRIu8",frag=%s", | |
1114 | IP_ARGS(ipv4_key->ipv4_src), | |
1115 | IP_ARGS(ipv4_key->ipv4_dst), | |
1116 | ipv4_key->ipv4_proto, ipv4_key->ipv4_tos, | |
1117 | ipv4_key->ipv4_ttl, | |
1118 | ovs_frag_type_to_string(ipv4_key->ipv4_frag)); | |
1119 | } | |
36956a7d BP |
1120 | break; |
1121 | ||
e6cc0bab | 1122 | case OVS_KEY_ATTR_IPV6: |
dc8c5408 | 1123 | if (!is_exact) { |
e6cc0bab AZ |
1124 | const struct ovs_key_ipv6 *ipv6_key, *ipv6_mask; |
1125 | char src_str[INET6_ADDRSTRLEN]; | |
1126 | char dst_str[INET6_ADDRSTRLEN]; | |
1127 | char src_mask[INET6_ADDRSTRLEN]; | |
1128 | char dst_mask[INET6_ADDRSTRLEN]; | |
1129 | ||
1130 | ipv6_key = nl_attr_get(a); | |
1131 | inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str); | |
1132 | inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str); | |
1133 | ||
1134 | ipv6_mask = nl_attr_get(ma); | |
1135 | inet_ntop(AF_INET6, ipv6_mask->ipv6_src, src_mask, sizeof src_mask); | |
1136 | inet_ntop(AF_INET6, ipv6_mask->ipv6_dst, dst_mask, sizeof dst_mask); | |
1137 | ||
1138 | ds_put_format(ds, "src=%s/%s,dst=%s/%s,label=%#"PRIx32"/%#"PRIx32 | |
1139 | ",proto=%"PRIu8"/%#"PRIx8",tclass=%#"PRIx8"/%#"PRIx8 | |
1140 | ",hlimit=%"PRIu8"/%#"PRIx8",frag=%s/%#"PRIx8, | |
1141 | src_str, src_mask, dst_str, dst_mask, | |
1142 | ntohl(ipv6_key->ipv6_label), | |
1143 | ntohl(ipv6_mask->ipv6_label), | |
1144 | ipv6_key->ipv6_proto, ipv6_mask->ipv6_proto, | |
1145 | ipv6_key->ipv6_tclass, ipv6_mask->ipv6_tclass, | |
1146 | ipv6_key->ipv6_hlimit, ipv6_mask->ipv6_hlimit, | |
1147 | ovs_frag_type_to_string(ipv6_key->ipv6_frag), | |
1148 | ipv6_mask->ipv6_frag); | |
1149 | } else { | |
1150 | const struct ovs_key_ipv6 *ipv6_key; | |
1151 | char src_str[INET6_ADDRSTRLEN]; | |
1152 | char dst_str[INET6_ADDRSTRLEN]; | |
1153 | ||
1154 | ipv6_key = nl_attr_get(a); | |
1155 | inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str); | |
1156 | inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str); | |
1157 | ||
1158 | ds_put_format(ds, "src=%s,dst=%s,label=%#"PRIx32",proto=%"PRIu8 | |
1159 | ",tclass=%#"PRIx8",hlimit=%"PRIu8",frag=%s", | |
1160 | src_str, dst_str, ntohl(ipv6_key->ipv6_label), | |
1161 | ipv6_key->ipv6_proto, ipv6_key->ipv6_tclass, | |
1162 | ipv6_key->ipv6_hlimit, | |
1163 | ovs_frag_type_to_string(ipv6_key->ipv6_frag)); | |
1164 | } | |
d31f1109 | 1165 | break; |
d31f1109 | 1166 | |
df2c07f4 | 1167 | case OVS_KEY_ATTR_TCP: |
dc8c5408 | 1168 | if (!is_exact) { |
e6cc0bab AZ |
1169 | const struct ovs_key_tcp *tcp_mask = nl_attr_get(ma); |
1170 | const struct ovs_key_tcp *tcp_key = nl_attr_get(a); | |
1171 | ||
1172 | ds_put_format(ds, "src=%"PRIu16"/%#"PRIx16 | |
1173 | ",dst=%"PRIu16"/%#"PRIx16, | |
1174 | ntohs(tcp_key->tcp_src), ntohs(tcp_mask->tcp_src), | |
1175 | ntohs(tcp_key->tcp_dst), ntohs(tcp_mask->tcp_dst)); | |
1176 | } else { | |
1177 | const struct ovs_key_tcp *tcp_key = nl_attr_get(a); | |
1178 | ||
1179 | ds_put_format(ds, "src=%"PRIu16",dst=%"PRIu16, | |
1180 | ntohs(tcp_key->tcp_src), ntohs(tcp_key->tcp_dst)); | |
1181 | } | |
36956a7d BP |
1182 | break; |
1183 | ||
df2c07f4 | 1184 | case OVS_KEY_ATTR_UDP: |
dc8c5408 | 1185 | if (!is_exact) { |
e6cc0bab AZ |
1186 | const struct ovs_key_udp *udp_mask = nl_attr_get(ma); |
1187 | const struct ovs_key_udp *udp_key = nl_attr_get(a); | |
1188 | ||
1189 | ds_put_format(ds, "src=%"PRIu16"/%#"PRIx16 | |
1190 | ",dst=%"PRIu16"/%#"PRIx16, | |
1191 | ntohs(udp_key->udp_src), ntohs(udp_mask->udp_src), | |
1192 | ntohs(udp_key->udp_dst), ntohs(udp_mask->udp_dst)); | |
1193 | } else { | |
1194 | const struct ovs_key_udp *udp_key = nl_attr_get(a); | |
1195 | ||
1196 | ds_put_format(ds, "src=%"PRIu16",dst=%"PRIu16, | |
1197 | ntohs(udp_key->udp_src), ntohs(udp_key->udp_dst)); | |
1198 | } | |
36956a7d BP |
1199 | break; |
1200 | ||
df2c07f4 | 1201 | case OVS_KEY_ATTR_ICMP: |
dc8c5408 | 1202 | if (!is_exact) { |
e6cc0bab AZ |
1203 | const struct ovs_key_icmp *icmp_mask = nl_attr_get(ma); |
1204 | const struct ovs_key_icmp *icmp_key = nl_attr_get(a); | |
1205 | ||
1206 | ds_put_format(ds, "type=%"PRIu8"/%#"PRIx8",code=%"PRIu8"/%#"PRIx8, | |
1207 | icmp_key->icmp_type, icmp_mask->icmp_type, | |
1208 | icmp_key->icmp_code, icmp_mask->icmp_code); | |
1209 | } else { | |
1210 | const struct ovs_key_icmp *icmp_key = nl_attr_get(a); | |
1211 | ||
1212 | ds_put_format(ds, "type=%"PRIu8",code=%"PRIu8, | |
1213 | icmp_key->icmp_type, icmp_key->icmp_code); | |
1214 | } | |
36956a7d BP |
1215 | break; |
1216 | ||
df2c07f4 | 1217 | case OVS_KEY_ATTR_ICMPV6: |
dc8c5408 | 1218 | if (!is_exact) { |
e6cc0bab AZ |
1219 | const struct ovs_key_icmpv6 *icmpv6_mask = nl_attr_get(ma); |
1220 | const struct ovs_key_icmpv6 *icmpv6_key = nl_attr_get(a); | |
1221 | ||
1222 | ds_put_format(ds, "type=%"PRIu8"/%#"PRIx8",code=%"PRIu8"/%#"PRIx8, | |
1223 | icmpv6_key->icmpv6_type, icmpv6_mask->icmpv6_type, | |
1224 | icmpv6_key->icmpv6_code, icmpv6_mask->icmpv6_code); | |
1225 | } else { | |
1226 | const struct ovs_key_icmpv6 *icmpv6_key = nl_attr_get(a); | |
1227 | ||
1228 | ds_put_format(ds, "type=%"PRIu8",code=%"PRIu8, | |
1229 | icmpv6_key->icmpv6_type, icmpv6_key->icmpv6_code); | |
1230 | } | |
d31f1109 JP |
1231 | break; |
1232 | ||
df2c07f4 | 1233 | case OVS_KEY_ATTR_ARP: |
dc8c5408 | 1234 | if (!is_exact) { |
e6cc0bab AZ |
1235 | const struct ovs_key_arp *arp_mask = nl_attr_get(ma); |
1236 | const struct ovs_key_arp *arp_key = nl_attr_get(a); | |
1237 | ||
1238 | ds_put_format(ds, "sip="IP_FMT"/"IP_FMT",tip="IP_FMT"/"IP_FMT | |
1239 | ",op=%"PRIu16"/%#"PRIx16 | |
1240 | ",sha="ETH_ADDR_FMT"/"ETH_ADDR_FMT | |
1241 | ",tha="ETH_ADDR_FMT"/"ETH_ADDR_FMT, | |
1242 | IP_ARGS(arp_key->arp_sip), | |
1243 | IP_ARGS(arp_mask->arp_sip), | |
1244 | IP_ARGS(arp_key->arp_tip), | |
1245 | IP_ARGS(arp_mask->arp_tip), | |
1246 | ntohs(arp_key->arp_op), ntohs(arp_mask->arp_op), | |
1247 | ETH_ADDR_ARGS(arp_key->arp_sha), | |
1248 | ETH_ADDR_ARGS(arp_mask->arp_sha), | |
1249 | ETH_ADDR_ARGS(arp_key->arp_tha), | |
1250 | ETH_ADDR_ARGS(arp_mask->arp_tha)); | |
1251 | } else { | |
1252 | const struct ovs_key_arp *arp_key = nl_attr_get(a); | |
1253 | ||
1254 | ds_put_format(ds, "sip="IP_FMT",tip="IP_FMT",op=%"PRIu16"," | |
1255 | "sha="ETH_ADDR_FMT",tha="ETH_ADDR_FMT, | |
1256 | IP_ARGS(arp_key->arp_sip), IP_ARGS(arp_key->arp_tip), | |
1257 | ntohs(arp_key->arp_op), | |
1258 | ETH_ADDR_ARGS(arp_key->arp_sha), | |
1259 | ETH_ADDR_ARGS(arp_key->arp_tha)); | |
1260 | } | |
36956a7d BP |
1261 | break; |
1262 | ||
df2c07f4 | 1263 | case OVS_KEY_ATTR_ND: { |
dc8c5408 | 1264 | const struct ovs_key_nd *nd_key, *nd_mask = NULL; |
685a51a5 JP |
1265 | char target[INET6_ADDRSTRLEN]; |
1266 | ||
1267 | nd_key = nl_attr_get(a); | |
dc8c5408 AZ |
1268 | if (!is_exact) { |
1269 | nd_mask = nl_attr_get(ma); | |
1270 | } | |
e6cc0bab | 1271 | |
685a51a5 | 1272 | inet_ntop(AF_INET6, nd_key->nd_target, target, sizeof target); |
e6cc0bab | 1273 | ds_put_format(ds, "target=%s", target); |
dc8c5408 | 1274 | if (!is_exact) { |
e6cc0bab AZ |
1275 | inet_ntop(AF_INET6, nd_mask->nd_target, target, sizeof target); |
1276 | ds_put_format(ds, "/%s", target); | |
1277 | } | |
685a51a5 | 1278 | |
685a51a5 JP |
1279 | if (!eth_addr_is_zero(nd_key->nd_sll)) { |
1280 | ds_put_format(ds, ",sll="ETH_ADDR_FMT, | |
1281 | ETH_ADDR_ARGS(nd_key->nd_sll)); | |
dc8c5408 | 1282 | if (!is_exact) { |
e6cc0bab AZ |
1283 | ds_put_format(ds, "/"ETH_ADDR_FMT, |
1284 | ETH_ADDR_ARGS(nd_mask->nd_sll)); | |
1285 | } | |
685a51a5 JP |
1286 | } |
1287 | if (!eth_addr_is_zero(nd_key->nd_tll)) { | |
1288 | ds_put_format(ds, ",tll="ETH_ADDR_FMT, | |
1289 | ETH_ADDR_ARGS(nd_key->nd_tll)); | |
dc8c5408 | 1290 | if (!is_exact) { |
e6cc0bab AZ |
1291 | ds_put_format(ds, "/"ETH_ADDR_FMT, |
1292 | ETH_ADDR_ARGS(nd_mask->nd_tll)); | |
1293 | } | |
685a51a5 | 1294 | } |
685a51a5 JP |
1295 | break; |
1296 | } | |
1297 | ||
7a8e9ed2 BP |
1298 | case OVS_KEY_ATTR_UNSPEC: |
1299 | case __OVS_KEY_ATTR_MAX: | |
36956a7d BP |
1300 | default: |
1301 | format_generic_odp_key(a, ds); | |
dc8c5408 | 1302 | if (!is_exact) { |
e6cc0bab AZ |
1303 | ds_put_char(ds, '/'); |
1304 | format_generic_odp_key(ma, ds); | |
1305 | } | |
36956a7d BP |
1306 | break; |
1307 | } | |
e6cc0bab | 1308 | ds_put_char(ds, ')'); |
36956a7d BP |
1309 | } |
1310 | ||
dc8c5408 AZ |
1311 | static struct nlattr * |
1312 | generate_all_wildcard_mask(struct ofpbuf *ofp, const struct nlattr *key) | |
1313 | { | |
1314 | const struct nlattr *a; | |
1315 | unsigned int left; | |
1316 | int type = nl_attr_type(key); | |
1317 | int size = nl_attr_get_size(key); | |
1318 | ||
1319 | if (odp_flow_key_attr_len(type) >=0) { | |
1320 | memset(nl_msg_put_unspec_uninit(ofp, type, size), 0, size); | |
1321 | } else { | |
1322 | size_t nested_mask; | |
1323 | ||
1324 | nested_mask = nl_msg_start_nested(ofp, type); | |
1325 | NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) { | |
1326 | generate_all_wildcard_mask(ofp, nl_attr_get(a)); | |
1327 | } | |
1328 | nl_msg_end_nested(ofp, nested_mask); | |
1329 | } | |
1330 | ||
1331 | return ofp->base; | |
1332 | } | |
1333 | ||
36956a7d | 1334 | /* Appends to 'ds' a string representation of the 'key_len' bytes of |
e6cc0bab AZ |
1335 | * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the |
1336 | * 'mask_len' bytes of 'mask' which apply to 'key'. */ | |
14608a15 | 1337 | void |
e6cc0bab AZ |
1338 | odp_flow_format(const struct nlattr *key, size_t key_len, |
1339 | const struct nlattr *mask, size_t mask_len, | |
1340 | struct ds *ds) | |
14608a15 | 1341 | { |
36956a7d BP |
1342 | if (key_len) { |
1343 | const struct nlattr *a; | |
1344 | unsigned int left; | |
e6cc0bab AZ |
1345 | bool has_ethtype_key = false; |
1346 | const struct nlattr *ma = NULL; | |
dc8c5408 | 1347 | struct ofpbuf ofp; |
36956a7d | 1348 | |
dc8c5408 | 1349 | ofpbuf_init(&ofp, 100); |
36956a7d BP |
1350 | NL_ATTR_FOR_EACH (a, left, key, key_len) { |
1351 | if (a != key) { | |
1352 | ds_put_char(ds, ','); | |
1353 | } | |
e6cc0bab AZ |
1354 | if (nl_attr_type(a) == OVS_KEY_ATTR_ETHERTYPE) { |
1355 | has_ethtype_key = true; | |
1356 | } | |
1357 | if (mask && mask_len) { | |
1358 | ma = nl_attr_find__(mask, mask_len, nl_attr_type(a)); | |
dc8c5408 AZ |
1359 | if (!ma) { |
1360 | ma = generate_all_wildcard_mask(&ofp, a); | |
1361 | } | |
e6cc0bab AZ |
1362 | } |
1363 | format_odp_key_attr(a, ma, ds); | |
dc8c5408 | 1364 | ofpbuf_clear(&ofp); |
36956a7d | 1365 | } |
dc8c5408 AZ |
1366 | ofpbuf_uninit(&ofp); |
1367 | ||
36956a7d | 1368 | if (left) { |
3a8cfc50 BP |
1369 | int i; |
1370 | ||
36956a7d BP |
1371 | if (left == key_len) { |
1372 | ds_put_cstr(ds, "<empty>"); | |
1373 | } | |
3a8cfc50 BP |
1374 | ds_put_format(ds, ",***%u leftover bytes*** (", left); |
1375 | for (i = 0; i < left; i++) { | |
1376 | ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]); | |
1377 | } | |
1378 | ds_put_char(ds, ')'); | |
36956a7d | 1379 | } |
e6cc0bab AZ |
1380 | if (!has_ethtype_key) { |
1381 | ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE); | |
1382 | if (ma) { | |
1383 | ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")", | |
1384 | ntohs(nl_attr_get_be16(ma))); | |
1385 | } | |
1386 | } | |
36956a7d BP |
1387 | } else { |
1388 | ds_put_cstr(ds, "<empty>"); | |
1389 | } | |
14608a15 | 1390 | } |
064af421 | 1391 | |
e6cc0bab AZ |
1392 | /* Appends to 'ds' a string representation of the 'key_len' bytes of |
1393 | * OVS_KEY_ATTR_* attributes in 'key'. */ | |
1394 | void | |
1395 | odp_flow_key_format(const struct nlattr *key, | |
1396 | size_t key_len, struct ds *ds) | |
1397 | { | |
1398 | odp_flow_format(key, key_len, NULL, 0, ds); | |
1399 | } | |
1400 | ||
dc8c5408 AZ |
1401 | static void |
1402 | put_nd(struct ovs_key_nd* nd_key, const uint8_t *nd_sll, | |
1403 | const uint8_t *nd_tll, struct ofpbuf *key) | |
1404 | { | |
1405 | if (nd_sll) { | |
1406 | memcpy(nd_key->nd_sll, nd_sll, ETH_ADDR_LEN); | |
1407 | } | |
1408 | ||
1409 | if (nd_tll) { | |
1410 | memcpy(nd_key->nd_tll, nd_tll, ETH_ADDR_LEN); | |
1411 | } | |
1412 | ||
1413 | nl_msg_put_unspec(key, OVS_KEY_ATTR_ND, nd_key, sizeof *nd_key); | |
1414 | } | |
1415 | ||
3bffc610 | 1416 | static int |
dc8c5408 AZ |
1417 | put_nd_key(int n, const char *nd_target_s, const uint8_t *nd_sll, |
1418 | const uint8_t *nd_tll, struct ofpbuf *key) | |
3bffc610 | 1419 | { |
df2c07f4 | 1420 | struct ovs_key_nd nd_key; |
3bffc610 BP |
1421 | |
1422 | memset(&nd_key, 0, sizeof nd_key); | |
dc8c5408 | 1423 | |
3bffc610 BP |
1424 | if (inet_pton(AF_INET6, nd_target_s, nd_key.nd_target) != 1) { |
1425 | return -EINVAL; | |
1426 | } | |
dc8c5408 AZ |
1427 | |
1428 | put_nd(&nd_key, nd_sll, nd_tll, key); | |
1429 | return n; | |
1430 | } | |
1431 | ||
1432 | static int | |
1433 | put_nd_mask(int n, const char *nd_target_s, | |
1434 | const uint8_t *nd_sll, const uint8_t *nd_tll, struct ofpbuf *mask) | |
1435 | { | |
1436 | struct ovs_key_nd nd_mask; | |
1437 | ||
1438 | memset(&nd_mask, 0xff, sizeof nd_mask); | |
1439 | ||
1440 | if (strlen(nd_target_s) != 0 && | |
1441 | inet_pton(AF_INET6, nd_target_s, nd_mask.nd_target) != 1) { | |
1442 | return -EINVAL; | |
3bffc610 | 1443 | } |
dc8c5408 AZ |
1444 | |
1445 | put_nd(&nd_mask, nd_sll, nd_tll, mask); | |
3bffc610 BP |
1446 | return n; |
1447 | } | |
1448 | ||
7257b535 BP |
1449 | static bool |
1450 | ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type) | |
1451 | { | |
1452 | if (!strcasecmp(s, "no")) { | |
1453 | *type = OVS_FRAG_TYPE_NONE; | |
1454 | } else if (!strcasecmp(s, "first")) { | |
1455 | *type = OVS_FRAG_TYPE_FIRST; | |
1456 | } else if (!strcasecmp(s, "later")) { | |
1457 | *type = OVS_FRAG_TYPE_LATER; | |
1458 | } else { | |
1459 | return false; | |
1460 | } | |
1461 | return true; | |
1462 | } | |
1463 | ||
b02475c5 SH |
1464 | static ovs_be32 |
1465 | mpls_lse_from_components(int mpls_label, int mpls_tc, int mpls_ttl, int mpls_bos) | |
1466 | { | |
1467 | return (htonl((mpls_label << MPLS_LABEL_SHIFT) | | |
1468 | (mpls_tc << MPLS_TC_SHIFT) | | |
1469 | (mpls_ttl << MPLS_TTL_SHIFT) | | |
1470 | (mpls_bos << MPLS_BOS_SHIFT))); | |
1471 | } | |
1472 | ||
3bffc610 | 1473 | static int |
e6cc0bab AZ |
1474 | parse_odp_key_mask_attr(const char *s, const struct simap *port_names, |
1475 | struct ofpbuf *key, struct ofpbuf *mask) | |
3bffc610 BP |
1476 | { |
1477 | /* Many of the sscanf calls in this function use oversized destination | |
1478 | * fields because some sscanf() implementations truncate the range of %i | |
1479 | * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a | |
1480 | * value of 0x7fff. The other alternatives are to allow only a single | |
1481 | * radix (e.g. decimal or hexadecimal) or to write more sophisticated | |
1482 | * parsers. | |
1483 | * | |
1484 | * The tun_id parser has to use an alternative approach because there is no | |
1485 | * type larger than 64 bits. */ | |
1486 | ||
abff858b PS |
1487 | { |
1488 | unsigned long long int priority; | |
e6cc0bab | 1489 | unsigned long long int priority_mask; |
abff858b PS |
1490 | int n = -1; |
1491 | ||
e6cc0bab AZ |
1492 | if (mask && sscanf(s, "skb_priority(%lli/%lli)%n", &priority, |
1493 | &priority_mask, &n) > 0 && n > 0) { | |
1494 | nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority); | |
1495 | nl_msg_put_u32(mask, OVS_KEY_ATTR_PRIORITY, priority_mask); | |
1496 | return n; | |
1497 | } else if (sscanf(s, "skb_priority(%lli)%n", | |
1498 | &priority, &n) > 0 && n > 0) { | |
abff858b | 1499 | nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority); |
e6cc0bab AZ |
1500 | if (mask) { |
1501 | nl_msg_put_u32(mask, OVS_KEY_ATTR_PRIORITY, UINT32_MAX); | |
1502 | } | |
abff858b PS |
1503 | return n; |
1504 | } | |
1505 | } | |
1506 | ||
72e8bf28 AA |
1507 | { |
1508 | unsigned long long int mark; | |
e6cc0bab | 1509 | unsigned long long int mark_mask; |
72e8bf28 AA |
1510 | int n = -1; |
1511 | ||
e6cc0bab AZ |
1512 | if (mask && sscanf(s, "skb_mark(%lli/%lli)%n", &mark, |
1513 | &mark_mask, &n) > 0 && n > 0) { | |
1514 | nl_msg_put_u32(key, OVS_KEY_ATTR_SKB_MARK, mark); | |
1515 | nl_msg_put_u32(mask, OVS_KEY_ATTR_SKB_MARK, mark_mask); | |
1516 | return n; | |
1517 | } else if (sscanf(s, "skb_mark(%lli)%n", &mark, &n) > 0 && n > 0) { | |
72e8bf28 | 1518 | nl_msg_put_u32(key, OVS_KEY_ATTR_SKB_MARK, mark); |
e6cc0bab AZ |
1519 | if (mask) { |
1520 | nl_msg_put_u32(mask, OVS_KEY_ATTR_SKB_MARK, UINT32_MAX); | |
1521 | } | |
72e8bf28 AA |
1522 | return n; |
1523 | } | |
1524 | } | |
1525 | ||
35651d6a JG |
1526 | { |
1527 | char tun_id_s[32]; | |
e6cc0bab AZ |
1528 | int tos, tos_mask, ttl, ttl_mask; |
1529 | struct flow_tnl tun_key, tun_key_mask; | |
1530 | unsigned long long tun_id_mask; | |
35651d6a JG |
1531 | int n = -1; |
1532 | ||
e6cc0bab AZ |
1533 | if (mask && sscanf(s, "tunnel(tun_id=%31[x0123456789abcdefABCDEF]/%llx," |
1534 | "src="IP_SCAN_FMT"/"IP_SCAN_FMT",dst="IP_SCAN_FMT | |
1535 | "/"IP_SCAN_FMT",tos=%i/%i,ttl=%i/%i,flags%n", | |
1536 | tun_id_s, &tun_id_mask, | |
1537 | IP_SCAN_ARGS(&tun_key.ip_src), | |
1538 | IP_SCAN_ARGS(&tun_key_mask.ip_src), | |
1539 | IP_SCAN_ARGS(&tun_key.ip_dst), | |
1540 | IP_SCAN_ARGS(&tun_key_mask.ip_dst), | |
1541 | &tos, &tos_mask, &ttl, &ttl_mask, | |
1542 | &n) > 0 && n > 0) { | |
1543 | int res; | |
1544 | uint32_t flags; | |
1545 | ||
1546 | tun_key.tun_id = htonll(strtoull(tun_id_s, NULL, 0)); | |
1547 | tun_key_mask.tun_id = htonll(tun_id_mask); | |
1548 | tun_key.ip_tos = tos; | |
1549 | tun_key_mask.ip_tos = tos_mask; | |
1550 | tun_key.ip_ttl = ttl; | |
1551 | tun_key_mask.ip_ttl = ttl_mask; | |
1552 | res = parse_flags(&s[n], flow_tun_flag_to_string, &flags); | |
1553 | tun_key.flags = flags; | |
1554 | tun_key_mask.flags = UINT16_MAX; | |
1555 | ||
1556 | if (res < 0) { | |
1557 | return res; | |
1558 | } | |
1559 | n += res; | |
1560 | if (s[n] != ')') { | |
1561 | return -EINVAL; | |
1562 | } | |
1563 | n++; | |
1564 | tun_key_to_attr(key, &tun_key); | |
1565 | if (mask) { | |
1566 | tun_key_to_attr(mask, &tun_key_mask); | |
1567 | } | |
1568 | return n; | |
1569 | } else if (sscanf(s, "tunnel(tun_id=%31[x0123456789abcdefABCDEF]," | |
29c7a2b2 PS |
1570 | "src="IP_SCAN_FMT",dst="IP_SCAN_FMT |
1571 | ",tos=%i,ttl=%i,flags%n", tun_id_s, | |
9b405f1a PS |
1572 | IP_SCAN_ARGS(&tun_key.ip_src), |
1573 | IP_SCAN_ARGS(&tun_key.ip_dst), &tos, &ttl, | |
35651d6a | 1574 | &n) > 0 && n > 0) { |
29c7a2b2 | 1575 | int res; |
9b405f1a | 1576 | uint32_t flags; |
29c7a2b2 | 1577 | |
35651d6a | 1578 | tun_key.tun_id = htonll(strtoull(tun_id_s, NULL, 0)); |
9b405f1a PS |
1579 | tun_key.ip_tos = tos; |
1580 | tun_key.ip_ttl = ttl; | |
1581 | res = parse_flags(&s[n], flow_tun_flag_to_string, &flags); | |
e6cc0bab | 1582 | tun_key.flags = flags; |
29c7a2b2 | 1583 | |
29c7a2b2 PS |
1584 | if (res < 0) { |
1585 | return res; | |
1586 | } | |
1587 | n += res; | |
1588 | if (s[n] != ')') { | |
1589 | return -EINVAL; | |
1590 | } | |
1591 | n++; | |
9b405f1a | 1592 | tun_key_to_attr(key, &tun_key); |
e6cc0bab AZ |
1593 | |
1594 | if (mask) { | |
1595 | memset(&tun_key, 0xff, sizeof tun_key); | |
1596 | tun_key_to_attr(mask, &tun_key); | |
1597 | } | |
35651d6a JG |
1598 | return n; |
1599 | } | |
1600 | } | |
1601 | ||
3bffc610 BP |
1602 | { |
1603 | unsigned long long int in_port; | |
e6cc0bab | 1604 | unsigned long long int in_port_mask; |
3bffc610 BP |
1605 | int n = -1; |
1606 | ||
e6cc0bab AZ |
1607 | if (mask && sscanf(s, "in_port(%lli/%lli)%n", &in_port, |
1608 | &in_port_mask, &n) > 0 && n > 0) { | |
1609 | nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port); | |
1610 | nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, in_port_mask); | |
1611 | return n; | |
1612 | } else if (sscanf(s, "in_port(%lli)%n", &in_port, &n) > 0 && n > 0) { | |
df2c07f4 | 1613 | nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port); |
e6cc0bab AZ |
1614 | if (mask) { |
1615 | nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, UINT32_MAX); | |
1616 | } | |
3bffc610 BP |
1617 | return n; |
1618 | } | |
1619 | } | |
1620 | ||
e6cc0bab | 1621 | |
b2a60db8 BP |
1622 | if (port_names && !strncmp(s, "in_port(", 8)) { |
1623 | const char *name; | |
44bac24b | 1624 | const struct simap_node *node; |
b2a60db8 BP |
1625 | int name_len; |
1626 | ||
1627 | name = s + 8; | |
1628 | name_len = strcspn(s, ")"); | |
44bac24b | 1629 | node = simap_find_len(port_names, name, name_len); |
b2a60db8 | 1630 | if (node) { |
44bac24b | 1631 | nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, node->data); |
e6cc0bab AZ |
1632 | |
1633 | if (mask) { | |
1634 | nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, UINT32_MAX); | |
1635 | } | |
b2a60db8 BP |
1636 | return 8 + name_len + 1; |
1637 | } | |
1638 | } | |
1639 | ||
3bffc610 | 1640 | { |
df2c07f4 | 1641 | struct ovs_key_ethernet eth_key; |
e6cc0bab | 1642 | struct ovs_key_ethernet eth_key_mask; |
3bffc610 BP |
1643 | int n = -1; |
1644 | ||
e6cc0bab AZ |
1645 | if (mask && sscanf(s, |
1646 | "eth(src="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"," | |
1647 | "dst="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n", | |
1648 | ETH_ADDR_SCAN_ARGS(eth_key.eth_src), | |
1649 | ETH_ADDR_SCAN_ARGS(eth_key_mask.eth_src), | |
1650 | ETH_ADDR_SCAN_ARGS(eth_key.eth_dst), | |
1651 | ETH_ADDR_SCAN_ARGS(eth_key_mask.eth_dst), &n) > 0 && n > 0) { | |
1652 | ||
1653 | nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET, | |
1654 | ð_key, sizeof eth_key); | |
1655 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_ETHERNET, | |
1656 | ð_key_mask, sizeof eth_key_mask); | |
1657 | return n; | |
1658 | } else if (sscanf(s, | |
3bffc610 BP |
1659 | "eth(src="ETH_ADDR_SCAN_FMT",dst="ETH_ADDR_SCAN_FMT")%n", |
1660 | ETH_ADDR_SCAN_ARGS(eth_key.eth_src), | |
1661 | ETH_ADDR_SCAN_ARGS(eth_key.eth_dst), &n) > 0 && n > 0) { | |
df2c07f4 | 1662 | nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET, |
3bffc610 | 1663 | ð_key, sizeof eth_key); |
e6cc0bab AZ |
1664 | |
1665 | if (mask) { | |
1666 | memset(ð_key, 0xff, sizeof eth_key); | |
1667 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_ETHERNET, | |
1668 | ð_key, sizeof eth_key); | |
1669 | } | |
3bffc610 BP |
1670 | return n; |
1671 | } | |
1672 | } | |
1673 | ||
1674 | { | |
e6cc0bab AZ |
1675 | uint16_t vid, vid_mask; |
1676 | int pcp, pcp_mask; | |
1677 | int cfi, cfi_mask; | |
3bffc610 BP |
1678 | int n = -1; |
1679 | ||
e6cc0bab AZ |
1680 | if (mask && (sscanf(s, "vlan(vid=%"SCNi16"/%"SCNi16",pcp=%i/%i)%n", |
1681 | &vid, &vid_mask, &pcp, &pcp_mask, &n) > 0 && n > 0)) { | |
1682 | nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN, | |
1683 | htons((vid << VLAN_VID_SHIFT) | | |
1684 | (pcp << VLAN_PCP_SHIFT) | | |
1685 | VLAN_CFI)); | |
1686 | nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN, | |
1687 | htons((vid_mask << VLAN_VID_SHIFT) | | |
1688 | (pcp_mask << VLAN_PCP_SHIFT) | | |
1689 | (1 << VLAN_CFI_SHIFT))); | |
1690 | return n; | |
1691 | } else if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i)%n", | |
1692 | &vid, &pcp, &n) > 0 && n > 0)) { | |
fea393b1 BP |
1693 | nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN, |
1694 | htons((vid << VLAN_VID_SHIFT) | | |
8ddc056d BP |
1695 | (pcp << VLAN_PCP_SHIFT) | |
1696 | VLAN_CFI)); | |
e6cc0bab AZ |
1697 | if (mask) { |
1698 | nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN, htons(UINT16_MAX)); | |
1699 | } | |
1700 | return n; | |
1701 | } else if (mask && (sscanf(s, "vlan(vid=%"SCNi16"/%"SCNi16",pcp=%i/%i,cfi=%i/%i)%n", | |
1702 | &vid, &vid_mask, &pcp, &pcp_mask, &cfi, &cfi_mask, &n) > 0 && n > 0)) { | |
1703 | nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN, | |
1704 | htons((vid << VLAN_VID_SHIFT) | | |
1705 | (pcp << VLAN_PCP_SHIFT) | | |
1706 | (cfi ? VLAN_CFI : 0))); | |
1707 | nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN, | |
1708 | htons((vid_mask << VLAN_VID_SHIFT) | | |
1709 | (pcp_mask << VLAN_PCP_SHIFT) | | |
1710 | (cfi_mask << VLAN_CFI_SHIFT))); | |
8ddc056d BP |
1711 | return n; |
1712 | } else if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i,cfi=%i)%n", | |
e6cc0bab | 1713 | &vid, &pcp, &cfi, &n) > 0 && n > 0)) { |
8ddc056d BP |
1714 | nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN, |
1715 | htons((vid << VLAN_VID_SHIFT) | | |
1716 | (pcp << VLAN_PCP_SHIFT) | | |
1717 | (cfi ? VLAN_CFI : 0))); | |
e6cc0bab AZ |
1718 | if (mask) { |
1719 | nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN, htons(UINT16_MAX)); | |
1720 | } | |
3bffc610 BP |
1721 | return n; |
1722 | } | |
1723 | } | |
1724 | ||
1725 | { | |
fac2e516 | 1726 | int eth_type; |
e6cc0bab | 1727 | int eth_type_mask; |
3bffc610 BP |
1728 | int n = -1; |
1729 | ||
e6cc0bab AZ |
1730 | if (mask && sscanf(s, "eth_type(%i/%i)%n", |
1731 | ð_type, ð_type_mask, &n) > 0 && n > 0) { | |
1732 | if (eth_type != 0) { | |
1733 | nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type)); | |
1734 | } | |
1735 | nl_msg_put_be16(mask, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type_mask)); | |
1736 | return n; | |
1737 | } else if (sscanf(s, "eth_type(%i)%n", ð_type, &n) > 0 && n > 0) { | |
df2c07f4 | 1738 | nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type)); |
e6cc0bab AZ |
1739 | if (mask) { |
1740 | nl_msg_put_be16(mask, OVS_KEY_ATTR_ETHERTYPE, | |
1741 | htons(UINT16_MAX)); | |
1742 | } | |
3bffc610 BP |
1743 | return n; |
1744 | } | |
1745 | } | |
1746 | ||
b02475c5 SH |
1747 | { |
1748 | int label, tc, ttl, bos; | |
e6cc0bab | 1749 | int label_mask, tc_mask, ttl_mask, bos_mask; |
b02475c5 SH |
1750 | int n = -1; |
1751 | ||
e6cc0bab AZ |
1752 | if (mask && sscanf(s, "mpls(label=%"SCNi32"/%"SCNi32",tc=%i/%i,ttl=%i/%i,bos=%i/%i)%n", |
1753 | &label, &label_mask, &tc, &tc_mask, &ttl, &ttl_mask, &bos, &bos_mask, &n) > 0 && n > 0) { | |
1754 | struct ovs_key_mpls *mpls, *mpls_mask; | |
1755 | ||
1756 | mpls = nl_msg_put_unspec_uninit(key, OVS_KEY_ATTR_MPLS, | |
1757 | sizeof *mpls); | |
1758 | mpls->mpls_lse = mpls_lse_from_components(label, tc, ttl, bos); | |
1759 | ||
1760 | mpls_mask = nl_msg_put_unspec_uninit(mask, OVS_KEY_ATTR_MPLS, | |
1761 | sizeof *mpls_mask); | |
1762 | mpls_mask->mpls_lse = mpls_lse_from_components( | |
1763 | label_mask, tc_mask, ttl_mask, bos_mask); | |
1764 | return n; | |
1765 | } else if (sscanf(s, "mpls(label=%"SCNi32",tc=%i,ttl=%i,bos=%i)%n", | |
b02475c5 SH |
1766 | &label, &tc, &ttl, &bos, &n) > 0 && |
1767 | n > 0) { | |
1768 | struct ovs_key_mpls *mpls; | |
1769 | ||
1770 | mpls = nl_msg_put_unspec_uninit(key, OVS_KEY_ATTR_MPLS, | |
1771 | sizeof *mpls); | |
20131cef | 1772 | mpls->mpls_lse = mpls_lse_from_components(label, tc, ttl, bos); |
e6cc0bab AZ |
1773 | if (mask) { |
1774 | mpls = nl_msg_put_unspec_uninit(mask, OVS_KEY_ATTR_MPLS, | |
1775 | sizeof *mpls); | |
1776 | mpls->mpls_lse = htonl(UINT32_MAX); | |
1777 | } | |
b02475c5 SH |
1778 | return n; |
1779 | } | |
1780 | } | |
1781 | ||
e6cc0bab | 1782 | |
3bffc610 | 1783 | { |
e6cc0bab AZ |
1784 | ovs_be32 ipv4_src, ipv4_src_mask; |
1785 | ovs_be32 ipv4_dst, ipv4_dst_mask; | |
1786 | int ipv4_proto, ipv4_proto_mask; | |
1787 | int ipv4_tos, ipv4_tos_mask; | |
1788 | int ipv4_ttl, ipv4_ttl_mask; | |
7257b535 | 1789 | char frag[8]; |
e6cc0bab | 1790 | int ipv4_frag_mask; |
7257b535 | 1791 | enum ovs_frag_type ipv4_frag; |
3bffc610 BP |
1792 | int n = -1; |
1793 | ||
e6cc0bab AZ |
1794 | if (mask && sscanf(s, "ipv4(src="IP_SCAN_FMT"/"IP_SCAN_FMT"," |
1795 | "dst="IP_SCAN_FMT"/"IP_SCAN_FMT"," | |
1796 | "proto=%i/%i,tos=%i/%i,ttl=%i/%i," | |
1797 | "frag=%7[a-z]/%i)%n", | |
1798 | IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_src_mask), | |
1799 | IP_SCAN_ARGS(&ipv4_dst), IP_SCAN_ARGS(&ipv4_dst_mask), | |
1800 | &ipv4_proto, &ipv4_proto_mask, | |
1801 | &ipv4_tos, &ipv4_tos_mask, &ipv4_ttl, &ipv4_ttl_mask, | |
1802 | frag, &ipv4_frag_mask, &n) > 0 | |
1803 | && n > 0 | |
1804 | && ovs_frag_type_from_string(frag, &ipv4_frag)) { | |
1805 | struct ovs_key_ipv4 ipv4_key; | |
1806 | struct ovs_key_ipv4 ipv4_mask; | |
1807 | ||
1808 | ipv4_key.ipv4_src = ipv4_src; | |
1809 | ipv4_key.ipv4_dst = ipv4_dst; | |
1810 | ipv4_key.ipv4_proto = ipv4_proto; | |
1811 | ipv4_key.ipv4_tos = ipv4_tos; | |
1812 | ipv4_key.ipv4_ttl = ipv4_ttl; | |
1813 | ipv4_key.ipv4_frag = ipv4_frag; | |
1814 | nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4, | |
1815 | &ipv4_key, sizeof ipv4_key); | |
1816 | ||
1817 | ipv4_mask.ipv4_src = ipv4_src_mask; | |
1818 | ipv4_mask.ipv4_dst = ipv4_dst_mask; | |
1819 | ipv4_mask.ipv4_proto = ipv4_proto_mask; | |
1820 | ipv4_mask.ipv4_tos = ipv4_tos_mask; | |
1821 | ipv4_mask.ipv4_ttl = ipv4_ttl_mask; | |
1822 | ipv4_mask.ipv4_frag = ipv4_frag_mask; | |
1823 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV4, | |
1824 | &ipv4_mask, sizeof ipv4_mask); | |
1825 | return n; | |
1826 | } else if (sscanf(s, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT"," | |
a61680c6 | 1827 | "proto=%i,tos=%i,ttl=%i,frag=%7[a-z])%n", |
7257b535 | 1828 | IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_dst), |
a61680c6 | 1829 | &ipv4_proto, &ipv4_tos, &ipv4_ttl, frag, &n) > 0 |
7257b535 BP |
1830 | && n > 0 |
1831 | && ovs_frag_type_from_string(frag, &ipv4_frag)) { | |
df2c07f4 | 1832 | struct ovs_key_ipv4 ipv4_key; |
3bffc610 | 1833 | |
3bffc610 BP |
1834 | ipv4_key.ipv4_src = ipv4_src; |
1835 | ipv4_key.ipv4_dst = ipv4_dst; | |
1836 | ipv4_key.ipv4_proto = ipv4_proto; | |
1837 | ipv4_key.ipv4_tos = ipv4_tos; | |
a61680c6 | 1838 | ipv4_key.ipv4_ttl = ipv4_ttl; |
7257b535 | 1839 | ipv4_key.ipv4_frag = ipv4_frag; |
df2c07f4 | 1840 | nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4, |
3bffc610 | 1841 | &ipv4_key, sizeof ipv4_key); |
e6cc0bab AZ |
1842 | |
1843 | if (mask) { | |
1844 | memset(&ipv4_key, 0xff, sizeof ipv4_key); | |
1845 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV4, | |
1846 | &ipv4_key, sizeof ipv4_key); | |
1847 | } | |
3bffc610 BP |
1848 | return n; |
1849 | } | |
1850 | } | |
1851 | ||
1852 | { | |
1853 | char ipv6_src_s[IPV6_SCAN_LEN + 1]; | |
e6cc0bab | 1854 | char ipv6_src_mask_s[IPV6_SCAN_LEN + 1]; |
3bffc610 | 1855 | char ipv6_dst_s[IPV6_SCAN_LEN + 1]; |
e6cc0bab AZ |
1856 | char ipv6_dst_mask_s[IPV6_SCAN_LEN + 1]; |
1857 | int ipv6_label, ipv6_label_mask; | |
1858 | int ipv6_proto, ipv6_proto_mask; | |
1859 | int ipv6_tclass, ipv6_tclass_mask; | |
1860 | int ipv6_hlimit, ipv6_hlimit_mask; | |
7257b535 BP |
1861 | char frag[8]; |
1862 | enum ovs_frag_type ipv6_frag; | |
e6cc0bab | 1863 | int ipv6_frag_mask; |
3bffc610 BP |
1864 | int n = -1; |
1865 | ||
e6cc0bab AZ |
1866 | if (mask && sscanf(s, "ipv6(src="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT",dst=" |
1867 | IPV6_SCAN_FMT"/"IPV6_SCAN_FMT"," | |
1868 | "label=%i/%i,proto=%i/%i,tclass=%i/%i," | |
1869 | "hlimit=%i/%i,frag=%7[a-z]/%i)%n", | |
1870 | ipv6_src_s, ipv6_src_mask_s, ipv6_dst_s, ipv6_dst_mask_s, | |
1871 | &ipv6_label, &ipv6_label_mask, &ipv6_proto, | |
1872 | &ipv6_proto_mask, &ipv6_tclass, &ipv6_tclass_mask, | |
1873 | &ipv6_hlimit, &ipv6_hlimit_mask, frag, | |
1874 | &ipv6_frag_mask, &n) > 0 | |
1875 | && n > 0 | |
1876 | && ovs_frag_type_from_string(frag, &ipv6_frag)) { | |
1877 | struct ovs_key_ipv6 ipv6_key; | |
1878 | struct ovs_key_ipv6 ipv6_mask; | |
1879 | ||
1880 | if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 || | |
1881 | inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1 || | |
1882 | inet_pton(AF_INET6, ipv6_src_mask_s, &ipv6_mask.ipv6_src) != 1 || | |
1883 | inet_pton(AF_INET6, ipv6_dst_mask_s, &ipv6_mask.ipv6_dst) != 1) { | |
1884 | return -EINVAL; | |
1885 | } | |
1886 | ||
1887 | ipv6_key.ipv6_label = htonl(ipv6_label); | |
1888 | ipv6_key.ipv6_proto = ipv6_proto; | |
1889 | ipv6_key.ipv6_tclass = ipv6_tclass; | |
1890 | ipv6_key.ipv6_hlimit = ipv6_hlimit; | |
1891 | ipv6_key.ipv6_frag = ipv6_frag; | |
1892 | nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6, | |
1893 | &ipv6_key, sizeof ipv6_key); | |
1894 | ||
1895 | ipv6_mask.ipv6_label = htonl(ipv6_label_mask); | |
1896 | ipv6_mask.ipv6_proto = ipv6_proto_mask; | |
1897 | ipv6_mask.ipv6_tclass = ipv6_tclass_mask; | |
1898 | ipv6_mask.ipv6_hlimit = ipv6_hlimit_mask; | |
1899 | ipv6_mask.ipv6_frag = ipv6_frag_mask; | |
1900 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV6, | |
1901 | &ipv6_mask, sizeof ipv6_mask); | |
1902 | return n; | |
1903 | } else if (sscanf(s, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT"," | |
60258dcb | 1904 | "label=%i,proto=%i,tclass=%i,hlimit=%i,frag=%7[a-z])%n", |
fa8223b7 | 1905 | ipv6_src_s, ipv6_dst_s, &ipv6_label, |
60258dcb | 1906 | &ipv6_proto, &ipv6_tclass, &ipv6_hlimit, frag, &n) > 0 |
7257b535 BP |
1907 | && n > 0 |
1908 | && ovs_frag_type_from_string(frag, &ipv6_frag)) { | |
df2c07f4 | 1909 | struct ovs_key_ipv6 ipv6_key; |
3bffc610 | 1910 | |
3bffc610 BP |
1911 | if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 || |
1912 | inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1) { | |
1913 | return -EINVAL; | |
1914 | } | |
fa8223b7 | 1915 | ipv6_key.ipv6_label = htonl(ipv6_label); |
3bffc610 | 1916 | ipv6_key.ipv6_proto = ipv6_proto; |
60258dcb | 1917 | ipv6_key.ipv6_tclass = ipv6_tclass; |
a61680c6 | 1918 | ipv6_key.ipv6_hlimit = ipv6_hlimit; |
7257b535 | 1919 | ipv6_key.ipv6_frag = ipv6_frag; |
df2c07f4 | 1920 | nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6, |
3bffc610 | 1921 | &ipv6_key, sizeof ipv6_key); |
e6cc0bab AZ |
1922 | |
1923 | if (mask) { | |
1924 | memset(&ipv6_key, 0xff, sizeof ipv6_key); | |
1925 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV6, | |
1926 | &ipv6_key, sizeof ipv6_key); | |
1927 | } | |
3bffc610 BP |
1928 | return n; |
1929 | } | |
1930 | } | |
1931 | ||
1932 | { | |
1933 | int tcp_src; | |
1934 | int tcp_dst; | |
e6cc0bab AZ |
1935 | int tcp_src_mask; |
1936 | int tcp_dst_mask; | |
3bffc610 BP |
1937 | int n = -1; |
1938 | ||
e6cc0bab AZ |
1939 | if (mask && sscanf(s, "tcp(src=%i/%i,dst=%i/%i)%n", |
1940 | &tcp_src, &tcp_src_mask, &tcp_dst, &tcp_dst_mask, &n) > 0 | |
3bffc610 | 1941 | && n > 0) { |
df2c07f4 | 1942 | struct ovs_key_tcp tcp_key; |
e6cc0bab | 1943 | struct ovs_key_tcp tcp_mask; |
3bffc610 BP |
1944 | |
1945 | tcp_key.tcp_src = htons(tcp_src); | |
1946 | tcp_key.tcp_dst = htons(tcp_dst); | |
df2c07f4 | 1947 | nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key); |
e6cc0bab AZ |
1948 | |
1949 | tcp_mask.tcp_src = htons(tcp_src_mask); | |
1950 | tcp_mask.tcp_dst = htons(tcp_dst_mask); | |
1951 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_TCP, | |
1952 | &tcp_mask, sizeof tcp_mask); | |
1953 | return n; | |
1954 | } else if (sscanf(s, "tcp(src=%i,dst=%i)%n",&tcp_src, &tcp_dst, &n) > 0 | |
1955 | && n > 0) { | |
1956 | struct ovs_key_tcp tcp_key; | |
1957 | ||
1958 | tcp_key.tcp_src = htons(tcp_src); | |
1959 | tcp_key.tcp_dst = htons(tcp_dst); | |
1960 | nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key); | |
1961 | ||
1962 | if (mask) { | |
1963 | memset(&tcp_key, 0xff, sizeof tcp_key); | |
1964 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_TCP, | |
1965 | &tcp_key, sizeof tcp_key); | |
1966 | } | |
3bffc610 BP |
1967 | return n; |
1968 | } | |
1969 | } | |
1970 | ||
1971 | { | |
1972 | int udp_src; | |
1973 | int udp_dst; | |
e6cc0bab AZ |
1974 | int udp_src_mask; |
1975 | int udp_dst_mask; | |
3bffc610 BP |
1976 | int n = -1; |
1977 | ||
e6cc0bab AZ |
1978 | if (mask && sscanf(s, "udp(src=%i/%i,dst=%i/%i)%n", |
1979 | &udp_src, &udp_src_mask, | |
1980 | &udp_dst, &udp_dst_mask, &n) > 0 && n > 0) { | |
1981 | struct ovs_key_udp udp_key; | |
1982 | struct ovs_key_udp udp_mask; | |
1983 | ||
1984 | udp_key.udp_src = htons(udp_src); | |
1985 | udp_key.udp_dst = htons(udp_dst); | |
1986 | nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key); | |
1987 | ||
1988 | udp_mask.udp_src = htons(udp_src_mask); | |
1989 | udp_mask.udp_dst = htons(udp_dst_mask); | |
1990 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_UDP, | |
1991 | &udp_mask, sizeof udp_mask); | |
1992 | return n; | |
1993 | } | |
3bffc610 BP |
1994 | if (sscanf(s, "udp(src=%i,dst=%i)%n", &udp_src, &udp_dst, &n) > 0 |
1995 | && n > 0) { | |
df2c07f4 | 1996 | struct ovs_key_udp udp_key; |
3bffc610 BP |
1997 | |
1998 | udp_key.udp_src = htons(udp_src); | |
1999 | udp_key.udp_dst = htons(udp_dst); | |
df2c07f4 | 2000 | nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key); |
e6cc0bab AZ |
2001 | |
2002 | if (mask) { | |
2003 | memset(&udp_key, 0xff, sizeof udp_key); | |
2004 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key); | |
2005 | } | |
3bffc610 BP |
2006 | return n; |
2007 | } | |
2008 | } | |
2009 | ||
2010 | { | |
2011 | int icmp_type; | |
2012 | int icmp_code; | |
e6cc0bab AZ |
2013 | int icmp_type_mask; |
2014 | int icmp_code_mask; | |
3bffc610 BP |
2015 | int n = -1; |
2016 | ||
e6cc0bab AZ |
2017 | if (mask && sscanf(s, "icmp(type=%i/%i,code=%i/%i)%n", |
2018 | &icmp_type, &icmp_type_mask, | |
2019 | &icmp_code, &icmp_code_mask, &n) > 0 && n > 0) { | |
2020 | struct ovs_key_icmp icmp_key; | |
2021 | struct ovs_key_icmp icmp_mask; | |
2022 | ||
2023 | icmp_key.icmp_type = icmp_type; | |
2024 | icmp_key.icmp_code = icmp_code; | |
2025 | nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP, | |
2026 | &icmp_key, sizeof icmp_key); | |
2027 | ||
2028 | icmp_mask.icmp_type = icmp_type_mask; | |
2029 | icmp_mask.icmp_code = icmp_code_mask; | |
2030 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMP, | |
2031 | &icmp_mask, sizeof icmp_mask); | |
2032 | return n; | |
2033 | } else if (sscanf(s, "icmp(type=%i,code=%i)%n", | |
3bffc610 BP |
2034 | &icmp_type, &icmp_code, &n) > 0 |
2035 | && n > 0) { | |
df2c07f4 | 2036 | struct ovs_key_icmp icmp_key; |
3bffc610 BP |
2037 | |
2038 | icmp_key.icmp_type = icmp_type; | |
2039 | icmp_key.icmp_code = icmp_code; | |
df2c07f4 | 2040 | nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP, |
3bffc610 | 2041 | &icmp_key, sizeof icmp_key); |
e6cc0bab AZ |
2042 | if (mask) { |
2043 | memset(&icmp_key, 0xff, sizeof icmp_key); | |
2044 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMP, &icmp_key, | |
2045 | sizeof icmp_key); | |
2046 | } | |
3bffc610 BP |
2047 | return n; |
2048 | } | |
2049 | } | |
2050 | ||
2051 | { | |
df2c07f4 | 2052 | struct ovs_key_icmpv6 icmpv6_key; |
e6cc0bab AZ |
2053 | struct ovs_key_icmpv6 icmpv6_mask; |
2054 | int icmpv6_type_mask; | |
2055 | int icmpv6_code_mask; | |
3bffc610 BP |
2056 | int n = -1; |
2057 | ||
e6cc0bab AZ |
2058 | if (mask && sscanf(s, "icmpv6(type=%"SCNi8"/%i,code=%"SCNi8"/%i)%n", |
2059 | &icmpv6_key.icmpv6_type, &icmpv6_type_mask, | |
2060 | &icmpv6_key.icmpv6_code, &icmpv6_code_mask, &n) > 0 | |
2061 | && n > 0) { | |
2062 | nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6, | |
2063 | &icmpv6_key, sizeof icmpv6_key); | |
2064 | ||
2065 | icmpv6_mask.icmpv6_type = icmpv6_type_mask; | |
2066 | icmpv6_mask.icmpv6_code = icmpv6_code_mask; | |
2067 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMPV6, &icmpv6_mask, | |
2068 | sizeof icmpv6_mask); | |
2069 | return n; | |
2070 | } else if (sscanf(s, "icmpv6(type=%"SCNi8",code=%"SCNi8")%n", | |
3bffc610 BP |
2071 | &icmpv6_key.icmpv6_type, &icmpv6_key.icmpv6_code,&n) > 0 |
2072 | && n > 0) { | |
df2c07f4 | 2073 | nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6, |
3bffc610 | 2074 | &icmpv6_key, sizeof icmpv6_key); |
e6cc0bab AZ |
2075 | |
2076 | if (mask) { | |
2077 | memset(&icmpv6_key, 0xff, sizeof icmpv6_key); | |
2078 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMPV6, &icmpv6_key, | |
2079 | sizeof icmpv6_key); | |
2080 | } | |
3bffc610 BP |
2081 | return n; |
2082 | } | |
2083 | } | |
2084 | ||
2085 | { | |
e6cc0bab AZ |
2086 | ovs_be32 arp_sip, arp_sip_mask; |
2087 | ovs_be32 arp_tip, arp_tip_mask; | |
2088 | int arp_op, arp_op_mask; | |
3bffc610 | 2089 | uint8_t arp_sha[ETH_ADDR_LEN]; |
e6cc0bab | 2090 | uint8_t arp_sha_mask[ETH_ADDR_LEN]; |
3bffc610 | 2091 | uint8_t arp_tha[ETH_ADDR_LEN]; |
e6cc0bab | 2092 | uint8_t arp_tha_mask[ETH_ADDR_LEN]; |
3bffc610 BP |
2093 | int n = -1; |
2094 | ||
e6cc0bab AZ |
2095 | if (mask && sscanf(s, "arp(sip="IP_SCAN_FMT"/"IP_SCAN_FMT"," |
2096 | "tip="IP_SCAN_FMT"/"IP_SCAN_FMT"," | |
2097 | "op=%i/%i,sha="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"," | |
2098 | "tha="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n", | |
2099 | IP_SCAN_ARGS(&arp_sip), IP_SCAN_ARGS(&arp_sip_mask), | |
2100 | IP_SCAN_ARGS(&arp_tip), IP_SCAN_ARGS(&arp_tip_mask), | |
2101 | &arp_op, &arp_op_mask, | |
2102 | ETH_ADDR_SCAN_ARGS(arp_sha), | |
2103 | ETH_ADDR_SCAN_ARGS(arp_sha_mask), | |
2104 | ETH_ADDR_SCAN_ARGS(arp_tha), | |
2105 | ETH_ADDR_SCAN_ARGS(arp_tha_mask), &n) > 0 && n > 0) { | |
2106 | struct ovs_key_arp arp_key; | |
2107 | struct ovs_key_arp arp_mask; | |
2108 | ||
2109 | memset(&arp_key, 0, sizeof arp_key); | |
2110 | arp_key.arp_sip = arp_sip; | |
2111 | arp_key.arp_tip = arp_tip; | |
2112 | arp_key.arp_op = htons(arp_op); | |
2113 | memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN); | |
2114 | memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN); | |
2115 | nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key); | |
2116 | ||
2117 | arp_mask.arp_sip = arp_sip_mask; | |
2118 | arp_mask.arp_tip = arp_tip_mask; | |
2119 | arp_mask.arp_op = htons(arp_op_mask); | |
2120 | memcpy(arp_mask.arp_sha, arp_sha_mask, ETH_ADDR_LEN); | |
2121 | memcpy(arp_mask.arp_tha, arp_tha_mask, ETH_ADDR_LEN); | |
2122 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_ARP, | |
2123 | &arp_mask, sizeof arp_mask); | |
2124 | return n; | |
2125 | } else if (sscanf(s, "arp(sip="IP_SCAN_FMT",tip="IP_SCAN_FMT"," | |
3bffc610 BP |
2126 | "op=%i,sha="ETH_ADDR_SCAN_FMT",tha="ETH_ADDR_SCAN_FMT")%n", |
2127 | IP_SCAN_ARGS(&arp_sip), | |
2128 | IP_SCAN_ARGS(&arp_tip), | |
2129 | &arp_op, | |
2130 | ETH_ADDR_SCAN_ARGS(arp_sha), | |
2131 | ETH_ADDR_SCAN_ARGS(arp_tha), &n) > 0 && n > 0) { | |
df2c07f4 | 2132 | struct ovs_key_arp arp_key; |
3bffc610 BP |
2133 | |
2134 | memset(&arp_key, 0, sizeof arp_key); | |
2135 | arp_key.arp_sip = arp_sip; | |
2136 | arp_key.arp_tip = arp_tip; | |
2137 | arp_key.arp_op = htons(arp_op); | |
2138 | memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN); | |
2139 | memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN); | |
df2c07f4 | 2140 | nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key); |
e6cc0bab AZ |
2141 | |
2142 | if (mask) { | |
2143 | memset(&arp_key, 0xff, sizeof arp_key); | |
2144 | nl_msg_put_unspec(mask, OVS_KEY_ATTR_ARP, | |
2145 | &arp_key, sizeof arp_key); | |
2146 | } | |
3bffc610 BP |
2147 | return n; |
2148 | } | |
2149 | } | |
2150 | ||
2151 | { | |
2152 | char nd_target_s[IPV6_SCAN_LEN + 1]; | |
e6cc0bab | 2153 | char nd_target_mask_s[IPV6_SCAN_LEN + 1]; |
3bffc610 | 2154 | uint8_t nd_sll[ETH_ADDR_LEN]; |
e6cc0bab | 2155 | uint8_t nd_sll_mask[ETH_ADDR_LEN]; |
3bffc610 | 2156 | uint8_t nd_tll[ETH_ADDR_LEN]; |
e6cc0bab | 2157 | uint8_t nd_tll_mask[ETH_ADDR_LEN]; |
3bffc610 BP |
2158 | int n = -1; |
2159 | ||
dc8c5408 AZ |
2160 | nd_target_mask_s[0] = 0; |
2161 | memset(nd_sll_mask, 0xff, sizeof nd_sll_mask); | |
2162 | memset(nd_tll_mask, 0xff, sizeof nd_tll_mask); | |
e6cc0bab AZ |
2163 | |
2164 | if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT")%n", | |
2165 | nd_target_s, nd_target_mask_s, &n) > 0 && n > 0) { | |
2166 | put_nd_key(n, nd_target_s, NULL, NULL, key); | |
dc8c5408 | 2167 | put_nd_mask(n, nd_target_mask_s, NULL, NULL, mask); |
e6cc0bab | 2168 | } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT")%n", |
3bffc610 | 2169 | nd_target_s, &n) > 0 && n > 0) { |
e6cc0bab AZ |
2170 | put_nd_key(n, nd_target_s, NULL, NULL, key); |
2171 | if (mask) { | |
dc8c5408 | 2172 | put_nd_mask(n, nd_target_mask_s, NULL, NULL, mask); |
e6cc0bab AZ |
2173 | } |
2174 | } else if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT | |
2175 | ",sll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n", | |
2176 | nd_target_s, nd_target_mask_s, | |
2177 | ETH_ADDR_SCAN_ARGS(nd_sll), | |
2178 | ETH_ADDR_SCAN_ARGS(nd_sll_mask), &n) > 0 && n > 0) { | |
2179 | put_nd_key(n, nd_target_s, nd_sll, NULL, key); | |
dc8c5408 | 2180 | put_nd_mask(n, nd_target_mask_s, nd_sll_mask, NULL, mask); |
e6cc0bab | 2181 | } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT")%n", |
3bffc610 BP |
2182 | nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), &n) > 0 |
2183 | && n > 0) { | |
e6cc0bab AZ |
2184 | put_nd_key(n, nd_target_s, nd_sll, NULL, key); |
2185 | if (mask) { | |
dc8c5408 | 2186 | put_nd_mask(n, nd_target_mask_s, nd_sll_mask, NULL, mask); |
e6cc0bab AZ |
2187 | } |
2188 | } else if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT | |
2189 | ",tll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n", | |
2190 | nd_target_s, nd_target_mask_s, | |
2191 | ETH_ADDR_SCAN_ARGS(nd_tll), | |
2192 | ETH_ADDR_SCAN_ARGS(nd_tll_mask), &n) > 0 && n > 0) { | |
2193 | put_nd_key(n, nd_target_s, NULL, nd_tll, key); | |
dc8c5408 | 2194 | put_nd_mask(n, nd_target_mask_s, NULL, nd_tll_mask, mask); |
e6cc0bab | 2195 | } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT",tll="ETH_ADDR_SCAN_FMT")%n", |
3bffc610 BP |
2196 | nd_target_s, ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0 |
2197 | && n > 0) { | |
e6cc0bab AZ |
2198 | put_nd_key(n, nd_target_s, NULL, nd_tll, key); |
2199 | if (mask) { | |
dc8c5408 | 2200 | put_nd_mask(n, nd_target_mask_s, NULL, nd_tll_mask, mask); |
e6cc0bab AZ |
2201 | } |
2202 | } else if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT | |
2203 | ",sll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"," | |
2204 | "tll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n", | |
2205 | nd_target_s, nd_target_mask_s, | |
2206 | ETH_ADDR_SCAN_ARGS(nd_sll), ETH_ADDR_SCAN_ARGS(nd_sll_mask), | |
2207 | ETH_ADDR_SCAN_ARGS(nd_tll), ETH_ADDR_SCAN_ARGS(nd_tll_mask), | |
2208 | &n) > 0 | |
2209 | && n > 0) { | |
2210 | put_nd_key(n, nd_target_s, nd_sll, nd_tll, key); | |
dc8c5408 | 2211 | put_nd_mask(n, nd_target_mask_s, nd_sll_mask, nd_tll_mask, mask); |
e6cc0bab | 2212 | } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT"," |
3bffc610 BP |
2213 | "tll="ETH_ADDR_SCAN_FMT")%n", |
2214 | nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), | |
2215 | ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0 | |
2216 | && n > 0) { | |
e6cc0bab AZ |
2217 | put_nd_key(n, nd_target_s, nd_sll, nd_tll, key); |
2218 | if (mask) { | |
dc8c5408 AZ |
2219 | put_nd_mask(n, nd_target_mask_s, |
2220 | nd_sll_mask, nd_tll_mask, mask); | |
e6cc0bab | 2221 | } |
3bffc610 | 2222 | } |
e6cc0bab AZ |
2223 | |
2224 | if (n != -1) | |
2225 | return n; | |
2226 | ||
3bffc610 BP |
2227 | } |
2228 | ||
fea393b1 BP |
2229 | if (!strncmp(s, "encap(", 6)) { |
2230 | const char *start = s; | |
e6cc0bab | 2231 | size_t encap, encap_mask = 0; |
fea393b1 BP |
2232 | |
2233 | encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP); | |
e6cc0bab AZ |
2234 | if (mask) { |
2235 | encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP); | |
2236 | } | |
fea393b1 BP |
2237 | |
2238 | s += 6; | |
2239 | for (;;) { | |
2240 | int retval; | |
2241 | ||
2242 | s += strspn(s, ", \t\r\n"); | |
2243 | if (!*s) { | |
2244 | return -EINVAL; | |
2245 | } else if (*s == ')') { | |
2246 | break; | |
2247 | } | |
2248 | ||
e6cc0bab | 2249 | retval = parse_odp_key_mask_attr(s, port_names, key, mask); |
fea393b1 BP |
2250 | if (retval < 0) { |
2251 | return retval; | |
2252 | } | |
2253 | s += retval; | |
2254 | } | |
2255 | s++; | |
2256 | ||
2257 | nl_msg_end_nested(key, encap); | |
e6cc0bab AZ |
2258 | if (mask) { |
2259 | nl_msg_end_nested(mask, encap_mask); | |
2260 | } | |
fea393b1 BP |
2261 | |
2262 | return s - start; | |
2263 | } | |
2264 | ||
3bffc610 BP |
2265 | return -EINVAL; |
2266 | } | |
2267 | ||
df2c07f4 JP |
2268 | /* Parses the string representation of a datapath flow key, in the |
2269 | * format output by odp_flow_key_format(). Returns 0 if successful, | |
2270 | * otherwise a positive errno value. On success, the flow key is | |
2271 | * appended to 'key' as a series of Netlink attributes. On failure, no | |
2272 | * data is appended to 'key'. Either way, 'key''s data might be | |
2273 | * reallocated. | |
3bffc610 | 2274 | * |
44bac24b BP |
2275 | * If 'port_names' is nonnull, it points to an simap that maps from a port name |
2276 | * to a port number. (Port names may be used instead of port numbers in | |
2277 | * in_port.) | |
b2a60db8 | 2278 | * |
3bffc610 BP |
2279 | * On success, the attributes appended to 'key' are individually syntactically |
2280 | * valid, but they may not be valid as a sequence. 'key' might, for example, | |
34118cae | 2281 | * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */ |
3bffc610 | 2282 | int |
e6cc0bab AZ |
2283 | odp_flow_from_string(const char *s, const struct simap *port_names, |
2284 | struct ofpbuf *key, struct ofpbuf *mask) | |
3bffc610 BP |
2285 | { |
2286 | const size_t old_size = key->size; | |
2287 | for (;;) { | |
2288 | int retval; | |
2289 | ||
7202cbe5 | 2290 | s += strspn(s, delimiters); |
3bffc610 BP |
2291 | if (!*s) { |
2292 | return 0; | |
2293 | } | |
2294 | ||
e6cc0bab | 2295 | retval = parse_odp_key_mask_attr(s, port_names, key, mask); |
3bffc610 BP |
2296 | if (retval < 0) { |
2297 | key->size = old_size; | |
2298 | return -retval; | |
2299 | } | |
2300 | s += retval; | |
2301 | } | |
2302 | ||
2303 | return 0; | |
2304 | } | |
2305 | ||
7257b535 | 2306 | static uint8_t |
c4f2731d | 2307 | ovs_to_odp_frag(uint8_t nw_frag) |
7257b535 | 2308 | { |
c4f2731d PS |
2309 | return (nw_frag == 0 ? OVS_FRAG_TYPE_NONE |
2310 | : nw_frag == FLOW_NW_FRAG_ANY ? OVS_FRAG_TYPE_FIRST | |
2311 | : OVS_FRAG_TYPE_LATER); | |
7257b535 BP |
2312 | } |
2313 | ||
661cbcd5 JP |
2314 | static void |
2315 | odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *data, | |
2316 | const struct flow *flow, odp_port_t odp_in_port) | |
14608a15 | 2317 | { |
661cbcd5 | 2318 | bool is_mask; |
df2c07f4 | 2319 | struct ovs_key_ethernet *eth_key; |
fea393b1 | 2320 | size_t encap; |
36956a7d | 2321 | |
661cbcd5 JP |
2322 | /* We assume that if 'data' and 'flow' are not the same, we should |
2323 | * treat 'data' as a mask. */ | |
2324 | is_mask = (data != flow); | |
2325 | ||
deedf7e7 | 2326 | if (flow->skb_priority) { |
661cbcd5 | 2327 | nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority); |
abff858b PS |
2328 | } |
2329 | ||
05fb0928 | 2330 | if (flow->tunnel.ip_dst) { |
661cbcd5 | 2331 | tun_key_to_attr(buf, &data->tunnel); |
36956a7d BP |
2332 | } |
2333 | ||
72e8bf28 | 2334 | if (flow->skb_mark) { |
661cbcd5 | 2335 | nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->skb_mark); |
72e8bf28 AA |
2336 | } |
2337 | ||
661cbcd5 JP |
2338 | /* Add an ingress port attribute if this is a mask or 'odp_in_port' |
2339 | * is not the magical value "ODPP_NONE". */ | |
2340 | if (is_mask || odp_in_port != ODPP_NONE) { | |
4e022ec0 | 2341 | nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, odp_in_port); |
18886b60 | 2342 | } |
36956a7d | 2343 | |
df2c07f4 | 2344 | eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET, |
36956a7d | 2345 | sizeof *eth_key); |
661cbcd5 JP |
2346 | memcpy(eth_key->eth_src, data->dl_src, ETH_ADDR_LEN); |
2347 | memcpy(eth_key->eth_dst, data->dl_dst, ETH_ADDR_LEN); | |
36956a7d | 2348 | |
8ddc056d | 2349 | if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) { |
bed7d6a1 JP |
2350 | if (is_mask) { |
2351 | nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(UINT16_MAX)); | |
2352 | } else { | |
2353 | nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN)); | |
2354 | } | |
661cbcd5 | 2355 | nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci); |
fea393b1 | 2356 | encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP); |
8ddc056d BP |
2357 | if (flow->vlan_tci == htons(0)) { |
2358 | goto unencap; | |
2359 | } | |
fea393b1 BP |
2360 | } else { |
2361 | encap = 0; | |
36956a7d BP |
2362 | } |
2363 | ||
2364 | if (ntohs(flow->dl_type) < ETH_TYPE_MIN) { | |
661cbcd5 JP |
2365 | /* For backwards compatibility with kernels that don't support |
2366 | * wildcarding, the following convention is used to encode the | |
2367 | * OVS_KEY_ATTR_ETHERTYPE for key and mask: | |
2368 | * | |
2369 | * key mask matches | |
2370 | * -------- -------- ------- | |
2371 | * >0x5ff 0xffff Specified Ethernet II Ethertype. | |
2372 | * >0x5ff 0 Any Ethernet II or non-Ethernet II frame. | |
2373 | * <none> 0xffff Any non-Ethernet II frame (except valid | |
2374 | * 802.3 SNAP packet with valid eth_type). | |
2375 | */ | |
2376 | if (is_mask) { | |
48432528 | 2377 | nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(UINT16_MAX)); |
661cbcd5 | 2378 | } |
fea393b1 | 2379 | goto unencap; |
36956a7d BP |
2380 | } |
2381 | ||
661cbcd5 | 2382 | nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type); |
36956a7d BP |
2383 | |
2384 | if (flow->dl_type == htons(ETH_TYPE_IP)) { | |
df2c07f4 | 2385 | struct ovs_key_ipv4 *ipv4_key; |
36956a7d | 2386 | |
df2c07f4 | 2387 | ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4, |
36956a7d | 2388 | sizeof *ipv4_key); |
661cbcd5 JP |
2389 | ipv4_key->ipv4_src = data->nw_src; |
2390 | ipv4_key->ipv4_dst = data->nw_dst; | |
2391 | ipv4_key->ipv4_proto = data->nw_proto; | |
2392 | ipv4_key->ipv4_tos = data->nw_tos; | |
2393 | ipv4_key->ipv4_ttl = data->nw_ttl; | |
2394 | ipv4_key->ipv4_frag = ovs_to_odp_frag(data->nw_frag); | |
d31f1109 | 2395 | } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) { |
df2c07f4 | 2396 | struct ovs_key_ipv6 *ipv6_key; |
d31f1109 | 2397 | |
df2c07f4 | 2398 | ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6, |
d31f1109 | 2399 | sizeof *ipv6_key); |
661cbcd5 JP |
2400 | memcpy(ipv6_key->ipv6_src, &data->ipv6_src, sizeof ipv6_key->ipv6_src); |
2401 | memcpy(ipv6_key->ipv6_dst, &data->ipv6_dst, sizeof ipv6_key->ipv6_dst); | |
2402 | ipv6_key->ipv6_label = data->ipv6_label; | |
2403 | ipv6_key->ipv6_proto = data->nw_proto; | |
2404 | ipv6_key->ipv6_tclass = data->nw_tos; | |
2405 | ipv6_key->ipv6_hlimit = data->nw_ttl; | |
eadef313 | 2406 | ipv6_key->ipv6_frag = ovs_to_odp_frag(flow->nw_frag); |
8087f5ff MM |
2407 | } else if (flow->dl_type == htons(ETH_TYPE_ARP) || |
2408 | flow->dl_type == htons(ETH_TYPE_RARP)) { | |
df2c07f4 | 2409 | struct ovs_key_arp *arp_key; |
d31f1109 | 2410 | |
df2c07f4 | 2411 | arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP, |
d31f1109 | 2412 | sizeof *arp_key); |
535d6987 | 2413 | memset(arp_key, 0, sizeof *arp_key); |
661cbcd5 JP |
2414 | arp_key->arp_sip = data->nw_src; |
2415 | arp_key->arp_tip = data->nw_dst; | |
2416 | arp_key->arp_op = htons(data->nw_proto); | |
2417 | memcpy(arp_key->arp_sha, data->arp_sha, ETH_ADDR_LEN); | |
2418 | memcpy(arp_key->arp_tha, data->arp_tha, ETH_ADDR_LEN); | |
d31f1109 | 2419 | } |
b53055f4 | 2420 | |
b02475c5 SH |
2421 | if (flow->mpls_depth) { |
2422 | struct ovs_key_mpls *mpls_key; | |
2423 | ||
2424 | mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS, | |
2425 | sizeof *mpls_key); | |
661cbcd5 | 2426 | mpls_key->mpls_lse = data->mpls_lse; |
b02475c5 SH |
2427 | } |
2428 | ||
48cecbdc | 2429 | if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) { |
6767a2cc | 2430 | if (flow->nw_proto == IPPROTO_TCP) { |
df2c07f4 | 2431 | struct ovs_key_tcp *tcp_key; |
36956a7d | 2432 | |
df2c07f4 | 2433 | tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP, |
36956a7d | 2434 | sizeof *tcp_key); |
661cbcd5 JP |
2435 | tcp_key->tcp_src = data->tp_src; |
2436 | tcp_key->tcp_dst = data->tp_dst; | |
6767a2cc | 2437 | } else if (flow->nw_proto == IPPROTO_UDP) { |
df2c07f4 | 2438 | struct ovs_key_udp *udp_key; |
36956a7d | 2439 | |
df2c07f4 | 2440 | udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP, |
36956a7d | 2441 | sizeof *udp_key); |
661cbcd5 JP |
2442 | udp_key->udp_src = data->tp_src; |
2443 | udp_key->udp_dst = data->tp_dst; | |
d31f1109 JP |
2444 | } else if (flow->dl_type == htons(ETH_TYPE_IP) |
2445 | && flow->nw_proto == IPPROTO_ICMP) { | |
df2c07f4 | 2446 | struct ovs_key_icmp *icmp_key; |
36956a7d | 2447 | |
df2c07f4 | 2448 | icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP, |
36956a7d | 2449 | sizeof *icmp_key); |
661cbcd5 JP |
2450 | icmp_key->icmp_type = ntohs(data->tp_src); |
2451 | icmp_key->icmp_code = ntohs(data->tp_dst); | |
d31f1109 JP |
2452 | } else if (flow->dl_type == htons(ETH_TYPE_IPV6) |
2453 | && flow->nw_proto == IPPROTO_ICMPV6) { | |
df2c07f4 | 2454 | struct ovs_key_icmpv6 *icmpv6_key; |
d31f1109 | 2455 | |
df2c07f4 | 2456 | icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6, |
d31f1109 | 2457 | sizeof *icmpv6_key); |
661cbcd5 JP |
2458 | icmpv6_key->icmpv6_type = ntohs(data->tp_src); |
2459 | icmpv6_key->icmpv6_code = ntohs(data->tp_dst); | |
685a51a5 JP |
2460 | |
2461 | if (icmpv6_key->icmpv6_type == ND_NEIGHBOR_SOLICIT | |
2462 | || icmpv6_key->icmpv6_type == ND_NEIGHBOR_ADVERT) { | |
df2c07f4 | 2463 | struct ovs_key_nd *nd_key; |
685a51a5 | 2464 | |
df2c07f4 | 2465 | nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND, |
685a51a5 | 2466 | sizeof *nd_key); |
661cbcd5 | 2467 | memcpy(nd_key->nd_target, &data->nd_target, |
685a51a5 | 2468 | sizeof nd_key->nd_target); |
661cbcd5 JP |
2469 | memcpy(nd_key->nd_sll, data->arp_sha, ETH_ADDR_LEN); |
2470 | memcpy(nd_key->nd_tll, data->arp_tha, ETH_ADDR_LEN); | |
685a51a5 | 2471 | } |
36956a7d | 2472 | } |
36956a7d | 2473 | } |
fea393b1 BP |
2474 | |
2475 | unencap: | |
2476 | if (encap) { | |
2477 | nl_msg_end_nested(buf, encap); | |
2478 | } | |
36956a7d | 2479 | } |
661cbcd5 JP |
2480 | |
2481 | /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'. | |
2482 | * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port | |
2483 | * number rather than a datapath port number). Instead, if 'odp_in_port' | |
2484 | * is anything other than ODPP_NONE, it is included in 'buf' as the input | |
2485 | * port. | |
2486 | * | |
2487 | * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be | |
2488 | * capable of being expanded to allow for that much space. */ | |
2489 | void | |
2490 | odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow, | |
2491 | odp_port_t odp_in_port) | |
2492 | { | |
2493 | odp_flow_key_from_flow__(buf, flow, flow, odp_in_port); | |
2494 | } | |
2495 | ||
2496 | /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to | |
2497 | * 'buf'. 'flow' is used as a template to determine how to interpret | |
2498 | * 'mask'. For example, the 'dl_type' of 'mask' describes the mask, but | |
2499 | * it doesn't indicate whether the other fields should be interpreted as | |
2500 | * ARP, IPv4, IPv6, etc. | |
2501 | * | |
2502 | * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be | |
2503 | * capable of being expanded to allow for that much space. */ | |
2504 | void | |
2505 | odp_flow_key_from_mask(struct ofpbuf *buf, const struct flow *mask, | |
2506 | const struct flow *flow, uint32_t odp_in_port_mask) | |
2507 | { | |
2508 | odp_flow_key_from_flow__(buf, mask, flow, u32_to_odp(odp_in_port_mask)); | |
2509 | } | |
36956a7d | 2510 | |
b0f7b9b5 BP |
2511 | uint32_t |
2512 | odp_flow_key_hash(const struct nlattr *key, size_t key_len) | |
2513 | { | |
2514 | BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t))); | |
db5a1019 AW |
2515 | return hash_words(ALIGNED_CAST(const uint32_t *, key), |
2516 | key_len / sizeof(uint32_t), 0); | |
b0f7b9b5 BP |
2517 | } |
2518 | ||
34118cae BP |
2519 | static void |
2520 | log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title, | |
b0f7b9b5 | 2521 | uint64_t attrs, int out_of_range_attr, |
34118cae BP |
2522 | const struct nlattr *key, size_t key_len) |
2523 | { | |
2524 | struct ds s; | |
2525 | int i; | |
2526 | ||
b0f7b9b5 | 2527 | if (VLOG_DROP_DBG(rl)) { |
34118cae BP |
2528 | return; |
2529 | } | |
2530 | ||
2531 | ds_init(&s); | |
b0f7b9b5 BP |
2532 | for (i = 0; i < 64; i++) { |
2533 | if (attrs & (UINT64_C(1) << i)) { | |
e6603631 BP |
2534 | char namebuf[OVS_KEY_ATTR_BUFSIZE]; |
2535 | ||
2536 | ds_put_format(&s, " %s", | |
2537 | ovs_key_attr_to_string(i, namebuf, sizeof namebuf)); | |
34118cae BP |
2538 | } |
2539 | } | |
b0f7b9b5 BP |
2540 | if (out_of_range_attr) { |
2541 | ds_put_format(&s, " %d (and possibly others)", out_of_range_attr); | |
2542 | } | |
34118cae BP |
2543 | |
2544 | ds_put_cstr(&s, ": "); | |
2545 | odp_flow_key_format(key, key_len, &s); | |
2546 | ||
b0f7b9b5 | 2547 | VLOG_DBG("%s:%s", title, ds_cstr(&s)); |
34118cae BP |
2548 | ds_destroy(&s); |
2549 | } | |
2550 | ||
7257b535 | 2551 | static bool |
9e44d715 | 2552 | odp_to_ovs_frag(uint8_t odp_frag, struct flow *flow) |
7257b535 | 2553 | { |
34118cae BP |
2554 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); |
2555 | ||
9e44d715 | 2556 | if (odp_frag > OVS_FRAG_TYPE_LATER) { |
b0f7b9b5 | 2557 | VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag); |
7257b535 BP |
2558 | return false; |
2559 | } | |
2560 | ||
7257b535 | 2561 | if (odp_frag != OVS_FRAG_TYPE_NONE) { |
eadef313 | 2562 | flow->nw_frag |= FLOW_NW_FRAG_ANY; |
7257b535 | 2563 | if (odp_frag == OVS_FRAG_TYPE_LATER) { |
eadef313 | 2564 | flow->nw_frag |= FLOW_NW_FRAG_LATER; |
7257b535 BP |
2565 | } |
2566 | } | |
2567 | return true; | |
2568 | } | |
2569 | ||
b0f7b9b5 | 2570 | static bool |
fea393b1 | 2571 | parse_flow_nlattrs(const struct nlattr *key, size_t key_len, |
b0f7b9b5 BP |
2572 | const struct nlattr *attrs[], uint64_t *present_attrsp, |
2573 | int *out_of_range_attrp) | |
36956a7d | 2574 | { |
b0f7b9b5 | 2575 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10); |
36956a7d | 2576 | const struct nlattr *nla; |
34118cae | 2577 | uint64_t present_attrs; |
36956a7d BP |
2578 | size_t left; |
2579 | ||
2aef1214 | 2580 | BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs); |
34118cae | 2581 | present_attrs = 0; |
b0f7b9b5 | 2582 | *out_of_range_attrp = 0; |
36956a7d | 2583 | NL_ATTR_FOR_EACH (nla, left, key, key_len) { |
34118cae BP |
2584 | uint16_t type = nl_attr_type(nla); |
2585 | size_t len = nl_attr_get_size(nla); | |
2586 | int expected_len = odp_flow_key_attr_len(type); | |
2587 | ||
b0f7b9b5 | 2588 | if (len != expected_len && expected_len >= 0) { |
e6603631 BP |
2589 | char namebuf[OVS_KEY_ATTR_BUFSIZE]; |
2590 | ||
b0f7b9b5 | 2591 | VLOG_ERR_RL(&rl, "attribute %s has length %zu but should have " |
e6603631 BP |
2592 | "length %d", ovs_key_attr_to_string(type, namebuf, |
2593 | sizeof namebuf), | |
b0f7b9b5 BP |
2594 | len, expected_len); |
2595 | return false; | |
34118cae BP |
2596 | } |
2597 | ||
2aef1214 | 2598 | if (type > OVS_KEY_ATTR_MAX) { |
b0f7b9b5 BP |
2599 | *out_of_range_attrp = type; |
2600 | } else { | |
2601 | if (present_attrs & (UINT64_C(1) << type)) { | |
e6603631 BP |
2602 | char namebuf[OVS_KEY_ATTR_BUFSIZE]; |
2603 | ||
b0f7b9b5 | 2604 | VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key", |
e6603631 BP |
2605 | ovs_key_attr_to_string(type, |
2606 | namebuf, sizeof namebuf)); | |
b0f7b9b5 BP |
2607 | return false; |
2608 | } | |
2609 | ||
2610 | present_attrs |= UINT64_C(1) << type; | |
2611 | attrs[type] = nla; | |
2612 | } | |
34118cae BP |
2613 | } |
2614 | if (left) { | |
2615 | VLOG_ERR_RL(&rl, "trailing garbage in flow key"); | |
b0f7b9b5 | 2616 | return false; |
34118cae BP |
2617 | } |
2618 | ||
fea393b1 | 2619 | *present_attrsp = present_attrs; |
b0f7b9b5 | 2620 | return true; |
fea393b1 BP |
2621 | } |
2622 | ||
b0f7b9b5 BP |
2623 | static enum odp_key_fitness |
2624 | check_expectations(uint64_t present_attrs, int out_of_range_attr, | |
2625 | uint64_t expected_attrs, | |
fea393b1 BP |
2626 | const struct nlattr *key, size_t key_len) |
2627 | { | |
2628 | uint64_t missing_attrs; | |
2629 | uint64_t extra_attrs; | |
2630 | ||
2631 | missing_attrs = expected_attrs & ~present_attrs; | |
2632 | if (missing_attrs) { | |
b0f7b9b5 BP |
2633 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10); |
2634 | log_odp_key_attributes(&rl, "expected but not present", | |
2635 | missing_attrs, 0, key, key_len); | |
2636 | return ODP_FIT_TOO_LITTLE; | |
fea393b1 BP |
2637 | } |
2638 | ||
2639 | extra_attrs = present_attrs & ~expected_attrs; | |
b0f7b9b5 BP |
2640 | if (extra_attrs || out_of_range_attr) { |
2641 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10); | |
2642 | log_odp_key_attributes(&rl, "present but not expected", | |
2643 | extra_attrs, out_of_range_attr, key, key_len); | |
2644 | return ODP_FIT_TOO_MUCH; | |
fea393b1 BP |
2645 | } |
2646 | ||
b0f7b9b5 | 2647 | return ODP_FIT_PERFECT; |
fea393b1 BP |
2648 | } |
2649 | ||
b0f7b9b5 BP |
2650 | static bool |
2651 | parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1], | |
2652 | uint64_t present_attrs, uint64_t *expected_attrs, | |
2653 | struct flow *flow) | |
fea393b1 BP |
2654 | { |
2655 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
36956a7d | 2656 | |
fea393b1 | 2657 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) { |
34118cae BP |
2658 | flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]); |
2659 | if (ntohs(flow->dl_type) < 1536) { | |
2660 | VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key", | |
2661 | ntohs(flow->dl_type)); | |
b0f7b9b5 | 2662 | return false; |
34118cae | 2663 | } |
b0f7b9b5 | 2664 | *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE; |
34118cae BP |
2665 | } else { |
2666 | flow->dl_type = htons(FLOW_DL_TYPE_NONE); | |
2667 | } | |
b0f7b9b5 BP |
2668 | return true; |
2669 | } | |
2670 | ||
2671 | static enum odp_key_fitness | |
b02475c5 SH |
2672 | parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1], |
2673 | uint64_t present_attrs, int out_of_range_attr, | |
2674 | uint64_t expected_attrs, struct flow *flow, | |
2675 | const struct nlattr *key, size_t key_len) | |
b0f7b9b5 BP |
2676 | { |
2677 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
36956a7d | 2678 | |
b02475c5 | 2679 | if (eth_type_mpls(flow->dl_type)) { |
b02475c5 SH |
2680 | expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS); |
2681 | ||
b02475c5 SH |
2682 | if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS))) { |
2683 | return ODP_FIT_TOO_LITTLE; | |
2684 | } | |
2685 | flow->mpls_lse = nl_attr_get_be32(attrs[OVS_KEY_ATTR_MPLS]); | |
2686 | flow->mpls_depth++; | |
cff78c88 | 2687 | } else if (flow->dl_type == htons(ETH_TYPE_IP)) { |
34118cae | 2688 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4; |
fea393b1 | 2689 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) { |
34118cae | 2690 | const struct ovs_key_ipv4 *ipv4_key; |
36956a7d | 2691 | |
34118cae | 2692 | ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]); |
36956a7d BP |
2693 | flow->nw_src = ipv4_key->ipv4_src; |
2694 | flow->nw_dst = ipv4_key->ipv4_dst; | |
2695 | flow->nw_proto = ipv4_key->ipv4_proto; | |
eadef313 | 2696 | flow->nw_tos = ipv4_key->ipv4_tos; |
a61680c6 | 2697 | flow->nw_ttl = ipv4_key->ipv4_ttl; |
9e44d715 | 2698 | if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) { |
b0f7b9b5 | 2699 | return ODP_FIT_ERROR; |
36956a7d | 2700 | } |
34118cae | 2701 | } |
cff78c88 | 2702 | } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) { |
34118cae | 2703 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6; |
fea393b1 | 2704 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) { |
34118cae | 2705 | const struct ovs_key_ipv6 *ipv6_key; |
36956a7d | 2706 | |
34118cae | 2707 | ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]); |
d31f1109 JP |
2708 | memcpy(&flow->ipv6_src, ipv6_key->ipv6_src, sizeof flow->ipv6_src); |
2709 | memcpy(&flow->ipv6_dst, ipv6_key->ipv6_dst, sizeof flow->ipv6_dst); | |
fa8223b7 | 2710 | flow->ipv6_label = ipv6_key->ipv6_label; |
d31f1109 | 2711 | flow->nw_proto = ipv6_key->ipv6_proto; |
eadef313 | 2712 | flow->nw_tos = ipv6_key->ipv6_tclass; |
a61680c6 | 2713 | flow->nw_ttl = ipv6_key->ipv6_hlimit; |
9e44d715 | 2714 | if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) { |
b0f7b9b5 | 2715 | return ODP_FIT_ERROR; |
d31f1109 | 2716 | } |
34118cae | 2717 | } |
cff78c88 SH |
2718 | } else if (flow->dl_type == htons(ETH_TYPE_ARP) || |
2719 | flow->dl_type == htons(ETH_TYPE_RARP)) { | |
34118cae | 2720 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP; |
fea393b1 | 2721 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) { |
34118cae | 2722 | const struct ovs_key_arp *arp_key; |
d31f1109 | 2723 | |
34118cae | 2724 | arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]); |
36956a7d BP |
2725 | flow->nw_src = arp_key->arp_sip; |
2726 | flow->nw_dst = arp_key->arp_tip; | |
2727 | if (arp_key->arp_op & htons(0xff00)) { | |
34118cae BP |
2728 | VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow " |
2729 | "key", ntohs(arp_key->arp_op)); | |
b0f7b9b5 | 2730 | return ODP_FIT_ERROR; |
36956a7d BP |
2731 | } |
2732 | flow->nw_proto = ntohs(arp_key->arp_op); | |
bad68a99 JP |
2733 | memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN); |
2734 | memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN); | |
36956a7d | 2735 | } |
36956a7d | 2736 | } |
36956a7d | 2737 | |
34118cae | 2738 | if (flow->nw_proto == IPPROTO_TCP |
cff78c88 SH |
2739 | && (flow->dl_type == htons(ETH_TYPE_IP) || |
2740 | flow->dl_type == htons(ETH_TYPE_IPV6)) | |
34118cae BP |
2741 | && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) { |
2742 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP; | |
fea393b1 | 2743 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) { |
34118cae | 2744 | const struct ovs_key_tcp *tcp_key; |
36956a7d | 2745 | |
34118cae BP |
2746 | tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]); |
2747 | flow->tp_src = tcp_key->tcp_src; | |
2748 | flow->tp_dst = tcp_key->tcp_dst; | |
7257b535 | 2749 | } |
34118cae | 2750 | } else if (flow->nw_proto == IPPROTO_UDP |
cff78c88 SH |
2751 | && (flow->dl_type == htons(ETH_TYPE_IP) || |
2752 | flow->dl_type == htons(ETH_TYPE_IPV6)) | |
34118cae BP |
2753 | && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) { |
2754 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP; | |
fea393b1 | 2755 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) { |
34118cae BP |
2756 | const struct ovs_key_udp *udp_key; |
2757 | ||
2758 | udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]); | |
2759 | flow->tp_src = udp_key->udp_src; | |
2760 | flow->tp_dst = udp_key->udp_dst; | |
d31f1109 | 2761 | } |
34118cae | 2762 | } else if (flow->nw_proto == IPPROTO_ICMP |
cff78c88 | 2763 | && flow->dl_type == htons(ETH_TYPE_IP) |
34118cae BP |
2764 | && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) { |
2765 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP; | |
fea393b1 | 2766 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) { |
34118cae BP |
2767 | const struct ovs_key_icmp *icmp_key; |
2768 | ||
2769 | icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]); | |
2770 | flow->tp_src = htons(icmp_key->icmp_type); | |
2771 | flow->tp_dst = htons(icmp_key->icmp_code); | |
685a51a5 | 2772 | } |
34118cae | 2773 | } else if (flow->nw_proto == IPPROTO_ICMPV6 |
cff78c88 | 2774 | && flow->dl_type == htons(ETH_TYPE_IPV6) |
34118cae BP |
2775 | && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) { |
2776 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6; | |
fea393b1 | 2777 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) { |
34118cae BP |
2778 | const struct ovs_key_icmpv6 *icmpv6_key; |
2779 | ||
2780 | icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]); | |
2781 | flow->tp_src = htons(icmpv6_key->icmpv6_type); | |
2782 | flow->tp_dst = htons(icmpv6_key->icmpv6_code); | |
685a51a5 | 2783 | |
34118cae BP |
2784 | if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) || |
2785 | flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) { | |
2786 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND; | |
fea393b1 | 2787 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) { |
34118cae BP |
2788 | const struct ovs_key_nd *nd_key; |
2789 | ||
2790 | nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]); | |
2791 | memcpy(&flow->nd_target, nd_key->nd_target, | |
2792 | sizeof flow->nd_target); | |
2793 | memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN); | |
2794 | memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN); | |
2795 | } | |
2796 | } | |
7257b535 | 2797 | } |
34118cae | 2798 | } |
7257b535 | 2799 | |
b0f7b9b5 BP |
2800 | return check_expectations(present_attrs, out_of_range_attr, expected_attrs, |
2801 | key, key_len); | |
2802 | } | |
2803 | ||
2804 | /* Parse 802.1Q header then encapsulated L3 attributes. */ | |
2805 | static enum odp_key_fitness | |
2806 | parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1], | |
2807 | uint64_t present_attrs, int out_of_range_attr, | |
2808 | uint64_t expected_attrs, struct flow *flow, | |
2809 | const struct nlattr *key, size_t key_len) | |
2810 | { | |
2811 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
2812 | ||
2813 | const struct nlattr *encap | |
2814 | = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP) | |
2815 | ? attrs[OVS_KEY_ATTR_ENCAP] : NULL); | |
2816 | enum odp_key_fitness encap_fitness; | |
2817 | enum odp_key_fitness fitness; | |
2818 | ovs_be16 tci; | |
2819 | ||
ec9f40dc | 2820 | /* Calculate fitness of outer attributes. */ |
b0f7b9b5 BP |
2821 | expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) | |
2822 | (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)); | |
2823 | fitness = check_expectations(present_attrs, out_of_range_attr, | |
2824 | expected_attrs, key, key_len); | |
2825 | ||
2826 | /* Get the VLAN TCI value. */ | |
2827 | if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) { | |
2828 | return ODP_FIT_TOO_LITTLE; | |
2829 | } | |
2830 | tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]); | |
2831 | if (tci == htons(0)) { | |
2832 | /* Corner case for a truncated 802.1Q header. */ | |
2833 | if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) { | |
2834 | return ODP_FIT_TOO_MUCH; | |
2835 | } | |
2836 | return fitness; | |
2837 | } else if (!(tci & htons(VLAN_CFI))) { | |
2838 | VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero " | |
2839 | "but CFI bit is not set", ntohs(tci)); | |
2840 | return ODP_FIT_ERROR; | |
2841 | } | |
2842 | ||
2843 | /* Set vlan_tci. | |
2844 | * Remove the TPID from dl_type since it's not the real Ethertype. */ | |
2845 | flow->vlan_tci = tci; | |
2846 | flow->dl_type = htons(0); | |
2847 | ||
2848 | /* Now parse the encapsulated attributes. */ | |
2849 | if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap), | |
2850 | attrs, &present_attrs, &out_of_range_attr)) { | |
2851 | return ODP_FIT_ERROR; | |
2852 | } | |
2853 | expected_attrs = 0; | |
2854 | ||
2855 | if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) { | |
2856 | return ODP_FIT_ERROR; | |
2857 | } | |
b02475c5 SH |
2858 | encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr, |
2859 | expected_attrs, flow, key, key_len); | |
b0f7b9b5 BP |
2860 | |
2861 | /* The overall fitness is the worse of the outer and inner attributes. */ | |
2862 | return MAX(fitness, encap_fitness); | |
2863 | } | |
2864 | ||
2865 | /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow | |
2866 | * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well | |
2867 | * 'key' fits our expectations for what a flow key should contain. | |
2868 | * | |
ddbfda84 JP |
2869 | * The 'in_port' will be the datapath's understanding of the port. The |
2870 | * caller will need to translate with odp_port_to_ofp_port() if the | |
2871 | * OpenFlow port is needed. | |
2872 | * | |
b0f7b9b5 BP |
2873 | * This function doesn't take the packet itself as an argument because none of |
2874 | * the currently understood OVS_KEY_ATTR_* attributes require it. Currently, | |
2875 | * it is always possible to infer which additional attribute(s) should appear | |
2876 | * by looking at the attributes for lower-level protocols, e.g. if the network | |
2877 | * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we | |
2878 | * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it | |
2879 | * must be absent. */ | |
2880 | enum odp_key_fitness | |
2881 | odp_flow_key_to_flow(const struct nlattr *key, size_t key_len, | |
2882 | struct flow *flow) | |
2883 | { | |
b0f7b9b5 BP |
2884 | const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1]; |
2885 | uint64_t expected_attrs; | |
2886 | uint64_t present_attrs; | |
2887 | int out_of_range_attr; | |
2888 | ||
2889 | memset(flow, 0, sizeof *flow); | |
2890 | ||
2891 | /* Parse attributes. */ | |
2892 | if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs, | |
2893 | &out_of_range_attr)) { | |
2894 | return ODP_FIT_ERROR; | |
2895 | } | |
2896 | expected_attrs = 0; | |
2897 | ||
2898 | /* Metadata. */ | |
2899 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) { | |
deedf7e7 | 2900 | flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]); |
b0f7b9b5 BP |
2901 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY; |
2902 | } | |
2903 | ||
72e8bf28 AA |
2904 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) { |
2905 | flow->skb_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]); | |
2906 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK; | |
2907 | } | |
2908 | ||
9b405f1a PS |
2909 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) { |
2910 | enum odp_key_fitness res; | |
05fb0928 | 2911 | |
617e10e7 | 2912 | res = odp_tun_key_from_attr(attrs[OVS_KEY_ATTR_TUNNEL], &flow->tunnel); |
9b405f1a PS |
2913 | if (res == ODP_FIT_ERROR) { |
2914 | return ODP_FIT_ERROR; | |
2915 | } else if (res == ODP_FIT_PERFECT) { | |
2916 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL; | |
05fb0928 JR |
2917 | } |
2918 | } | |
2919 | ||
b0f7b9b5 | 2920 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) { |
4e022ec0 AW |
2921 | flow->in_port.odp_port |
2922 | = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]); | |
b0f7b9b5 BP |
2923 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT; |
2924 | } else { | |
4e022ec0 | 2925 | flow->in_port.odp_port = ODPP_NONE; |
b0f7b9b5 BP |
2926 | } |
2927 | ||
2928 | /* Ethernet header. */ | |
2929 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) { | |
2930 | const struct ovs_key_ethernet *eth_key; | |
2931 | ||
2932 | eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]); | |
2933 | memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN); | |
2934 | memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN); | |
2935 | } | |
2936 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET; | |
2937 | ||
2938 | /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */ | |
2939 | if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) { | |
2940 | return ODP_FIT_ERROR; | |
2941 | } | |
2942 | ||
2943 | if (flow->dl_type == htons(ETH_TYPE_VLAN)) { | |
2944 | return parse_8021q_onward(attrs, present_attrs, out_of_range_attr, | |
2945 | expected_attrs, flow, key, key_len); | |
2946 | } | |
b02475c5 SH |
2947 | return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr, |
2948 | expected_attrs, flow, key, key_len); | |
14608a15 | 2949 | } |
39db78a0 | 2950 | |
6814e51f BP |
2951 | /* Returns 'fitness' as a string, for use in debug messages. */ |
2952 | const char * | |
2953 | odp_key_fitness_to_string(enum odp_key_fitness fitness) | |
2954 | { | |
2955 | switch (fitness) { | |
2956 | case ODP_FIT_PERFECT: | |
2957 | return "OK"; | |
2958 | case ODP_FIT_TOO_MUCH: | |
2959 | return "too_much"; | |
2960 | case ODP_FIT_TOO_LITTLE: | |
2961 | return "too_little"; | |
2962 | case ODP_FIT_ERROR: | |
2963 | return "error"; | |
2964 | default: | |
2965 | return "<unknown>"; | |
2966 | } | |
2967 | } | |
2968 | ||
39db78a0 | 2969 | /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies |
e995e3df BP |
2970 | * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute |
2971 | * whose contents are the 'userdata_size' bytes at 'userdata' and returns the | |
2972 | * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is | |
2973 | * null, then the return value is not meaningful.) */ | |
39db78a0 | 2974 | size_t |
e995e3df BP |
2975 | odp_put_userspace_action(uint32_t pid, |
2976 | const void *userdata, size_t userdata_size, | |
39db78a0 BP |
2977 | struct ofpbuf *odp_actions) |
2978 | { | |
e995e3df | 2979 | size_t userdata_ofs; |
39db78a0 BP |
2980 | size_t offset; |
2981 | ||
2982 | offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE); | |
2983 | nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid); | |
e995e3df BP |
2984 | if (userdata) { |
2985 | userdata_ofs = odp_actions->size + NLA_HDRLEN; | |
39db78a0 | 2986 | nl_msg_put_unspec(odp_actions, OVS_USERSPACE_ATTR_USERDATA, |
e995e3df BP |
2987 | userdata, userdata_size); |
2988 | } else { | |
2989 | userdata_ofs = 0; | |
39db78a0 BP |
2990 | } |
2991 | nl_msg_end_nested(odp_actions, offset); | |
2992 | ||
e995e3df | 2993 | return userdata_ofs; |
39db78a0 | 2994 | } |
b9ad7294 EJ |
2995 | |
2996 | void | |
2997 | odp_put_tunnel_action(const struct flow_tnl *tunnel, | |
2998 | struct ofpbuf *odp_actions) | |
2999 | { | |
3000 | size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET); | |
3001 | tun_key_to_attr(odp_actions, tunnel); | |
3002 | nl_msg_end_nested(odp_actions, offset); | |
3003 | } | |
5bbda0aa EJ |
3004 | \f |
3005 | /* The commit_odp_actions() function and its helpers. */ | |
3006 | ||
3007 | static void | |
3008 | commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type, | |
3009 | const void *key, size_t key_size) | |
3010 | { | |
3011 | size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET); | |
3012 | nl_msg_put_unspec(odp_actions, key_type, key, key_size); | |
3013 | nl_msg_end_nested(odp_actions, offset); | |
3014 | } | |
3015 | ||
09a0d2e7 AA |
3016 | void |
3017 | odp_put_skb_mark_action(const uint32_t skb_mark, | |
3018 | struct ofpbuf *odp_actions) | |
3019 | { | |
3020 | commit_set_action(odp_actions, OVS_KEY_ATTR_SKB_MARK, &skb_mark, | |
3021 | sizeof(skb_mark)); | |
3022 | } | |
3023 | ||
b9ad7294 EJ |
3024 | /* If any of the flow key data that ODP actions can modify are different in |
3025 | * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to | |
3026 | * 'odp_actions' that change the flow tunneling information in key from | |
3027 | * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the | |
3028 | * same way. In other words, operates the same as commit_odp_actions(), but | |
3029 | * only on tunneling information. */ | |
3030 | void | |
3031 | commit_odp_tunnel_action(const struct flow *flow, struct flow *base, | |
5bbda0aa EJ |
3032 | struct ofpbuf *odp_actions) |
3033 | { | |
05fb0928 JR |
3034 | /* A valid IPV4_TUNNEL must have non-zero ip_dst. */ |
3035 | if (flow->tunnel.ip_dst) { | |
fc80de30 JR |
3036 | if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) { |
3037 | return; | |
3038 | } | |
3039 | memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel); | |
b9ad7294 | 3040 | odp_put_tunnel_action(&base->tunnel, odp_actions); |
05fb0928 | 3041 | } |
5bbda0aa EJ |
3042 | } |
3043 | ||
3044 | static void | |
3045 | commit_set_ether_addr_action(const struct flow *flow, struct flow *base, | |
1dd35f8a JP |
3046 | struct ofpbuf *odp_actions, |
3047 | struct flow_wildcards *wc) | |
5bbda0aa EJ |
3048 | { |
3049 | struct ovs_key_ethernet eth_key; | |
3050 | ||
3051 | if (eth_addr_equals(base->dl_src, flow->dl_src) && | |
3052 | eth_addr_equals(base->dl_dst, flow->dl_dst)) { | |
3053 | return; | |
3054 | } | |
3055 | ||
1dd35f8a JP |
3056 | memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src); |
3057 | memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst); | |
3058 | ||
5bbda0aa EJ |
3059 | memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN); |
3060 | memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN); | |
3061 | ||
3062 | memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN); | |
3063 | memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN); | |
3064 | ||
3065 | commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET, | |
3066 | ð_key, sizeof(eth_key)); | |
3067 | } | |
3068 | ||
3069 | static void | |
3070 | commit_vlan_action(const struct flow *flow, struct flow *base, | |
1dd35f8a | 3071 | struct ofpbuf *odp_actions, struct flow_wildcards *wc) |
5bbda0aa EJ |
3072 | { |
3073 | if (base->vlan_tci == flow->vlan_tci) { | |
3074 | return; | |
3075 | } | |
3076 | ||
1dd35f8a JP |
3077 | memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci); |
3078 | ||
5bbda0aa EJ |
3079 | if (base->vlan_tci & htons(VLAN_CFI)) { |
3080 | nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN); | |
3081 | } | |
3082 | ||
3083 | if (flow->vlan_tci & htons(VLAN_CFI)) { | |
3084 | struct ovs_action_push_vlan vlan; | |
3085 | ||
3086 | vlan.vlan_tpid = htons(ETH_TYPE_VLAN); | |
3087 | vlan.vlan_tci = flow->vlan_tci; | |
3088 | nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN, | |
3089 | &vlan, sizeof vlan); | |
3090 | } | |
3091 | base->vlan_tci = flow->vlan_tci; | |
3092 | } | |
3093 | ||
b02475c5 SH |
3094 | static void |
3095 | commit_mpls_action(const struct flow *flow, struct flow *base, | |
1dd35f8a | 3096 | struct ofpbuf *odp_actions, struct flow_wildcards *wc) |
b02475c5 SH |
3097 | { |
3098 | if (flow->mpls_lse == base->mpls_lse && | |
3099 | flow->mpls_depth == base->mpls_depth) { | |
3100 | return; | |
3101 | } | |
3102 | ||
1dd35f8a JP |
3103 | memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse); |
3104 | ||
b02475c5 SH |
3105 | if (flow->mpls_depth < base->mpls_depth) { |
3106 | if (base->mpls_depth - flow->mpls_depth > 1) { | |
3107 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10); | |
3108 | VLOG_WARN_RL(&rl, "Multiple mpls_pop actions reduced to " | |
3109 | " a single mpls_pop action"); | |
3110 | } | |
3111 | ||
3112 | nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, flow->dl_type); | |
3113 | } else if (flow->mpls_depth > base->mpls_depth) { | |
3114 | struct ovs_action_push_mpls *mpls; | |
3115 | ||
3116 | if (flow->mpls_depth - base->mpls_depth > 1) { | |
3117 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10); | |
3118 | VLOG_WARN_RL(&rl, "Multiple mpls_push actions reduced to " | |
3119 | " a single mpls_push action"); | |
3120 | } | |
3121 | ||
3122 | mpls = nl_msg_put_unspec_uninit(odp_actions, OVS_ACTION_ATTR_PUSH_MPLS, | |
3123 | sizeof *mpls); | |
3124 | memset(mpls, 0, sizeof *mpls); | |
3125 | mpls->mpls_ethertype = flow->dl_type; | |
3126 | mpls->mpls_lse = flow->mpls_lse; | |
3127 | } else { | |
3128 | struct ovs_key_mpls mpls_key; | |
3129 | ||
20131cef | 3130 | mpls_key.mpls_lse = flow->mpls_lse; |
b02475c5 SH |
3131 | commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS, |
3132 | &mpls_key, sizeof(mpls_key)); | |
3133 | } | |
3134 | ||
3135 | base->dl_type = flow->dl_type; | |
3136 | base->mpls_lse = flow->mpls_lse; | |
3137 | base->mpls_depth = flow->mpls_depth; | |
3138 | } | |
3139 | ||
5bbda0aa | 3140 | static void |
c4f2731d | 3141 | commit_set_ipv4_action(const struct flow *flow, struct flow *base, |
1dd35f8a | 3142 | struct ofpbuf *odp_actions, struct flow_wildcards *wc) |
5bbda0aa EJ |
3143 | { |
3144 | struct ovs_key_ipv4 ipv4_key; | |
3145 | ||
5bbda0aa EJ |
3146 | if (base->nw_src == flow->nw_src && |
3147 | base->nw_dst == flow->nw_dst && | |
3148 | base->nw_tos == flow->nw_tos && | |
3149 | base->nw_ttl == flow->nw_ttl && | |
3150 | base->nw_frag == flow->nw_frag) { | |
3151 | return; | |
3152 | } | |
3153 | ||
1dd35f8a JP |
3154 | memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src); |
3155 | memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst); | |
3156 | memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos); | |
3157 | memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl); | |
3158 | memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto); | |
3159 | memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag); | |
3160 | ||
5bbda0aa EJ |
3161 | ipv4_key.ipv4_src = base->nw_src = flow->nw_src; |
3162 | ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst; | |
3163 | ipv4_key.ipv4_tos = base->nw_tos = flow->nw_tos; | |
3164 | ipv4_key.ipv4_ttl = base->nw_ttl = flow->nw_ttl; | |
3165 | ipv4_key.ipv4_proto = base->nw_proto; | |
c4f2731d | 3166 | ipv4_key.ipv4_frag = ovs_to_odp_frag(base->nw_frag); |
5bbda0aa EJ |
3167 | |
3168 | commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4, | |
3169 | &ipv4_key, sizeof(ipv4_key)); | |
3170 | } | |
3171 | ||
c4f2731d PS |
3172 | static void |
3173 | commit_set_ipv6_action(const struct flow *flow, struct flow *base, | |
1dd35f8a | 3174 | struct ofpbuf *odp_actions, struct flow_wildcards *wc) |
c4f2731d PS |
3175 | { |
3176 | struct ovs_key_ipv6 ipv6_key; | |
3177 | ||
3178 | if (ipv6_addr_equals(&base->ipv6_src, &flow->ipv6_src) && | |
3179 | ipv6_addr_equals(&base->ipv6_dst, &flow->ipv6_dst) && | |
3180 | base->ipv6_label == flow->ipv6_label && | |
3181 | base->nw_tos == flow->nw_tos && | |
3182 | base->nw_ttl == flow->nw_ttl && | |
3183 | base->nw_frag == flow->nw_frag) { | |
3184 | return; | |
3185 | } | |
3186 | ||
1dd35f8a JP |
3187 | memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src); |
3188 | memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst); | |
3189 | memset(&wc->masks.ipv6_label, 0xff, sizeof wc->masks.ipv6_label); | |
3190 | memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos); | |
3191 | memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl); | |
3192 | memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto); | |
3193 | memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag); | |
3194 | ||
c4f2731d PS |
3195 | base->ipv6_src = flow->ipv6_src; |
3196 | memcpy(&ipv6_key.ipv6_src, &base->ipv6_src, sizeof(ipv6_key.ipv6_src)); | |
3197 | base->ipv6_dst = flow->ipv6_dst; | |
3198 | memcpy(&ipv6_key.ipv6_dst, &base->ipv6_dst, sizeof(ipv6_key.ipv6_dst)); | |
3199 | ||
3200 | ipv6_key.ipv6_label = base->ipv6_label = flow->ipv6_label; | |
3201 | ipv6_key.ipv6_tclass = base->nw_tos = flow->nw_tos; | |
3202 | ipv6_key.ipv6_hlimit = base->nw_ttl = flow->nw_ttl; | |
3203 | ipv6_key.ipv6_proto = base->nw_proto; | |
3204 | ipv6_key.ipv6_frag = ovs_to_odp_frag(base->nw_frag); | |
3205 | ||
3206 | commit_set_action(odp_actions, OVS_KEY_ATTR_IPV6, | |
3207 | &ipv6_key, sizeof(ipv6_key)); | |
3208 | } | |
3209 | ||
3210 | static void | |
3211 | commit_set_nw_action(const struct flow *flow, struct flow *base, | |
1dd35f8a | 3212 | struct ofpbuf *odp_actions, struct flow_wildcards *wc) |
c4f2731d PS |
3213 | { |
3214 | /* Check if flow really have an IP header. */ | |
3215 | if (!flow->nw_proto) { | |
3216 | return; | |
3217 | } | |
3218 | ||
1b035ef2 | 3219 | if (base->dl_type == htons(ETH_TYPE_IP)) { |
1dd35f8a | 3220 | commit_set_ipv4_action(flow, base, odp_actions, wc); |
1b035ef2 | 3221 | } else if (base->dl_type == htons(ETH_TYPE_IPV6)) { |
1dd35f8a | 3222 | commit_set_ipv6_action(flow, base, odp_actions, wc); |
c4f2731d PS |
3223 | } |
3224 | } | |
3225 | ||
5bbda0aa EJ |
3226 | static void |
3227 | commit_set_port_action(const struct flow *flow, struct flow *base, | |
1dd35f8a | 3228 | struct ofpbuf *odp_actions, struct flow_wildcards *wc) |
5bbda0aa | 3229 | { |
1b035ef2 | 3230 | if (!is_ip_any(base) || (!base->tp_src && !base->tp_dst)) { |
5bbda0aa EJ |
3231 | return; |
3232 | } | |
3233 | ||
3234 | if (base->tp_src == flow->tp_src && | |
3235 | base->tp_dst == flow->tp_dst) { | |
3236 | return; | |
3237 | } | |
3238 | ||
1dd35f8a JP |
3239 | memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src); |
3240 | memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst); | |
3241 | ||
5bbda0aa EJ |
3242 | if (flow->nw_proto == IPPROTO_TCP) { |
3243 | struct ovs_key_tcp port_key; | |
3244 | ||
3245 | port_key.tcp_src = base->tp_src = flow->tp_src; | |
3246 | port_key.tcp_dst = base->tp_dst = flow->tp_dst; | |
3247 | ||
3248 | commit_set_action(odp_actions, OVS_KEY_ATTR_TCP, | |
3249 | &port_key, sizeof(port_key)); | |
3250 | ||
3251 | } else if (flow->nw_proto == IPPROTO_UDP) { | |
3252 | struct ovs_key_udp port_key; | |
3253 | ||
3254 | port_key.udp_src = base->tp_src = flow->tp_src; | |
3255 | port_key.udp_dst = base->tp_dst = flow->tp_dst; | |
3256 | ||
3257 | commit_set_action(odp_actions, OVS_KEY_ATTR_UDP, | |
3258 | &port_key, sizeof(port_key)); | |
3259 | } | |
3260 | } | |
3261 | ||
3262 | static void | |
3263 | commit_set_priority_action(const struct flow *flow, struct flow *base, | |
1dd35f8a JP |
3264 | struct ofpbuf *odp_actions, |
3265 | struct flow_wildcards *wc) | |
5bbda0aa | 3266 | { |
deedf7e7 | 3267 | if (base->skb_priority == flow->skb_priority) { |
5bbda0aa EJ |
3268 | return; |
3269 | } | |
1dd35f8a JP |
3270 | |
3271 | memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority); | |
deedf7e7 | 3272 | base->skb_priority = flow->skb_priority; |
5bbda0aa EJ |
3273 | |
3274 | commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY, | |
deedf7e7 | 3275 | &base->skb_priority, sizeof(base->skb_priority)); |
5bbda0aa EJ |
3276 | } |
3277 | ||
72e8bf28 AA |
3278 | static void |
3279 | commit_set_skb_mark_action(const struct flow *flow, struct flow *base, | |
1dd35f8a JP |
3280 | struct ofpbuf *odp_actions, |
3281 | struct flow_wildcards *wc) | |
72e8bf28 AA |
3282 | { |
3283 | if (base->skb_mark == flow->skb_mark) { | |
3284 | return; | |
3285 | } | |
1dd35f8a JP |
3286 | |
3287 | memset(&wc->masks.skb_mark, 0xff, sizeof wc->masks.skb_mark); | |
72e8bf28 AA |
3288 | base->skb_mark = flow->skb_mark; |
3289 | ||
09a0d2e7 | 3290 | odp_put_skb_mark_action(base->skb_mark, odp_actions); |
72e8bf28 | 3291 | } |
5bbda0aa EJ |
3292 | /* If any of the flow key data that ODP actions can modify are different in |
3293 | * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow | |
b9ad7294 EJ |
3294 | * key from 'base' into 'flow', and then changes 'base' the same way. Does not |
3295 | * commit set_tunnel actions. Users should call commit_odp_tunnel_action() | |
1dd35f8a JP |
3296 | * in addition to this function if needed. Sets fields in 'wc' that are |
3297 | * used as part of the action. */ | |
5bbda0aa EJ |
3298 | void |
3299 | commit_odp_actions(const struct flow *flow, struct flow *base, | |
1dd35f8a | 3300 | struct ofpbuf *odp_actions, struct flow_wildcards *wc) |
5bbda0aa | 3301 | { |
1dd35f8a JP |
3302 | commit_set_ether_addr_action(flow, base, odp_actions, wc); |
3303 | commit_vlan_action(flow, base, odp_actions, wc); | |
3304 | commit_set_nw_action(flow, base, odp_actions, wc); | |
3305 | commit_set_port_action(flow, base, odp_actions, wc); | |
ec9f40dc | 3306 | /* Committing MPLS actions should occur after committing nw and port |
1b035ef2 SH |
3307 | * actions. This is because committing MPLS actions may alter a packet so |
3308 | * that it is no longer IP and thus nw and port actions are no longer valid. | |
3309 | */ | |
1dd35f8a JP |
3310 | commit_mpls_action(flow, base, odp_actions, wc); |
3311 | commit_set_priority_action(flow, base, odp_actions, wc); | |
3312 | commit_set_skb_mark_action(flow, base, odp_actions, wc); | |
5bbda0aa | 3313 | } |