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