]>
Commit | Line | Data |
---|---|---|
18ebd48c PB |
1 | /* |
2 | * Copyright (c) 2016 Mellanox Technologies, Ltd. | |
3 | * | |
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: | |
7 | * | |
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. | |
15 | */ | |
16 | ||
17 | #include <config.h> | |
18 | #include "netdev-tc-offloads.h" | |
ef3767f5 | 19 | |
18ebd48c PB |
20 | #include <errno.h> |
21 | #include <linux/if_ether.h> | |
ef3767f5 JS |
22 | |
23 | #include "dpif.h" | |
24 | #include "hash.h" | |
18ebd48c PB |
25 | #include "openvswitch/hmap.h" |
26 | #include "openvswitch/match.h" | |
27 | #include "openvswitch/ofpbuf.h" | |
28 | #include "openvswitch/thread.h" | |
29 | #include "openvswitch/types.h" | |
30 | #include "openvswitch/vlog.h" | |
ef3767f5 | 31 | #include "netdev-linux.h" |
18ebd48c PB |
32 | #include "netlink.h" |
33 | #include "netlink-socket.h" | |
34 | #include "odp-netlink.h" | |
ef3767f5 | 35 | #include "tc.h" |
18ebd48c PB |
36 | #include "unaligned.h" |
37 | #include "util.h" | |
18ebd48c PB |
38 | |
39 | VLOG_DEFINE_THIS_MODULE(netdev_tc_offloads); | |
40 | ||
8140a5ff PB |
41 | static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(60, 5); |
42 | ||
9116730d PB |
43 | static struct hmap ufid_tc = HMAP_INITIALIZER(&ufid_tc); |
44 | static struct ovs_mutex ufid_lock = OVS_MUTEX_INITIALIZER; | |
45 | ||
46 | /** | |
47 | * struct ufid_tc_data - data entry for ufid_tc hmap. | |
48 | * @ufid_node: Element in @ufid_tc hash table by ufid key. | |
49 | * @tc_node: Element in @ufid_tc hash table by prio/handle/ifindex key. | |
50 | * @ufid: ufid assigned to the flow | |
51 | * @prio: tc priority | |
52 | * @handle: tc handle | |
53 | * @ifindex: netdev ifindex. | |
54 | * @netdev: netdev associated with the tc rule | |
55 | */ | |
56 | struct ufid_tc_data { | |
57 | struct hmap_node ufid_node; | |
58 | struct hmap_node tc_node; | |
59 | ovs_u128 ufid; | |
60 | uint16_t prio; | |
61 | uint32_t handle; | |
62 | int ifindex; | |
63 | struct netdev *netdev; | |
64 | }; | |
65 | ||
66 | /* Remove matching ufid entry from ufid_tc hashmap. */ | |
67 | static void | |
68 | del_ufid_tc_mapping(const ovs_u128 *ufid) | |
69 | { | |
70 | size_t ufid_hash = hash_bytes(ufid, sizeof *ufid, 0); | |
71 | struct ufid_tc_data *data; | |
72 | ||
73 | ovs_mutex_lock(&ufid_lock); | |
74 | HMAP_FOR_EACH_WITH_HASH(data, ufid_node, ufid_hash, &ufid_tc) { | |
75 | if (ovs_u128_equals(*ufid, data->ufid)) { | |
76 | break; | |
77 | } | |
78 | } | |
79 | ||
80 | if (!data) { | |
81 | ovs_mutex_unlock(&ufid_lock); | |
82 | return; | |
83 | } | |
84 | ||
85 | hmap_remove(&ufid_tc, &data->ufid_node); | |
86 | hmap_remove(&ufid_tc, &data->tc_node); | |
87 | netdev_close(data->netdev); | |
88 | free(data); | |
89 | ovs_mutex_unlock(&ufid_lock); | |
90 | } | |
91 | ||
92 | /* Add ufid entry to ufid_tc hashmap. | |
93 | * If entry exists already it will be replaced. */ | |
8f283af8 | 94 | static void |
9116730d PB |
95 | add_ufid_tc_mapping(const ovs_u128 *ufid, int prio, int handle, |
96 | struct netdev *netdev, int ifindex) | |
97 | { | |
98 | size_t ufid_hash = hash_bytes(ufid, sizeof *ufid, 0); | |
99 | size_t tc_hash = hash_int(hash_int(prio, handle), ifindex); | |
100 | struct ufid_tc_data *new_data = xzalloc(sizeof *new_data); | |
101 | ||
102 | del_ufid_tc_mapping(ufid); | |
103 | ||
104 | new_data->ufid = *ufid; | |
105 | new_data->prio = prio; | |
106 | new_data->handle = handle; | |
107 | new_data->netdev = netdev_ref(netdev); | |
108 | new_data->ifindex = ifindex; | |
109 | ||
110 | ovs_mutex_lock(&ufid_lock); | |
111 | hmap_insert(&ufid_tc, &new_data->ufid_node, ufid_hash); | |
112 | hmap_insert(&ufid_tc, &new_data->tc_node, tc_hash); | |
113 | ovs_mutex_unlock(&ufid_lock); | |
114 | } | |
115 | ||
116 | /* Get ufid from ufid_tc hashmap. | |
117 | * | |
118 | * If netdev output param is not NULL then the function will return | |
119 | * associated netdev on success and a refcount is taken on that netdev. | |
120 | * The caller is then responsible to close the netdev. | |
121 | * | |
122 | * Returns handle if successful and fill prio and netdev for that ufid. | |
123 | * Otherwise returns 0. | |
124 | */ | |
8f283af8 | 125 | static int |
9116730d PB |
126 | get_ufid_tc_mapping(const ovs_u128 *ufid, int *prio, struct netdev **netdev) |
127 | { | |
128 | size_t ufid_hash = hash_bytes(ufid, sizeof *ufid, 0); | |
129 | struct ufid_tc_data *data; | |
130 | int handle = 0; | |
131 | ||
132 | ovs_mutex_lock(&ufid_lock); | |
133 | HMAP_FOR_EACH_WITH_HASH(data, ufid_node, ufid_hash, &ufid_tc) { | |
134 | if (ovs_u128_equals(*ufid, data->ufid)) { | |
135 | if (prio) { | |
136 | *prio = data->prio; | |
137 | } | |
138 | if (netdev) { | |
139 | *netdev = netdev_ref(data->netdev); | |
140 | } | |
141 | handle = data->handle; | |
142 | break; | |
143 | } | |
144 | } | |
145 | ovs_mutex_unlock(&ufid_lock); | |
146 | ||
147 | return handle; | |
148 | } | |
149 | ||
150 | /* Find ufid entry in ufid_tc hashmap using prio, handle and netdev. | |
151 | * The result is saved in ufid. | |
152 | * | |
153 | * Returns true on success. | |
154 | */ | |
8f7620e6 | 155 | static bool |
9116730d PB |
156 | find_ufid(int prio, int handle, struct netdev *netdev, ovs_u128 *ufid) |
157 | { | |
158 | int ifindex = netdev_get_ifindex(netdev); | |
159 | struct ufid_tc_data *data; | |
160 | size_t tc_hash = hash_int(hash_int(prio, handle), ifindex); | |
161 | ||
162 | ovs_mutex_lock(&ufid_lock); | |
163 | HMAP_FOR_EACH_WITH_HASH(data, tc_node, tc_hash, &ufid_tc) { | |
164 | if (data->prio == prio && data->handle == handle | |
165 | && data->ifindex == ifindex) { | |
166 | *ufid = data->ufid; | |
167 | break; | |
168 | } | |
169 | } | |
170 | ovs_mutex_unlock(&ufid_lock); | |
171 | ||
172 | return (data != NULL); | |
173 | } | |
174 | ||
d86eea7c PB |
175 | struct prio_map_data { |
176 | struct hmap_node node; | |
177 | struct tc_flower_key mask; | |
178 | ovs_be16 protocol; | |
179 | uint16_t prio; | |
180 | }; | |
181 | ||
182 | /* Get free prio for tc flower | |
183 | * If prio is already allocated for mask/eth_type combination then return it. | |
184 | * If not assign new prio. | |
185 | * | |
186 | * Return prio on success or 0 if we are out of prios. | |
187 | */ | |
8f283af8 | 188 | static uint16_t |
d86eea7c PB |
189 | get_prio_for_tc_flower(struct tc_flower *flower) |
190 | { | |
191 | static struct hmap prios = HMAP_INITIALIZER(&prios); | |
192 | static struct ovs_mutex prios_lock = OVS_MUTEX_INITIALIZER; | |
193 | static uint16_t last_prio = 0; | |
194 | size_t key_len = sizeof(struct tc_flower_key); | |
195 | size_t hash = hash_bytes(&flower->mask, key_len, | |
196 | (OVS_FORCE uint32_t) flower->key.eth_type); | |
197 | struct prio_map_data *data; | |
198 | struct prio_map_data *new_data; | |
199 | ||
200 | /* We can use the same prio for same mask/eth combination but must have | |
201 | * different prio if not. Flower classifier will reject same prio for | |
202 | * different mask/eth combination. */ | |
203 | ovs_mutex_lock(&prios_lock); | |
204 | HMAP_FOR_EACH_WITH_HASH(data, node, hash, &prios) { | |
205 | if (!memcmp(&flower->mask, &data->mask, key_len) | |
206 | && data->protocol == flower->key.eth_type) { | |
207 | ovs_mutex_unlock(&prios_lock); | |
208 | return data->prio; | |
209 | } | |
210 | } | |
211 | ||
212 | if (last_prio == UINT16_MAX) { | |
213 | /* last_prio can overflow if there will be many different kinds of | |
214 | * flows which shouldn't happen organically. */ | |
215 | ovs_mutex_unlock(&prios_lock); | |
216 | return 0; | |
217 | } | |
218 | ||
219 | new_data = xzalloc(sizeof *new_data); | |
220 | memcpy(&new_data->mask, &flower->mask, key_len); | |
221 | new_data->prio = ++last_prio; | |
222 | new_data->protocol = flower->key.eth_type; | |
223 | hmap_insert(&prios, &new_data->node, hash); | |
224 | ovs_mutex_unlock(&prios_lock); | |
225 | ||
226 | return new_data->prio; | |
227 | } | |
228 | ||
18ebd48c | 229 | int |
8140a5ff | 230 | netdev_tc_flow_flush(struct netdev *netdev) |
18ebd48c | 231 | { |
8140a5ff PB |
232 | int ifindex = netdev_get_ifindex(netdev); |
233 | ||
234 | if (ifindex < 0) { | |
235 | VLOG_ERR_RL(&error_rl, "failed to get ifindex for %s: %s", | |
236 | netdev_get_name(netdev), ovs_strerror(-ifindex)); | |
237 | return -ifindex; | |
238 | } | |
239 | ||
240 | return tc_flush(ifindex); | |
18ebd48c PB |
241 | } |
242 | ||
243 | int | |
244 | netdev_tc_flow_dump_create(struct netdev *netdev, | |
245 | struct netdev_flow_dump **dump_out) | |
246 | { | |
8f7620e6 PB |
247 | struct netdev_flow_dump *dump; |
248 | int ifindex; | |
249 | ||
250 | ifindex = netdev_get_ifindex(netdev); | |
251 | if (ifindex < 0) { | |
252 | VLOG_ERR_RL(&error_rl, "failed to get ifindex for %s: %s", | |
253 | netdev_get_name(netdev), ovs_strerror(-ifindex)); | |
254 | return -ifindex; | |
255 | } | |
18ebd48c | 256 | |
8f7620e6 PB |
257 | dump = xzalloc(sizeof *dump); |
258 | dump->nl_dump = xzalloc(sizeof *dump->nl_dump); | |
18ebd48c | 259 | dump->netdev = netdev_ref(netdev); |
8f7620e6 | 260 | tc_dump_flower_start(ifindex, dump->nl_dump); |
18ebd48c PB |
261 | |
262 | *dump_out = dump; | |
263 | ||
264 | return 0; | |
265 | } | |
266 | ||
267 | int | |
268 | netdev_tc_flow_dump_destroy(struct netdev_flow_dump *dump) | |
269 | { | |
8f7620e6 | 270 | nl_dump_done(dump->nl_dump); |
18ebd48c | 271 | netdev_close(dump->netdev); |
8f7620e6 | 272 | free(dump->nl_dump); |
18ebd48c | 273 | free(dump); |
8f7620e6 PB |
274 | return 0; |
275 | } | |
276 | ||
277 | static int | |
278 | parse_tc_flower_to_match(struct tc_flower *flower, | |
279 | struct match *match, | |
280 | struct nlattr **actions, | |
281 | struct dpif_flow_stats *stats, | |
282 | struct ofpbuf *buf) { | |
283 | size_t act_off; | |
284 | struct tc_flower_key *key = &flower->key; | |
285 | struct tc_flower_key *mask = &flower->mask; | |
286 | odp_port_t outport = 0; | |
287 | ||
288 | if (flower->ifindex_out) { | |
289 | outport = netdev_ifindex_to_odp_port(flower->ifindex_out); | |
290 | if (!outport) { | |
291 | return ENOENT; | |
292 | } | |
293 | } | |
294 | ||
295 | ofpbuf_clear(buf); | |
296 | ||
297 | match_init_catchall(match); | |
298 | match_set_dl_src_masked(match, key->src_mac, mask->src_mac); | |
299 | match_set_dl_dst_masked(match, key->dst_mac, mask->dst_mac); | |
300 | ||
301 | if (key->eth_type == htons(ETH_TYPE_VLAN)) { | |
302 | match_set_dl_vlan(match, htons(key->vlan_id)); | |
303 | match_set_dl_vlan_pcp(match, key->vlan_prio); | |
304 | match_set_dl_type(match, key->encap_eth_type); | |
305 | flow_fix_vlan_tpid(&match->flow); | |
306 | } else { | |
307 | match_set_dl_type(match, key->eth_type); | |
308 | } | |
309 | ||
310 | if (key->ip_proto && is_ip_any(&match->flow)) { | |
311 | match_set_nw_proto(match, key->ip_proto); | |
312 | } | |
313 | ||
314 | match_set_nw_src_masked(match, key->ipv4.ipv4_src, mask->ipv4.ipv4_src); | |
315 | match_set_nw_dst_masked(match, key->ipv4.ipv4_dst, mask->ipv4.ipv4_dst); | |
316 | ||
317 | match_set_ipv6_src_masked(match, | |
318 | &key->ipv6.ipv6_src, &mask->ipv6.ipv6_src); | |
319 | match_set_ipv6_dst_masked(match, | |
320 | &key->ipv6.ipv6_dst, &mask->ipv6.ipv6_dst); | |
321 | ||
322 | match_set_tp_dst_masked(match, key->dst_port, mask->dst_port); | |
323 | match_set_tp_src_masked(match, key->src_port, mask->src_port); | |
324 | ||
325 | if (flower->tunnel.tunnel) { | |
326 | match_set_tun_id(match, flower->tunnel.id); | |
327 | if (flower->tunnel.ipv4.ipv4_dst) { | |
328 | match_set_tun_src(match, flower->tunnel.ipv4.ipv4_src); | |
329 | match_set_tun_dst(match, flower->tunnel.ipv4.ipv4_dst); | |
330 | } else if (!is_all_zeros(&flower->tunnel.ipv6.ipv6_dst, | |
331 | sizeof flower->tunnel.ipv6.ipv6_dst)) { | |
332 | match_set_tun_ipv6_src(match, &flower->tunnel.ipv6.ipv6_src); | |
333 | match_set_tun_ipv6_dst(match, &flower->tunnel.ipv6.ipv6_dst); | |
334 | } | |
335 | if (flower->tunnel.tp_dst) { | |
336 | match_set_tun_tp_dst(match, flower->tunnel.tp_dst); | |
337 | } | |
338 | } | |
339 | ||
340 | act_off = nl_msg_start_nested(buf, OVS_FLOW_ATTR_ACTIONS); | |
341 | { | |
342 | if (flower->vlan_pop) { | |
343 | nl_msg_put_flag(buf, OVS_ACTION_ATTR_POP_VLAN); | |
344 | } | |
345 | ||
346 | if (flower->vlan_push_id || flower->vlan_push_prio) { | |
347 | struct ovs_action_push_vlan *push; | |
348 | push = nl_msg_put_unspec_zero(buf, OVS_ACTION_ATTR_PUSH_VLAN, | |
349 | sizeof *push); | |
350 | ||
351 | push->vlan_tpid = htons(ETH_TYPE_VLAN); | |
352 | push->vlan_tci = htons(flower->vlan_push_id | |
353 | | (flower->vlan_push_prio << 13) | |
354 | | VLAN_CFI); | |
355 | } | |
356 | ||
357 | if (flower->set.set) { | |
358 | size_t set_offset = nl_msg_start_nested(buf, OVS_ACTION_ATTR_SET); | |
359 | size_t tunnel_offset = | |
360 | nl_msg_start_nested(buf, OVS_KEY_ATTR_TUNNEL); | |
361 | ||
362 | nl_msg_put_be64(buf, OVS_TUNNEL_KEY_ATTR_ID, flower->set.id); | |
363 | if (flower->set.ipv4.ipv4_src) { | |
364 | nl_msg_put_be32(buf, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, | |
365 | flower->set.ipv4.ipv4_src); | |
366 | } | |
367 | if (flower->set.ipv4.ipv4_dst) { | |
368 | nl_msg_put_be32(buf, OVS_TUNNEL_KEY_ATTR_IPV4_DST, | |
369 | flower->set.ipv4.ipv4_dst); | |
370 | } | |
371 | if (!is_all_zeros(&flower->set.ipv6.ipv6_src, | |
372 | sizeof flower->set.ipv6.ipv6_src)) { | |
373 | nl_msg_put_in6_addr(buf, OVS_TUNNEL_KEY_ATTR_IPV6_SRC, | |
374 | &flower->set.ipv6.ipv6_src); | |
375 | } | |
376 | if (!is_all_zeros(&flower->set.ipv6.ipv6_dst, | |
377 | sizeof flower->set.ipv6.ipv6_dst)) { | |
378 | nl_msg_put_in6_addr(buf, OVS_TUNNEL_KEY_ATTR_IPV6_DST, | |
379 | &flower->set.ipv6.ipv6_dst); | |
380 | } | |
381 | nl_msg_put_be16(buf, OVS_TUNNEL_KEY_ATTR_TP_DST, | |
382 | flower->set.tp_dst); | |
383 | ||
384 | nl_msg_end_nested(buf, tunnel_offset); | |
385 | nl_msg_end_nested(buf, set_offset); | |
386 | } | |
387 | ||
388 | if (flower->ifindex_out > 0) { | |
389 | nl_msg_put_u32(buf, OVS_ACTION_ATTR_OUTPUT, odp_to_u32(outport)); | |
390 | } | |
391 | ||
392 | } | |
393 | nl_msg_end_nested(buf, act_off); | |
394 | ||
395 | *actions = ofpbuf_at_assert(buf, act_off, sizeof(struct nlattr)); | |
396 | ||
397 | if (stats) { | |
398 | memset(stats, 0, sizeof *stats); | |
399 | stats->n_packets = get_32aligned_u64(&flower->stats.n_packets); | |
400 | stats->n_bytes = get_32aligned_u64(&flower->stats.n_bytes); | |
401 | stats->used = flower->lastused; | |
402 | } | |
18ebd48c PB |
403 | |
404 | return 0; | |
405 | } | |
406 | ||
407 | bool | |
8f7620e6 PB |
408 | netdev_tc_flow_dump_next(struct netdev_flow_dump *dump, |
409 | struct match *match, | |
410 | struct nlattr **actions, | |
411 | struct dpif_flow_stats *stats, | |
412 | ovs_u128 *ufid, | |
413 | struct ofpbuf *rbuffer, | |
414 | struct ofpbuf *wbuffer) | |
18ebd48c | 415 | { |
8f7620e6 PB |
416 | struct ofpbuf nl_flow; |
417 | ||
418 | while (nl_dump_next(dump->nl_dump, &nl_flow, rbuffer)) { | |
419 | struct tc_flower flower; | |
420 | struct netdev *netdev = dump->netdev; | |
421 | ||
422 | if (parse_netlink_to_tc_flower(&nl_flow, &flower)) { | |
423 | continue; | |
424 | } | |
425 | ||
426 | if (parse_tc_flower_to_match(&flower, match, actions, stats, | |
427 | wbuffer)) { | |
428 | continue; | |
429 | } | |
430 | ||
431 | if (flower.act_cookie.len) { | |
432 | *ufid = *((ovs_u128 *) flower.act_cookie.data); | |
433 | } else if (!find_ufid(flower.prio, flower.handle, netdev, ufid)) { | |
434 | continue; | |
435 | } | |
436 | ||
437 | match->wc.masks.in_port.odp_port = u32_to_odp(UINT32_MAX); | |
438 | match->flow.in_port.odp_port = dump->port; | |
439 | ||
440 | return true; | |
441 | } | |
442 | ||
18ebd48c PB |
443 | return false; |
444 | } | |
445 | ||
8f283af8 PB |
446 | static int |
447 | parse_put_flow_set_action(struct tc_flower *flower, const struct nlattr *set, | |
448 | size_t set_len) | |
449 | { | |
450 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20); | |
451 | const struct nlattr *set_attr; | |
452 | size_t set_left; | |
453 | ||
454 | NL_ATTR_FOR_EACH_UNSAFE(set_attr, set_left, set, set_len) { | |
455 | if (nl_attr_type(set_attr) == OVS_KEY_ATTR_TUNNEL) { | |
456 | const struct nlattr *tunnel = nl_attr_get(set_attr); | |
457 | const size_t tunnel_len = nl_attr_get_size(set_attr); | |
458 | const struct nlattr *tun_attr; | |
459 | size_t tun_left; | |
460 | ||
461 | flower->set.set = true; | |
462 | NL_ATTR_FOR_EACH_UNSAFE(tun_attr, tun_left, tunnel, tunnel_len) { | |
463 | switch (nl_attr_type(tun_attr)) { | |
464 | case OVS_TUNNEL_KEY_ATTR_ID: { | |
465 | flower->set.id = nl_attr_get_be64(tun_attr); | |
466 | } | |
467 | break; | |
468 | case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: { | |
469 | flower->set.ipv4.ipv4_src = nl_attr_get_be32(tun_attr); | |
470 | } | |
471 | break; | |
472 | case OVS_TUNNEL_KEY_ATTR_IPV4_DST: { | |
473 | flower->set.ipv4.ipv4_dst = nl_attr_get_be32(tun_attr); | |
474 | } | |
475 | break; | |
476 | case OVS_TUNNEL_KEY_ATTR_IPV6_SRC: { | |
477 | flower->set.ipv6.ipv6_src = | |
478 | nl_attr_get_in6_addr(tun_attr); | |
479 | } | |
480 | break; | |
481 | case OVS_TUNNEL_KEY_ATTR_IPV6_DST: { | |
482 | flower->set.ipv6.ipv6_dst = | |
483 | nl_attr_get_in6_addr(tun_attr); | |
484 | } | |
485 | break; | |
486 | case OVS_TUNNEL_KEY_ATTR_TP_SRC: { | |
487 | flower->set.tp_src = nl_attr_get_be16(tun_attr); | |
488 | } | |
489 | break; | |
490 | case OVS_TUNNEL_KEY_ATTR_TP_DST: { | |
491 | flower->set.tp_dst = nl_attr_get_be16(tun_attr); | |
492 | } | |
493 | break; | |
494 | } | |
495 | } | |
496 | } else { | |
497 | VLOG_DBG_RL(&rl, "unsupported set action type: %d", | |
498 | nl_attr_type(set_attr)); | |
499 | return EOPNOTSUPP; | |
500 | } | |
501 | } | |
502 | return 0; | |
503 | } | |
504 | ||
505 | static int | |
506 | test_key_and_mask(struct match *match) | |
507 | { | |
508 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20); | |
509 | const struct flow *key = &match->flow; | |
510 | struct flow *mask = &match->wc.masks; | |
511 | ||
512 | if (mask->pkt_mark) { | |
513 | VLOG_DBG_RL(&rl, "offloading attribute pkt_mark isn't supported"); | |
514 | return EOPNOTSUPP; | |
515 | } | |
516 | ||
517 | if (mask->recirc_id && key->recirc_id) { | |
518 | VLOG_DBG_RL(&rl, "offloading attribute recirc_id isn't supported"); | |
519 | return EOPNOTSUPP; | |
520 | } | |
521 | mask->recirc_id = 0; | |
522 | ||
523 | if (mask->dp_hash) { | |
524 | VLOG_DBG_RL(&rl, "offloading attribute dp_hash isn't supported"); | |
525 | return EOPNOTSUPP; | |
526 | } | |
527 | ||
528 | if (mask->conj_id) { | |
529 | VLOG_DBG_RL(&rl, "offloading attribute conj_id isn't supported"); | |
530 | return EOPNOTSUPP; | |
531 | } | |
532 | ||
533 | if (mask->skb_priority) { | |
534 | VLOG_DBG_RL(&rl, "offloading attribute skb_priority isn't supported"); | |
535 | return EOPNOTSUPP; | |
536 | } | |
537 | ||
538 | if (mask->actset_output) { | |
539 | VLOG_DBG_RL(&rl, | |
540 | "offloading attribute actset_output isn't supported"); | |
541 | return EOPNOTSUPP; | |
542 | } | |
543 | ||
544 | if (mask->ct_state) { | |
545 | VLOG_DBG_RL(&rl, "offloading attribute ct_state isn't supported"); | |
546 | return EOPNOTSUPP; | |
547 | } | |
548 | ||
549 | if (mask->ct_zone) { | |
550 | VLOG_DBG_RL(&rl, "offloading attribute ct_zone isn't supported"); | |
551 | return EOPNOTSUPP; | |
552 | } | |
553 | ||
554 | if (mask->ct_mark) { | |
555 | VLOG_DBG_RL(&rl, "offloading attribute ct_mark isn't supported"); | |
556 | return EOPNOTSUPP; | |
557 | } | |
558 | ||
559 | if (mask->packet_type && key->packet_type) { | |
560 | VLOG_DBG_RL(&rl, "offloading attribute packet_type isn't supported"); | |
561 | return EOPNOTSUPP; | |
562 | } | |
563 | mask->packet_type = 0; | |
564 | ||
565 | if (!ovs_u128_is_zero(mask->ct_label)) { | |
566 | VLOG_DBG_RL(&rl, "offloading attribute ct_label isn't supported"); | |
567 | return EOPNOTSUPP; | |
568 | } | |
569 | ||
570 | for (int i = 0; i < FLOW_N_REGS; i++) { | |
571 | if (mask->regs[i]) { | |
572 | VLOG_DBG_RL(&rl, | |
573 | "offloading attribute regs[%d] isn't supported", i); | |
574 | return EOPNOTSUPP; | |
575 | } | |
576 | } | |
577 | ||
578 | if (mask->metadata) { | |
579 | VLOG_DBG_RL(&rl, "offloading attribute metadata isn't supported"); | |
580 | return EOPNOTSUPP; | |
581 | } | |
582 | ||
583 | if (mask->nw_tos) { | |
584 | VLOG_DBG_RL(&rl, "offloading attribute nw_tos isn't supported"); | |
585 | return EOPNOTSUPP; | |
586 | } | |
587 | ||
588 | if (mask->nw_ttl) { | |
589 | VLOG_DBG_RL(&rl, "offloading attribute nw_ttl isn't supported"); | |
590 | return EOPNOTSUPP; | |
591 | } | |
592 | ||
593 | if (mask->nw_frag) { | |
594 | VLOG_DBG_RL(&rl, "offloading attribute nw_frag isn't supported"); | |
595 | return EOPNOTSUPP; | |
596 | } | |
597 | ||
598 | for (int i = 0; i < FLOW_MAX_MPLS_LABELS; i++) { | |
599 | if (mask->mpls_lse[i]) { | |
600 | VLOG_DBG_RL(&rl, "offloading attribute mpls_lse isn't supported"); | |
601 | return EOPNOTSUPP; | |
602 | } | |
603 | } | |
604 | ||
605 | if (key->dl_type == htons(ETH_TYPE_IP) && | |
606 | key->nw_proto == IPPROTO_ICMP) { | |
607 | if (mask->tp_src) { | |
608 | VLOG_DBG_RL(&rl, | |
609 | "offloading attribute icmp_type isn't supported"); | |
610 | return EOPNOTSUPP; | |
611 | } | |
612 | if (mask->tp_dst) { | |
613 | VLOG_DBG_RL(&rl, | |
614 | "offloading attribute icmp_code isn't supported"); | |
615 | return EOPNOTSUPP; | |
616 | } | |
617 | } else if (key->dl_type == htons(ETH_TYPE_IP) && | |
618 | key->nw_proto == IPPROTO_IGMP) { | |
619 | if (mask->tp_src) { | |
620 | VLOG_DBG_RL(&rl, | |
621 | "offloading attribute igmp_type isn't supported"); | |
622 | return EOPNOTSUPP; | |
623 | } | |
624 | if (mask->tp_dst) { | |
625 | VLOG_DBG_RL(&rl, | |
626 | "offloading attribute igmp_code isn't supported"); | |
627 | return EOPNOTSUPP; | |
628 | } | |
629 | } else if (key->dl_type == htons(ETH_TYPE_IPV6) && | |
630 | key->nw_proto == IPPROTO_ICMPV6) { | |
631 | if (mask->tp_src) { | |
632 | VLOG_DBG_RL(&rl, | |
633 | "offloading attribute icmp_type isn't supported"); | |
634 | return EOPNOTSUPP; | |
635 | } | |
636 | if (mask->tp_dst) { | |
637 | VLOG_DBG_RL(&rl, | |
638 | "offloading attribute icmp_code isn't supported"); | |
639 | return EOPNOTSUPP; | |
640 | } | |
641 | } | |
642 | if (is_ip_any(key) && key->nw_proto == IPPROTO_TCP && mask->tcp_flags) { | |
643 | if (mask->tcp_flags) { | |
644 | VLOG_DBG_RL(&rl, | |
645 | "offloading attribute tcp_flags isn't supported"); | |
646 | return EOPNOTSUPP; | |
647 | } | |
648 | } | |
649 | ||
650 | if (!is_all_zeros(mask, sizeof *mask)) { | |
651 | VLOG_DBG_RL(&rl, "offloading isn't supported, unknown attribute"); | |
652 | return EOPNOTSUPP; | |
653 | } | |
654 | ||
655 | return 0; | |
656 | } | |
657 | ||
18ebd48c | 658 | int |
8f283af8 PB |
659 | netdev_tc_flow_put(struct netdev *netdev, struct match *match, |
660 | struct nlattr *actions, size_t actions_len, | |
661 | const ovs_u128 *ufid, struct offload_info *info, | |
18ebd48c PB |
662 | struct dpif_flow_stats *stats OVS_UNUSED) |
663 | { | |
8f283af8 PB |
664 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20); |
665 | struct tc_flower flower; | |
666 | const struct flow *key = &match->flow; | |
667 | struct flow *mask = &match->wc.masks; | |
668 | const struct flow_tnl *tnl = &match->flow.tunnel; | |
669 | struct nlattr *nla; | |
670 | size_t left; | |
671 | int prio = 0; | |
672 | int handle; | |
673 | int ifindex; | |
674 | int err; | |
675 | ||
676 | ifindex = netdev_get_ifindex(netdev); | |
677 | if (ifindex < 0) { | |
678 | VLOG_ERR_RL(&error_rl, "failed to get ifindex for %s: %s", | |
679 | netdev_get_name(netdev), ovs_strerror(-ifindex)); | |
680 | return -ifindex; | |
681 | } | |
682 | ||
683 | memset(&flower, 0, sizeof flower); | |
684 | ||
685 | if (tnl->tun_id) { | |
686 | VLOG_DBG_RL(&rl, | |
687 | "tunnel: id %#" PRIx64 " src " IP_FMT | |
688 | " dst " IP_FMT " tp_src %d tp_dst %d", | |
689 | ntohll(tnl->tun_id), | |
690 | IP_ARGS(tnl->ip_src), IP_ARGS(tnl->ip_dst), | |
691 | ntohs(tnl->tp_src), ntohs(tnl->tp_dst)); | |
692 | flower.tunnel.id = tnl->tun_id; | |
693 | flower.tunnel.ipv4.ipv4_src = tnl->ip_src; | |
694 | flower.tunnel.ipv4.ipv4_dst = tnl->ip_dst; | |
695 | flower.tunnel.ipv6.ipv6_src = tnl->ipv6_src; | |
696 | flower.tunnel.ipv6.ipv6_dst = tnl->ipv6_dst; | |
697 | flower.tunnel.tp_src = tnl->tp_src; | |
698 | flower.tunnel.tp_dst = tnl->tp_dst; | |
699 | flower.tunnel.tunnel = true; | |
700 | ||
701 | memset(&mask->tunnel, 0, sizeof mask->tunnel); | |
702 | } | |
703 | ||
704 | flower.key.eth_type = key->dl_type; | |
705 | flower.mask.eth_type = mask->dl_type; | |
706 | ||
707 | if (mask->vlans[0].tci) { | |
708 | ovs_be16 vid_mask = mask->vlans[0].tci & htons(VLAN_VID_MASK); | |
709 | ovs_be16 pcp_mask = mask->vlans[0].tci & htons(VLAN_PCP_MASK); | |
710 | ovs_be16 cfi = mask->vlans[0].tci & htons(VLAN_CFI); | |
711 | ||
712 | if (cfi && key->vlans[0].tci & htons(VLAN_CFI) | |
713 | && (!vid_mask || vid_mask == htons(VLAN_VID_MASK)) | |
714 | && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK)) | |
715 | && (vid_mask || pcp_mask)) { | |
716 | if (vid_mask) { | |
717 | flower.key.vlan_id = vlan_tci_to_vid(key->vlans[0].tci); | |
718 | VLOG_DBG_RL(&rl, "vlan_id: %d\n", flower.key.vlan_id); | |
719 | } | |
720 | if (pcp_mask) { | |
721 | flower.key.vlan_prio = vlan_tci_to_pcp(key->vlans[0].tci); | |
722 | VLOG_DBG_RL(&rl, "vlan_prio: %d\n", flower.key.vlan_prio); | |
723 | } | |
724 | flower.key.encap_eth_type = flower.key.eth_type; | |
725 | flower.key.eth_type = htons(ETH_TYPE_VLAN); | |
726 | } else if (mask->vlans[0].tci == htons(0xffff) && | |
727 | ntohs(key->vlans[0].tci) == 0) { | |
728 | /* exact && no vlan */ | |
729 | } else { | |
730 | /* partial mask */ | |
731 | return EOPNOTSUPP; | |
732 | } | |
733 | } else if (mask->vlans[1].tci) { | |
734 | return EOPNOTSUPP; | |
735 | } | |
736 | memset(mask->vlans, 0, sizeof mask->vlans); | |
737 | ||
738 | flower.key.dst_mac = key->dl_dst; | |
739 | flower.mask.dst_mac = mask->dl_dst; | |
740 | flower.key.src_mac = key->dl_src; | |
741 | flower.mask.src_mac = mask->dl_src; | |
742 | memset(&mask->dl_dst, 0, sizeof mask->dl_dst); | |
743 | memset(&mask->dl_src, 0, sizeof mask->dl_src); | |
744 | mask->dl_type = 0; | |
745 | mask->in_port.odp_port = 0; | |
746 | ||
747 | if (is_ip_any(key)) { | |
748 | flower.key.ip_proto = key->nw_proto; | |
749 | flower.mask.ip_proto = mask->nw_proto; | |
750 | ||
4862b4e5 VB |
751 | if (key->nw_proto == IPPROTO_TCP |
752 | || key->nw_proto == IPPROTO_UDP | |
753 | || key->nw_proto == IPPROTO_SCTP) { | |
8f283af8 PB |
754 | flower.key.dst_port = key->tp_dst; |
755 | flower.mask.dst_port = mask->tp_dst; | |
756 | flower.key.src_port = key->tp_src; | |
757 | flower.mask.src_port = mask->tp_src; | |
758 | mask->tp_src = 0; | |
759 | mask->tp_dst = 0; | |
760 | } | |
761 | ||
762 | mask->nw_frag = 0; | |
763 | mask->nw_tos = 0; | |
764 | mask->nw_proto = 0; | |
765 | ||
766 | if (key->dl_type == htons(ETH_P_IP)) { | |
767 | flower.key.ipv4.ipv4_src = key->nw_src; | |
768 | flower.mask.ipv4.ipv4_src = mask->nw_src; | |
769 | flower.key.ipv4.ipv4_dst = key->nw_dst; | |
770 | flower.mask.ipv4.ipv4_dst = mask->nw_dst; | |
771 | mask->nw_src = 0; | |
772 | mask->nw_dst = 0; | |
773 | } else if (key->dl_type == htons(ETH_P_IPV6)) { | |
774 | flower.key.ipv6.ipv6_src = key->ipv6_src; | |
775 | flower.mask.ipv6.ipv6_src = mask->ipv6_src; | |
776 | flower.key.ipv6.ipv6_dst = key->ipv6_dst; | |
777 | flower.mask.ipv6.ipv6_dst = mask->ipv6_dst; | |
778 | memset(&mask->ipv6_src, 0, sizeof mask->ipv6_src); | |
779 | memset(&mask->ipv6_dst, 0, sizeof mask->ipv6_dst); | |
780 | } | |
781 | } | |
782 | ||
783 | err = test_key_and_mask(match); | |
784 | if (err) { | |
785 | return err; | |
786 | } | |
787 | ||
788 | NL_ATTR_FOR_EACH(nla, left, actions, actions_len) { | |
789 | if (nl_attr_type(nla) == OVS_ACTION_ATTR_OUTPUT) { | |
790 | odp_port_t port = nl_attr_get_odp_port(nla); | |
dfaf79dd | 791 | struct netdev *outdev = netdev_ports_get(port, info->dpif_class); |
8f283af8 PB |
792 | |
793 | flower.ifindex_out = netdev_get_ifindex(outdev); | |
794 | flower.set.tp_dst = info->tp_dst_port; | |
795 | netdev_close(outdev); | |
796 | } else if (nl_attr_type(nla) == OVS_ACTION_ATTR_PUSH_VLAN) { | |
797 | const struct ovs_action_push_vlan *vlan_push = nl_attr_get(nla); | |
798 | ||
799 | flower.vlan_push_id = vlan_tci_to_vid(vlan_push->vlan_tci); | |
800 | flower.vlan_push_prio = vlan_tci_to_pcp(vlan_push->vlan_tci); | |
801 | } else if (nl_attr_type(nla) == OVS_ACTION_ATTR_POP_VLAN) { | |
802 | flower.vlan_pop = 1; | |
803 | } else if (nl_attr_type(nla) == OVS_ACTION_ATTR_SET) { | |
804 | const struct nlattr *set = nl_attr_get(nla); | |
805 | const size_t set_len = nl_attr_get_size(nla); | |
806 | ||
807 | err = parse_put_flow_set_action(&flower, set, set_len); | |
808 | if (err) { | |
809 | return err; | |
810 | } | |
811 | } else { | |
812 | VLOG_DBG_RL(&rl, "unsupported put action type: %d", | |
813 | nl_attr_type(nla)); | |
814 | return EOPNOTSUPP; | |
815 | } | |
816 | } | |
817 | ||
818 | handle = get_ufid_tc_mapping(ufid, &prio, NULL); | |
819 | if (handle && prio) { | |
820 | VLOG_DBG_RL(&rl, "updating old handle: %d prio: %d", handle, prio); | |
821 | tc_del_filter(ifindex, prio, handle); | |
822 | } | |
823 | ||
824 | if (!prio) { | |
825 | prio = get_prio_for_tc_flower(&flower); | |
826 | if (prio == 0) { | |
827 | VLOG_ERR_RL(&rl, "couldn't get tc prio: %s", ovs_strerror(ENOSPC)); | |
828 | return ENOSPC; | |
829 | } | |
830 | } | |
831 | ||
832 | flower.act_cookie.data = ufid; | |
833 | flower.act_cookie.len = sizeof *ufid; | |
834 | ||
835 | err = tc_replace_flower(ifindex, prio, handle, &flower); | |
836 | if (!err) { | |
837 | add_ufid_tc_mapping(ufid, flower.prio, flower.handle, netdev, ifindex); | |
838 | } | |
839 | ||
840 | return err; | |
18ebd48c PB |
841 | } |
842 | ||
843 | int | |
844 | netdev_tc_flow_get(struct netdev *netdev OVS_UNUSED, | |
7ecdef27 PB |
845 | struct match *match, |
846 | struct nlattr **actions, | |
847 | const ovs_u128 *ufid, | |
848 | struct dpif_flow_stats *stats, | |
849 | struct ofpbuf *buf) | |
18ebd48c | 850 | { |
7ecdef27 PB |
851 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20); |
852 | struct netdev *dev; | |
853 | struct tc_flower flower; | |
854 | odp_port_t in_port; | |
855 | int prio = 0; | |
856 | int ifindex; | |
857 | int handle; | |
858 | int err; | |
859 | ||
860 | handle = get_ufid_tc_mapping(ufid, &prio, &dev); | |
861 | if (!handle) { | |
862 | return ENOENT; | |
863 | } | |
864 | ||
865 | ifindex = netdev_get_ifindex(dev); | |
866 | if (ifindex < 0) { | |
867 | VLOG_ERR_RL(&error_rl, "failed to get ifindex for %s: %s", | |
868 | netdev_get_name(dev), ovs_strerror(-ifindex)); | |
869 | netdev_close(dev); | |
870 | return -ifindex; | |
871 | } | |
872 | ||
873 | VLOG_DBG_RL(&rl, "flow get (dev %s prio %d handle %d)", | |
874 | netdev_get_name(dev), prio, handle); | |
875 | err = tc_get_flower(ifindex, prio, handle, &flower); | |
876 | netdev_close(dev); | |
877 | if (err) { | |
878 | VLOG_ERR_RL(&error_rl, "flow get failed (dev %s prio %d handle %d): %s", | |
879 | netdev_get_name(dev), prio, handle, ovs_strerror(err)); | |
880 | return err; | |
881 | } | |
882 | ||
883 | in_port = netdev_ifindex_to_odp_port(ifindex); | |
884 | parse_tc_flower_to_match(&flower, match, actions, stats, buf); | |
885 | ||
886 | match->wc.masks.in_port.odp_port = u32_to_odp(UINT32_MAX); | |
887 | match->flow.in_port.odp_port = in_port; | |
888 | ||
889 | return 0; | |
18ebd48c PB |
890 | } |
891 | ||
892 | int | |
893 | netdev_tc_flow_del(struct netdev *netdev OVS_UNUSED, | |
30b6b047 PB |
894 | const ovs_u128 *ufid, |
895 | struct dpif_flow_stats *stats) | |
18ebd48c | 896 | { |
30b6b047 PB |
897 | struct netdev *dev; |
898 | int prio = 0; | |
899 | int ifindex; | |
900 | int handle; | |
901 | int error; | |
902 | ||
903 | handle = get_ufid_tc_mapping(ufid, &prio, &dev); | |
904 | if (!handle) { | |
905 | return ENOENT; | |
906 | } | |
907 | ||
908 | ifindex = netdev_get_ifindex(dev); | |
909 | if (ifindex < 0) { | |
910 | VLOG_ERR_RL(&error_rl, "failed to get ifindex for %s: %s", | |
911 | netdev_get_name(dev), ovs_strerror(-ifindex)); | |
912 | netdev_close(dev); | |
913 | return -ifindex; | |
914 | } | |
915 | ||
916 | error = tc_del_filter(ifindex, prio, handle); | |
917 | del_ufid_tc_mapping(ufid); | |
918 | ||
919 | netdev_close(dev); | |
920 | ||
921 | if (stats) { | |
922 | memset(stats, 0, sizeof *stats); | |
923 | } | |
924 | return error; | |
18ebd48c PB |
925 | } |
926 | ||
927 | int | |
adbbe97f | 928 | netdev_tc_init_flow_api(struct netdev *netdev) |
18ebd48c | 929 | { |
adbbe97f PB |
930 | int ifindex; |
931 | int error; | |
932 | ||
933 | ifindex = netdev_get_ifindex(netdev); | |
934 | if (ifindex < 0) { | |
935 | VLOG_ERR_RL(&error_rl, "failed to get ifindex for %s: %s", | |
936 | netdev_get_name(netdev), ovs_strerror(-ifindex)); | |
937 | return -ifindex; | |
938 | } | |
939 | ||
940 | error = tc_add_del_ingress_qdisc(ifindex, true); | |
941 | ||
942 | if (error && error != EEXIST) { | |
943 | VLOG_ERR("failed adding ingress qdisc required for offloading: %s", | |
944 | ovs_strerror(error)); | |
945 | return error; | |
946 | } | |
947 | ||
948 | VLOG_INFO("added ingress qdisc to %s", netdev_get_name(netdev)); | |
949 | ||
18ebd48c PB |
950 | return 0; |
951 | } |