]>
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" | |
1ae83bb2 | 30 | #include "openvswitch/util.h" |
18ebd48c | 31 | #include "openvswitch/vlog.h" |
ef3767f5 | 32 | #include "netdev-linux.h" |
18ebd48c PB |
33 | #include "netlink.h" |
34 | #include "netlink-socket.h" | |
35 | #include "odp-netlink.h" | |
1ae83bb2 | 36 | #include "odp-util.h" |
ef3767f5 | 37 | #include "tc.h" |
18ebd48c PB |
38 | #include "unaligned.h" |
39 | #include "util.h" | |
18ebd48c PB |
40 | |
41 | VLOG_DEFINE_THIS_MODULE(netdev_tc_offloads); | |
42 | ||
8140a5ff PB |
43 | static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(60, 5); |
44 | ||
9116730d | 45 | static struct hmap ufid_tc = HMAP_INITIALIZER(&ufid_tc); |
1ae83bb2 PB |
46 | |
47 | struct netlink_field { | |
48 | int offset; | |
49 | int flower_offset; | |
50 | int size; | |
51 | }; | |
52 | ||
53 | static struct netlink_field set_flower_map[][3] = { | |
54 | [OVS_KEY_ATTR_IPV4] = { | |
55 | { offsetof(struct ovs_key_ipv4, ipv4_src), | |
56 | offsetof(struct tc_flower_key, ipv4.ipv4_src), | |
57 | MEMBER_SIZEOF(struct tc_flower_key, ipv4.ipv4_src) | |
58 | }, | |
59 | { offsetof(struct ovs_key_ipv4, ipv4_dst), | |
60 | offsetof(struct tc_flower_key, ipv4.ipv4_dst), | |
61 | MEMBER_SIZEOF(struct tc_flower_key, ipv4.ipv4_dst) | |
62 | }, | |
63 | { offsetof(struct ovs_key_ipv4, ipv4_ttl), | |
64 | offsetof(struct tc_flower_key, ipv4.rewrite_ttl), | |
65 | MEMBER_SIZEOF(struct tc_flower_key, ipv4.rewrite_ttl) | |
66 | }, | |
67 | }, | |
68 | [OVS_KEY_ATTR_IPV6] = { | |
69 | { offsetof(struct ovs_key_ipv6, ipv6_src), | |
70 | offsetof(struct tc_flower_key, ipv6.ipv6_src), | |
71 | MEMBER_SIZEOF(struct tc_flower_key, ipv6.ipv6_src) | |
72 | }, | |
73 | { offsetof(struct ovs_key_ipv6, ipv6_dst), | |
74 | offsetof(struct tc_flower_key, ipv6.ipv6_dst), | |
75 | MEMBER_SIZEOF(struct tc_flower_key, ipv6.ipv6_dst) | |
76 | }, | |
77 | }, | |
78 | [OVS_KEY_ATTR_ETHERNET] = { | |
79 | { offsetof(struct ovs_key_ethernet, eth_src), | |
80 | offsetof(struct tc_flower_key, src_mac), | |
81 | MEMBER_SIZEOF(struct tc_flower_key, src_mac) | |
82 | }, | |
83 | { offsetof(struct ovs_key_ethernet, eth_dst), | |
84 | offsetof(struct tc_flower_key, dst_mac), | |
85 | MEMBER_SIZEOF(struct tc_flower_key, dst_mac) | |
86 | }, | |
87 | }, | |
88 | [OVS_KEY_ATTR_ETHERTYPE] = { | |
89 | { 0, | |
90 | offsetof(struct tc_flower_key, eth_type), | |
91 | MEMBER_SIZEOF(struct tc_flower_key, eth_type) | |
92 | }, | |
93 | }, | |
94 | [OVS_KEY_ATTR_TCP] = { | |
95 | { offsetof(struct ovs_key_tcp, tcp_src), | |
96 | offsetof(struct tc_flower_key, tcp_src), | |
97 | MEMBER_SIZEOF(struct tc_flower_key, tcp_src) | |
98 | }, | |
99 | { offsetof(struct ovs_key_tcp, tcp_dst), | |
100 | offsetof(struct tc_flower_key, tcp_dst), | |
101 | MEMBER_SIZEOF(struct tc_flower_key, tcp_dst) | |
102 | }, | |
103 | }, | |
104 | [OVS_KEY_ATTR_UDP] = { | |
105 | { offsetof(struct ovs_key_udp, udp_src), | |
106 | offsetof(struct tc_flower_key, udp_src), | |
107 | MEMBER_SIZEOF(struct tc_flower_key, udp_src) | |
108 | }, | |
109 | { offsetof(struct ovs_key_udp, udp_dst), | |
110 | offsetof(struct tc_flower_key, udp_dst), | |
111 | MEMBER_SIZEOF(struct tc_flower_key, udp_dst) | |
112 | }, | |
113 | }, | |
114 | }; | |
115 | ||
9116730d PB |
116 | static struct ovs_mutex ufid_lock = OVS_MUTEX_INITIALIZER; |
117 | ||
118 | /** | |
119 | * struct ufid_tc_data - data entry for ufid_tc hmap. | |
120 | * @ufid_node: Element in @ufid_tc hash table by ufid key. | |
121 | * @tc_node: Element in @ufid_tc hash table by prio/handle/ifindex key. | |
122 | * @ufid: ufid assigned to the flow | |
123 | * @prio: tc priority | |
124 | * @handle: tc handle | |
125 | * @ifindex: netdev ifindex. | |
126 | * @netdev: netdev associated with the tc rule | |
127 | */ | |
128 | struct ufid_tc_data { | |
129 | struct hmap_node ufid_node; | |
130 | struct hmap_node tc_node; | |
131 | ovs_u128 ufid; | |
132 | uint16_t prio; | |
133 | uint32_t handle; | |
134 | int ifindex; | |
135 | struct netdev *netdev; | |
136 | }; | |
137 | ||
138 | /* Remove matching ufid entry from ufid_tc hashmap. */ | |
139 | static void | |
140 | del_ufid_tc_mapping(const ovs_u128 *ufid) | |
141 | { | |
142 | size_t ufid_hash = hash_bytes(ufid, sizeof *ufid, 0); | |
143 | struct ufid_tc_data *data; | |
144 | ||
145 | ovs_mutex_lock(&ufid_lock); | |
146 | HMAP_FOR_EACH_WITH_HASH(data, ufid_node, ufid_hash, &ufid_tc) { | |
147 | if (ovs_u128_equals(*ufid, data->ufid)) { | |
148 | break; | |
149 | } | |
150 | } | |
151 | ||
152 | if (!data) { | |
153 | ovs_mutex_unlock(&ufid_lock); | |
154 | return; | |
155 | } | |
156 | ||
157 | hmap_remove(&ufid_tc, &data->ufid_node); | |
158 | hmap_remove(&ufid_tc, &data->tc_node); | |
159 | netdev_close(data->netdev); | |
160 | free(data); | |
161 | ovs_mutex_unlock(&ufid_lock); | |
162 | } | |
163 | ||
164 | /* Add ufid entry to ufid_tc hashmap. | |
165 | * If entry exists already it will be replaced. */ | |
8f283af8 | 166 | static void |
9116730d PB |
167 | add_ufid_tc_mapping(const ovs_u128 *ufid, int prio, int handle, |
168 | struct netdev *netdev, int ifindex) | |
169 | { | |
170 | size_t ufid_hash = hash_bytes(ufid, sizeof *ufid, 0); | |
171 | size_t tc_hash = hash_int(hash_int(prio, handle), ifindex); | |
172 | struct ufid_tc_data *new_data = xzalloc(sizeof *new_data); | |
173 | ||
174 | del_ufid_tc_mapping(ufid); | |
175 | ||
176 | new_data->ufid = *ufid; | |
177 | new_data->prio = prio; | |
178 | new_data->handle = handle; | |
179 | new_data->netdev = netdev_ref(netdev); | |
180 | new_data->ifindex = ifindex; | |
181 | ||
182 | ovs_mutex_lock(&ufid_lock); | |
183 | hmap_insert(&ufid_tc, &new_data->ufid_node, ufid_hash); | |
184 | hmap_insert(&ufid_tc, &new_data->tc_node, tc_hash); | |
185 | ovs_mutex_unlock(&ufid_lock); | |
186 | } | |
187 | ||
188 | /* Get ufid from ufid_tc hashmap. | |
189 | * | |
190 | * If netdev output param is not NULL then the function will return | |
191 | * associated netdev on success and a refcount is taken on that netdev. | |
192 | * The caller is then responsible to close the netdev. | |
193 | * | |
194 | * Returns handle if successful and fill prio and netdev for that ufid. | |
195 | * Otherwise returns 0. | |
196 | */ | |
8f283af8 | 197 | static int |
9116730d PB |
198 | get_ufid_tc_mapping(const ovs_u128 *ufid, int *prio, struct netdev **netdev) |
199 | { | |
200 | size_t ufid_hash = hash_bytes(ufid, sizeof *ufid, 0); | |
201 | struct ufid_tc_data *data; | |
202 | int handle = 0; | |
203 | ||
204 | ovs_mutex_lock(&ufid_lock); | |
205 | HMAP_FOR_EACH_WITH_HASH(data, ufid_node, ufid_hash, &ufid_tc) { | |
206 | if (ovs_u128_equals(*ufid, data->ufid)) { | |
207 | if (prio) { | |
208 | *prio = data->prio; | |
209 | } | |
210 | if (netdev) { | |
211 | *netdev = netdev_ref(data->netdev); | |
212 | } | |
213 | handle = data->handle; | |
214 | break; | |
215 | } | |
216 | } | |
217 | ovs_mutex_unlock(&ufid_lock); | |
218 | ||
219 | return handle; | |
220 | } | |
221 | ||
222 | /* Find ufid entry in ufid_tc hashmap using prio, handle and netdev. | |
223 | * The result is saved in ufid. | |
224 | * | |
225 | * Returns true on success. | |
226 | */ | |
8f7620e6 | 227 | static bool |
9116730d PB |
228 | find_ufid(int prio, int handle, struct netdev *netdev, ovs_u128 *ufid) |
229 | { | |
230 | int ifindex = netdev_get_ifindex(netdev); | |
231 | struct ufid_tc_data *data; | |
232 | size_t tc_hash = hash_int(hash_int(prio, handle), ifindex); | |
233 | ||
234 | ovs_mutex_lock(&ufid_lock); | |
235 | HMAP_FOR_EACH_WITH_HASH(data, tc_node, tc_hash, &ufid_tc) { | |
236 | if (data->prio == prio && data->handle == handle | |
237 | && data->ifindex == ifindex) { | |
238 | *ufid = data->ufid; | |
239 | break; | |
240 | } | |
241 | } | |
242 | ovs_mutex_unlock(&ufid_lock); | |
243 | ||
244 | return (data != NULL); | |
245 | } | |
246 | ||
d86eea7c PB |
247 | struct prio_map_data { |
248 | struct hmap_node node; | |
249 | struct tc_flower_key mask; | |
250 | ovs_be16 protocol; | |
251 | uint16_t prio; | |
252 | }; | |
253 | ||
254 | /* Get free prio for tc flower | |
255 | * If prio is already allocated for mask/eth_type combination then return it. | |
256 | * If not assign new prio. | |
257 | * | |
258 | * Return prio on success or 0 if we are out of prios. | |
259 | */ | |
8f283af8 | 260 | static uint16_t |
d86eea7c PB |
261 | get_prio_for_tc_flower(struct tc_flower *flower) |
262 | { | |
263 | static struct hmap prios = HMAP_INITIALIZER(&prios); | |
264 | static struct ovs_mutex prios_lock = OVS_MUTEX_INITIALIZER; | |
265 | static uint16_t last_prio = 0; | |
266 | size_t key_len = sizeof(struct tc_flower_key); | |
267 | size_t hash = hash_bytes(&flower->mask, key_len, | |
268 | (OVS_FORCE uint32_t) flower->key.eth_type); | |
269 | struct prio_map_data *data; | |
270 | struct prio_map_data *new_data; | |
271 | ||
272 | /* We can use the same prio for same mask/eth combination but must have | |
273 | * different prio if not. Flower classifier will reject same prio for | |
274 | * different mask/eth combination. */ | |
275 | ovs_mutex_lock(&prios_lock); | |
276 | HMAP_FOR_EACH_WITH_HASH(data, node, hash, &prios) { | |
277 | if (!memcmp(&flower->mask, &data->mask, key_len) | |
278 | && data->protocol == flower->key.eth_type) { | |
279 | ovs_mutex_unlock(&prios_lock); | |
280 | return data->prio; | |
281 | } | |
282 | } | |
283 | ||
284 | if (last_prio == UINT16_MAX) { | |
285 | /* last_prio can overflow if there will be many different kinds of | |
286 | * flows which shouldn't happen organically. */ | |
287 | ovs_mutex_unlock(&prios_lock); | |
288 | return 0; | |
289 | } | |
290 | ||
291 | new_data = xzalloc(sizeof *new_data); | |
292 | memcpy(&new_data->mask, &flower->mask, key_len); | |
293 | new_data->prio = ++last_prio; | |
294 | new_data->protocol = flower->key.eth_type; | |
295 | hmap_insert(&prios, &new_data->node, hash); | |
296 | ovs_mutex_unlock(&prios_lock); | |
297 | ||
298 | return new_data->prio; | |
299 | } | |
300 | ||
18ebd48c | 301 | int |
8140a5ff | 302 | netdev_tc_flow_flush(struct netdev *netdev) |
18ebd48c | 303 | { |
8140a5ff PB |
304 | int ifindex = netdev_get_ifindex(netdev); |
305 | ||
306 | if (ifindex < 0) { | |
0164f10c | 307 | VLOG_ERR_RL(&error_rl, "flow_flush: failed to get ifindex for %s: %s", |
8140a5ff PB |
308 | netdev_get_name(netdev), ovs_strerror(-ifindex)); |
309 | return -ifindex; | |
310 | } | |
311 | ||
312 | return tc_flush(ifindex); | |
18ebd48c PB |
313 | } |
314 | ||
315 | int | |
316 | netdev_tc_flow_dump_create(struct netdev *netdev, | |
317 | struct netdev_flow_dump **dump_out) | |
318 | { | |
8f7620e6 PB |
319 | struct netdev_flow_dump *dump; |
320 | int ifindex; | |
321 | ||
322 | ifindex = netdev_get_ifindex(netdev); | |
323 | if (ifindex < 0) { | |
0164f10c | 324 | VLOG_ERR_RL(&error_rl, "dump_create: failed to get ifindex for %s: %s", |
8f7620e6 PB |
325 | netdev_get_name(netdev), ovs_strerror(-ifindex)); |
326 | return -ifindex; | |
327 | } | |
18ebd48c | 328 | |
8f7620e6 PB |
329 | dump = xzalloc(sizeof *dump); |
330 | dump->nl_dump = xzalloc(sizeof *dump->nl_dump); | |
18ebd48c | 331 | dump->netdev = netdev_ref(netdev); |
8f7620e6 | 332 | tc_dump_flower_start(ifindex, dump->nl_dump); |
18ebd48c PB |
333 | |
334 | *dump_out = dump; | |
335 | ||
336 | return 0; | |
337 | } | |
338 | ||
339 | int | |
340 | netdev_tc_flow_dump_destroy(struct netdev_flow_dump *dump) | |
341 | { | |
8f7620e6 | 342 | nl_dump_done(dump->nl_dump); |
18ebd48c | 343 | netdev_close(dump->netdev); |
8f7620e6 | 344 | free(dump->nl_dump); |
18ebd48c | 345 | free(dump); |
8f7620e6 PB |
346 | return 0; |
347 | } | |
348 | ||
1ae83bb2 PB |
349 | static void |
350 | parse_flower_rewrite_to_netlink_action(struct ofpbuf *buf, | |
351 | struct tc_flower *flower) | |
352 | { | |
353 | char *mask = (char *) &flower->rewrite.mask; | |
354 | char *data = (char *) &flower->rewrite.key; | |
355 | ||
356 | for (int type = 0; type < ARRAY_SIZE(set_flower_map); type++) { | |
357 | char *put = NULL; | |
358 | size_t nested = 0; | |
359 | int len = ovs_flow_key_attr_lens[type].len; | |
360 | ||
361 | if (len <= 0) { | |
362 | continue; | |
363 | } | |
364 | ||
365 | for (int j = 0; j < ARRAY_SIZE(set_flower_map[type]); j++) { | |
366 | struct netlink_field *f = &set_flower_map[type][j]; | |
367 | ||
368 | if (!f->size) { | |
369 | break; | |
370 | } | |
371 | ||
372 | if (!is_all_zeros(mask + f->flower_offset, f->size)) { | |
373 | if (!put) { | |
374 | nested = nl_msg_start_nested(buf, | |
375 | OVS_ACTION_ATTR_SET_MASKED); | |
376 | put = nl_msg_put_unspec_zero(buf, type, len * 2); | |
377 | } | |
378 | ||
379 | memcpy(put + f->offset, data + f->flower_offset, f->size); | |
380 | memcpy(put + len + f->offset, | |
381 | mask + f->flower_offset, f->size); | |
382 | } | |
383 | } | |
384 | ||
385 | if (put) { | |
386 | nl_msg_end_nested(buf, nested); | |
387 | } | |
388 | } | |
389 | } | |
390 | ||
8f7620e6 PB |
391 | static int |
392 | parse_tc_flower_to_match(struct tc_flower *flower, | |
393 | struct match *match, | |
394 | struct nlattr **actions, | |
395 | struct dpif_flow_stats *stats, | |
396 | struct ofpbuf *buf) { | |
397 | size_t act_off; | |
398 | struct tc_flower_key *key = &flower->key; | |
399 | struct tc_flower_key *mask = &flower->mask; | |
400 | odp_port_t outport = 0; | |
401 | ||
402 | if (flower->ifindex_out) { | |
403 | outport = netdev_ifindex_to_odp_port(flower->ifindex_out); | |
404 | if (!outport) { | |
405 | return ENOENT; | |
406 | } | |
407 | } | |
408 | ||
409 | ofpbuf_clear(buf); | |
410 | ||
411 | match_init_catchall(match); | |
412 | match_set_dl_src_masked(match, key->src_mac, mask->src_mac); | |
413 | match_set_dl_dst_masked(match, key->dst_mac, mask->dst_mac); | |
414 | ||
415 | if (key->eth_type == htons(ETH_TYPE_VLAN)) { | |
416 | match_set_dl_vlan(match, htons(key->vlan_id)); | |
417 | match_set_dl_vlan_pcp(match, key->vlan_prio); | |
418 | match_set_dl_type(match, key->encap_eth_type); | |
419 | flow_fix_vlan_tpid(&match->flow); | |
420 | } else { | |
421 | match_set_dl_type(match, key->eth_type); | |
422 | } | |
423 | ||
83a87fae PB |
424 | if (is_ip_any(&match->flow)) { |
425 | if (key->ip_proto) { | |
426 | match_set_nw_proto(match, key->ip_proto); | |
427 | } | |
8f7620e6 | 428 | |
ab7ecf26 PB |
429 | match_set_nw_ttl_masked(match, key->ip_ttl, mask->ip_ttl); |
430 | ||
83e86606 RD |
431 | if (mask->flags) { |
432 | uint8_t flags = 0; | |
433 | uint8_t flags_mask = 0; | |
434 | ||
435 | if (mask->flags & TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT) { | |
436 | if (key->flags & TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT) { | |
437 | flags |= FLOW_NW_FRAG_ANY; | |
438 | } | |
439 | flags_mask |= FLOW_NW_FRAG_ANY; | |
440 | } | |
441 | ||
442 | if (mask->flags & TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST) { | |
443 | if (!(key->flags & TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST)) { | |
444 | flags |= FLOW_NW_FRAG_LATER; | |
445 | } | |
446 | flags_mask |= FLOW_NW_FRAG_LATER; | |
447 | } | |
448 | ||
449 | match_set_nw_frag_masked(match, flags, flags_mask); | |
450 | } | |
451 | ||
83a87fae PB |
452 | match_set_nw_src_masked(match, key->ipv4.ipv4_src, mask->ipv4.ipv4_src); |
453 | match_set_nw_dst_masked(match, key->ipv4.ipv4_dst, mask->ipv4.ipv4_dst); | |
8f7620e6 | 454 | |
83a87fae PB |
455 | match_set_ipv6_src_masked(match, |
456 | &key->ipv6.ipv6_src, &mask->ipv6.ipv6_src); | |
457 | match_set_ipv6_dst_masked(match, | |
458 | &key->ipv6.ipv6_dst, &mask->ipv6.ipv6_dst); | |
8f7620e6 | 459 | |
2b1d9fa9 PB |
460 | if (key->ip_proto == IPPROTO_TCP) { |
461 | match_set_tp_dst_masked(match, key->tcp_dst, mask->tcp_dst); | |
462 | match_set_tp_src_masked(match, key->tcp_src, mask->tcp_src); | |
f2698407 | 463 | match_set_tcp_flags_masked(match, key->tcp_flags, mask->tcp_flags); |
2b1d9fa9 PB |
464 | } else if (key->ip_proto == IPPROTO_UDP) { |
465 | match_set_tp_dst_masked(match, key->udp_dst, mask->udp_dst); | |
466 | match_set_tp_src_masked(match, key->udp_src, mask->udp_src); | |
a238dbb5 RD |
467 | } else if (key->ip_proto == IPPROTO_SCTP) { |
468 | match_set_tp_dst_masked(match, key->sctp_dst, mask->sctp_dst); | |
469 | match_set_tp_src_masked(match, key->sctp_src, mask->sctp_src); | |
2b1d9fa9 PB |
470 | } |
471 | } | |
8f7620e6 PB |
472 | |
473 | if (flower->tunnel.tunnel) { | |
474 | match_set_tun_id(match, flower->tunnel.id); | |
475 | if (flower->tunnel.ipv4.ipv4_dst) { | |
476 | match_set_tun_src(match, flower->tunnel.ipv4.ipv4_src); | |
477 | match_set_tun_dst(match, flower->tunnel.ipv4.ipv4_dst); | |
478 | } else if (!is_all_zeros(&flower->tunnel.ipv6.ipv6_dst, | |
479 | sizeof flower->tunnel.ipv6.ipv6_dst)) { | |
480 | match_set_tun_ipv6_src(match, &flower->tunnel.ipv6.ipv6_src); | |
481 | match_set_tun_ipv6_dst(match, &flower->tunnel.ipv6.ipv6_dst); | |
482 | } | |
483 | if (flower->tunnel.tp_dst) { | |
484 | match_set_tun_tp_dst(match, flower->tunnel.tp_dst); | |
485 | } | |
486 | } | |
487 | ||
488 | act_off = nl_msg_start_nested(buf, OVS_FLOW_ATTR_ACTIONS); | |
489 | { | |
490 | if (flower->vlan_pop) { | |
491 | nl_msg_put_flag(buf, OVS_ACTION_ATTR_POP_VLAN); | |
492 | } | |
493 | ||
494 | if (flower->vlan_push_id || flower->vlan_push_prio) { | |
495 | struct ovs_action_push_vlan *push; | |
496 | push = nl_msg_put_unspec_zero(buf, OVS_ACTION_ATTR_PUSH_VLAN, | |
497 | sizeof *push); | |
498 | ||
499 | push->vlan_tpid = htons(ETH_TYPE_VLAN); | |
500 | push->vlan_tci = htons(flower->vlan_push_id | |
501 | | (flower->vlan_push_prio << 13) | |
502 | | VLAN_CFI); | |
503 | } | |
504 | ||
1ae83bb2 PB |
505 | if (flower->rewrite.rewrite) { |
506 | parse_flower_rewrite_to_netlink_action(buf, flower); | |
507 | } | |
508 | ||
8f7620e6 PB |
509 | if (flower->set.set) { |
510 | size_t set_offset = nl_msg_start_nested(buf, OVS_ACTION_ATTR_SET); | |
511 | size_t tunnel_offset = | |
512 | nl_msg_start_nested(buf, OVS_KEY_ATTR_TUNNEL); | |
513 | ||
514 | nl_msg_put_be64(buf, OVS_TUNNEL_KEY_ATTR_ID, flower->set.id); | |
515 | if (flower->set.ipv4.ipv4_src) { | |
516 | nl_msg_put_be32(buf, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, | |
517 | flower->set.ipv4.ipv4_src); | |
518 | } | |
519 | if (flower->set.ipv4.ipv4_dst) { | |
520 | nl_msg_put_be32(buf, OVS_TUNNEL_KEY_ATTR_IPV4_DST, | |
521 | flower->set.ipv4.ipv4_dst); | |
522 | } | |
523 | if (!is_all_zeros(&flower->set.ipv6.ipv6_src, | |
524 | sizeof flower->set.ipv6.ipv6_src)) { | |
525 | nl_msg_put_in6_addr(buf, OVS_TUNNEL_KEY_ATTR_IPV6_SRC, | |
526 | &flower->set.ipv6.ipv6_src); | |
527 | } | |
528 | if (!is_all_zeros(&flower->set.ipv6.ipv6_dst, | |
529 | sizeof flower->set.ipv6.ipv6_dst)) { | |
530 | nl_msg_put_in6_addr(buf, OVS_TUNNEL_KEY_ATTR_IPV6_DST, | |
531 | &flower->set.ipv6.ipv6_dst); | |
532 | } | |
533 | nl_msg_put_be16(buf, OVS_TUNNEL_KEY_ATTR_TP_DST, | |
534 | flower->set.tp_dst); | |
535 | ||
536 | nl_msg_end_nested(buf, tunnel_offset); | |
537 | nl_msg_end_nested(buf, set_offset); | |
538 | } | |
539 | ||
540 | if (flower->ifindex_out > 0) { | |
541 | nl_msg_put_u32(buf, OVS_ACTION_ATTR_OUTPUT, odp_to_u32(outport)); | |
542 | } | |
543 | ||
544 | } | |
545 | nl_msg_end_nested(buf, act_off); | |
546 | ||
547 | *actions = ofpbuf_at_assert(buf, act_off, sizeof(struct nlattr)); | |
548 | ||
549 | if (stats) { | |
550 | memset(stats, 0, sizeof *stats); | |
551 | stats->n_packets = get_32aligned_u64(&flower->stats.n_packets); | |
552 | stats->n_bytes = get_32aligned_u64(&flower->stats.n_bytes); | |
553 | stats->used = flower->lastused; | |
554 | } | |
18ebd48c PB |
555 | |
556 | return 0; | |
557 | } | |
558 | ||
559 | bool | |
8f7620e6 PB |
560 | netdev_tc_flow_dump_next(struct netdev_flow_dump *dump, |
561 | struct match *match, | |
562 | struct nlattr **actions, | |
563 | struct dpif_flow_stats *stats, | |
564 | ovs_u128 *ufid, | |
565 | struct ofpbuf *rbuffer, | |
566 | struct ofpbuf *wbuffer) | |
18ebd48c | 567 | { |
8f7620e6 PB |
568 | struct ofpbuf nl_flow; |
569 | ||
570 | while (nl_dump_next(dump->nl_dump, &nl_flow, rbuffer)) { | |
571 | struct tc_flower flower; | |
572 | struct netdev *netdev = dump->netdev; | |
573 | ||
574 | if (parse_netlink_to_tc_flower(&nl_flow, &flower)) { | |
575 | continue; | |
576 | } | |
577 | ||
578 | if (parse_tc_flower_to_match(&flower, match, actions, stats, | |
579 | wbuffer)) { | |
580 | continue; | |
581 | } | |
582 | ||
583 | if (flower.act_cookie.len) { | |
584 | *ufid = *((ovs_u128 *) flower.act_cookie.data); | |
585 | } else if (!find_ufid(flower.prio, flower.handle, netdev, ufid)) { | |
586 | continue; | |
587 | } | |
588 | ||
589 | match->wc.masks.in_port.odp_port = u32_to_odp(UINT32_MAX); | |
590 | match->flow.in_port.odp_port = dump->port; | |
591 | ||
592 | return true; | |
593 | } | |
594 | ||
18ebd48c PB |
595 | return false; |
596 | } | |
597 | ||
1ae83bb2 PB |
598 | static int |
599 | parse_put_flow_set_masked_action(struct tc_flower *flower, | |
600 | const struct nlattr *set, | |
601 | size_t set_len, | |
602 | bool hasmask) | |
603 | { | |
604 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20); | |
40f58368 BP |
605 | uint64_t set_stub[1024 / 8]; |
606 | struct ofpbuf set_buf = OFPBUF_STUB_INITIALIZER(set_stub); | |
607 | char *set_data, *set_mask; | |
1ae83bb2 PB |
608 | char *key = (char *) &flower->rewrite.key; |
609 | char *mask = (char *) &flower->rewrite.mask; | |
610 | const struct nlattr *attr; | |
611 | int i, j, type; | |
612 | size_t size; | |
613 | ||
614 | /* copy so we can set attr mask to 0 for used ovs key struct members */ | |
40f58368 | 615 | attr = ofpbuf_put(&set_buf, set, set_len); |
1ae83bb2 PB |
616 | |
617 | type = nl_attr_type(attr); | |
618 | size = nl_attr_get_size(attr) / 2; | |
619 | set_data = CONST_CAST(char *, nl_attr_get(attr)); | |
620 | set_mask = set_data + size; | |
621 | ||
622 | if (type >= ARRAY_SIZE(set_flower_map) | |
623 | || !set_flower_map[type][0].size) { | |
624 | VLOG_DBG_RL(&rl, "unsupported set action type: %d", type); | |
40f58368 | 625 | ofpbuf_uninit(&set_buf); |
1ae83bb2 PB |
626 | return EOPNOTSUPP; |
627 | } | |
628 | ||
629 | for (i = 0; i < ARRAY_SIZE(set_flower_map[type]); i++) { | |
630 | struct netlink_field *f = &set_flower_map[type][i]; | |
631 | ||
632 | if (!f->size) { | |
633 | break; | |
634 | } | |
635 | ||
636 | /* copy masked value */ | |
637 | for (j = 0; j < f->size; j++) { | |
638 | char maskval = hasmask ? set_mask[f->offset + j] : 0xFF; | |
639 | ||
640 | key[f->flower_offset + j] = maskval & set_data[f->offset + j]; | |
641 | mask[f->flower_offset + j] = maskval; | |
642 | ||
643 | } | |
644 | ||
645 | /* set its mask to 0 to show it's been used. */ | |
646 | if (hasmask) { | |
647 | memset(set_mask + f->offset, 0, f->size); | |
648 | } | |
649 | } | |
650 | ||
651 | if (!is_all_zeros(&flower->rewrite, sizeof flower->rewrite)) { | |
652 | flower->rewrite.rewrite = true; | |
653 | } | |
654 | ||
655 | if (hasmask && !is_all_zeros(set_mask, size)) { | |
656 | VLOG_DBG_RL(&rl, "unsupported sub attribute of set action type %d", | |
657 | type); | |
40f58368 | 658 | ofpbuf_uninit(&set_buf); |
1ae83bb2 PB |
659 | return EOPNOTSUPP; |
660 | } | |
661 | ||
40f58368 | 662 | ofpbuf_uninit(&set_buf); |
1ae83bb2 PB |
663 | return 0; |
664 | } | |
665 | ||
8f283af8 PB |
666 | static int |
667 | parse_put_flow_set_action(struct tc_flower *flower, const struct nlattr *set, | |
668 | size_t set_len) | |
669 | { | |
518bbe99 PB |
670 | const struct nlattr *tunnel; |
671 | const struct nlattr *tun_attr; | |
672 | size_t tun_left, tunnel_len; | |
673 | ||
674 | if (nl_attr_type(set) != OVS_KEY_ATTR_TUNNEL) { | |
1ae83bb2 PB |
675 | return parse_put_flow_set_masked_action(flower, set, set_len, |
676 | false); | |
518bbe99 PB |
677 | } |
678 | ||
679 | tunnel = nl_attr_get(set); | |
680 | tunnel_len = nl_attr_get_size(set); | |
681 | ||
682 | flower->set.set = true; | |
683 | NL_ATTR_FOR_EACH_UNSAFE(tun_attr, tun_left, tunnel, tunnel_len) { | |
684 | switch (nl_attr_type(tun_attr)) { | |
685 | case OVS_TUNNEL_KEY_ATTR_ID: { | |
686 | flower->set.id = nl_attr_get_be64(tun_attr); | |
687 | } | |
688 | break; | |
689 | case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: { | |
690 | flower->set.ipv4.ipv4_src = nl_attr_get_be32(tun_attr); | |
691 | } | |
692 | break; | |
693 | case OVS_TUNNEL_KEY_ATTR_IPV4_DST: { | |
694 | flower->set.ipv4.ipv4_dst = nl_attr_get_be32(tun_attr); | |
695 | } | |
696 | break; | |
697 | case OVS_TUNNEL_KEY_ATTR_IPV6_SRC: { | |
698 | flower->set.ipv6.ipv6_src = | |
699 | nl_attr_get_in6_addr(tun_attr); | |
700 | } | |
701 | break; | |
702 | case OVS_TUNNEL_KEY_ATTR_IPV6_DST: { | |
703 | flower->set.ipv6.ipv6_dst = | |
704 | nl_attr_get_in6_addr(tun_attr); | |
705 | } | |
706 | break; | |
707 | case OVS_TUNNEL_KEY_ATTR_TP_SRC: { | |
708 | flower->set.tp_src = nl_attr_get_be16(tun_attr); | |
709 | } | |
710 | break; | |
711 | case OVS_TUNNEL_KEY_ATTR_TP_DST: { | |
712 | flower->set.tp_dst = nl_attr_get_be16(tun_attr); | |
713 | } | |
714 | break; | |
8f283af8 PB |
715 | } |
716 | } | |
518bbe99 | 717 | |
8f283af8 PB |
718 | return 0; |
719 | } | |
720 | ||
721 | static int | |
722 | test_key_and_mask(struct match *match) | |
723 | { | |
724 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20); | |
725 | const struct flow *key = &match->flow; | |
726 | struct flow *mask = &match->wc.masks; | |
727 | ||
728 | if (mask->pkt_mark) { | |
729 | VLOG_DBG_RL(&rl, "offloading attribute pkt_mark isn't supported"); | |
730 | return EOPNOTSUPP; | |
731 | } | |
732 | ||
733 | if (mask->recirc_id && key->recirc_id) { | |
734 | VLOG_DBG_RL(&rl, "offloading attribute recirc_id isn't supported"); | |
735 | return EOPNOTSUPP; | |
736 | } | |
737 | mask->recirc_id = 0; | |
738 | ||
739 | if (mask->dp_hash) { | |
740 | VLOG_DBG_RL(&rl, "offloading attribute dp_hash isn't supported"); | |
741 | return EOPNOTSUPP; | |
742 | } | |
743 | ||
744 | if (mask->conj_id) { | |
745 | VLOG_DBG_RL(&rl, "offloading attribute conj_id isn't supported"); | |
746 | return EOPNOTSUPP; | |
747 | } | |
748 | ||
749 | if (mask->skb_priority) { | |
750 | VLOG_DBG_RL(&rl, "offloading attribute skb_priority isn't supported"); | |
751 | return EOPNOTSUPP; | |
752 | } | |
753 | ||
754 | if (mask->actset_output) { | |
755 | VLOG_DBG_RL(&rl, | |
756 | "offloading attribute actset_output isn't supported"); | |
757 | return EOPNOTSUPP; | |
758 | } | |
759 | ||
760 | if (mask->ct_state) { | |
761 | VLOG_DBG_RL(&rl, "offloading attribute ct_state isn't supported"); | |
762 | return EOPNOTSUPP; | |
763 | } | |
764 | ||
765 | if (mask->ct_zone) { | |
766 | VLOG_DBG_RL(&rl, "offloading attribute ct_zone isn't supported"); | |
767 | return EOPNOTSUPP; | |
768 | } | |
769 | ||
770 | if (mask->ct_mark) { | |
771 | VLOG_DBG_RL(&rl, "offloading attribute ct_mark isn't supported"); | |
772 | return EOPNOTSUPP; | |
773 | } | |
774 | ||
775 | if (mask->packet_type && key->packet_type) { | |
776 | VLOG_DBG_RL(&rl, "offloading attribute packet_type isn't supported"); | |
777 | return EOPNOTSUPP; | |
778 | } | |
779 | mask->packet_type = 0; | |
780 | ||
781 | if (!ovs_u128_is_zero(mask->ct_label)) { | |
782 | VLOG_DBG_RL(&rl, "offloading attribute ct_label isn't supported"); | |
783 | return EOPNOTSUPP; | |
784 | } | |
785 | ||
786 | for (int i = 0; i < FLOW_N_REGS; i++) { | |
787 | if (mask->regs[i]) { | |
788 | VLOG_DBG_RL(&rl, | |
789 | "offloading attribute regs[%d] isn't supported", i); | |
790 | return EOPNOTSUPP; | |
791 | } | |
792 | } | |
793 | ||
794 | if (mask->metadata) { | |
795 | VLOG_DBG_RL(&rl, "offloading attribute metadata isn't supported"); | |
796 | return EOPNOTSUPP; | |
797 | } | |
798 | ||
799 | if (mask->nw_tos) { | |
800 | VLOG_DBG_RL(&rl, "offloading attribute nw_tos isn't supported"); | |
801 | return EOPNOTSUPP; | |
802 | } | |
803 | ||
8f283af8 PB |
804 | for (int i = 0; i < FLOW_MAX_MPLS_LABELS; i++) { |
805 | if (mask->mpls_lse[i]) { | |
806 | VLOG_DBG_RL(&rl, "offloading attribute mpls_lse isn't supported"); | |
807 | return EOPNOTSUPP; | |
808 | } | |
809 | } | |
810 | ||
811 | if (key->dl_type == htons(ETH_TYPE_IP) && | |
812 | key->nw_proto == IPPROTO_ICMP) { | |
813 | if (mask->tp_src) { | |
814 | VLOG_DBG_RL(&rl, | |
815 | "offloading attribute icmp_type isn't supported"); | |
816 | return EOPNOTSUPP; | |
817 | } | |
818 | if (mask->tp_dst) { | |
819 | VLOG_DBG_RL(&rl, | |
820 | "offloading attribute icmp_code isn't supported"); | |
821 | return EOPNOTSUPP; | |
822 | } | |
823 | } else if (key->dl_type == htons(ETH_TYPE_IP) && | |
824 | key->nw_proto == IPPROTO_IGMP) { | |
825 | if (mask->tp_src) { | |
826 | VLOG_DBG_RL(&rl, | |
827 | "offloading attribute igmp_type isn't supported"); | |
828 | return EOPNOTSUPP; | |
829 | } | |
830 | if (mask->tp_dst) { | |
831 | VLOG_DBG_RL(&rl, | |
832 | "offloading attribute igmp_code isn't supported"); | |
833 | return EOPNOTSUPP; | |
834 | } | |
835 | } else if (key->dl_type == htons(ETH_TYPE_IPV6) && | |
836 | key->nw_proto == IPPROTO_ICMPV6) { | |
837 | if (mask->tp_src) { | |
838 | VLOG_DBG_RL(&rl, | |
839 | "offloading attribute icmp_type isn't supported"); | |
840 | return EOPNOTSUPP; | |
841 | } | |
842 | if (mask->tp_dst) { | |
843 | VLOG_DBG_RL(&rl, | |
844 | "offloading attribute icmp_code isn't supported"); | |
845 | return EOPNOTSUPP; | |
846 | } | |
847 | } | |
8f283af8 PB |
848 | |
849 | if (!is_all_zeros(mask, sizeof *mask)) { | |
850 | VLOG_DBG_RL(&rl, "offloading isn't supported, unknown attribute"); | |
851 | return EOPNOTSUPP; | |
852 | } | |
853 | ||
854 | return 0; | |
855 | } | |
856 | ||
18ebd48c | 857 | int |
8f283af8 PB |
858 | netdev_tc_flow_put(struct netdev *netdev, struct match *match, |
859 | struct nlattr *actions, size_t actions_len, | |
860 | const ovs_u128 *ufid, struct offload_info *info, | |
18ebd48c PB |
861 | struct dpif_flow_stats *stats OVS_UNUSED) |
862 | { | |
8f283af8 PB |
863 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20); |
864 | struct tc_flower flower; | |
865 | const struct flow *key = &match->flow; | |
866 | struct flow *mask = &match->wc.masks; | |
867 | const struct flow_tnl *tnl = &match->flow.tunnel; | |
868 | struct nlattr *nla; | |
869 | size_t left; | |
870 | int prio = 0; | |
871 | int handle; | |
872 | int ifindex; | |
873 | int err; | |
874 | ||
875 | ifindex = netdev_get_ifindex(netdev); | |
876 | if (ifindex < 0) { | |
0164f10c | 877 | VLOG_ERR_RL(&error_rl, "flow_put: failed to get ifindex for %s: %s", |
8f283af8 PB |
878 | netdev_get_name(netdev), ovs_strerror(-ifindex)); |
879 | return -ifindex; | |
880 | } | |
881 | ||
882 | memset(&flower, 0, sizeof flower); | |
883 | ||
a90120fc | 884 | if (flow_tnl_dst_is_set(&key->tunnel)) { |
8f283af8 PB |
885 | VLOG_DBG_RL(&rl, |
886 | "tunnel: id %#" PRIx64 " src " IP_FMT | |
887 | " dst " IP_FMT " tp_src %d tp_dst %d", | |
888 | ntohll(tnl->tun_id), | |
889 | IP_ARGS(tnl->ip_src), IP_ARGS(tnl->ip_dst), | |
890 | ntohs(tnl->tp_src), ntohs(tnl->tp_dst)); | |
891 | flower.tunnel.id = tnl->tun_id; | |
892 | flower.tunnel.ipv4.ipv4_src = tnl->ip_src; | |
893 | flower.tunnel.ipv4.ipv4_dst = tnl->ip_dst; | |
894 | flower.tunnel.ipv6.ipv6_src = tnl->ipv6_src; | |
895 | flower.tunnel.ipv6.ipv6_dst = tnl->ipv6_dst; | |
896 | flower.tunnel.tp_src = tnl->tp_src; | |
897 | flower.tunnel.tp_dst = tnl->tp_dst; | |
898 | flower.tunnel.tunnel = true; | |
8f283af8 | 899 | } |
a90120fc | 900 | memset(&mask->tunnel, 0, sizeof mask->tunnel); |
8f283af8 PB |
901 | |
902 | flower.key.eth_type = key->dl_type; | |
903 | flower.mask.eth_type = mask->dl_type; | |
904 | ||
905 | if (mask->vlans[0].tci) { | |
906 | ovs_be16 vid_mask = mask->vlans[0].tci & htons(VLAN_VID_MASK); | |
907 | ovs_be16 pcp_mask = mask->vlans[0].tci & htons(VLAN_PCP_MASK); | |
908 | ovs_be16 cfi = mask->vlans[0].tci & htons(VLAN_CFI); | |
909 | ||
910 | if (cfi && key->vlans[0].tci & htons(VLAN_CFI) | |
911 | && (!vid_mask || vid_mask == htons(VLAN_VID_MASK)) | |
912 | && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK)) | |
913 | && (vid_mask || pcp_mask)) { | |
914 | if (vid_mask) { | |
915 | flower.key.vlan_id = vlan_tci_to_vid(key->vlans[0].tci); | |
916 | VLOG_DBG_RL(&rl, "vlan_id: %d\n", flower.key.vlan_id); | |
917 | } | |
918 | if (pcp_mask) { | |
919 | flower.key.vlan_prio = vlan_tci_to_pcp(key->vlans[0].tci); | |
920 | VLOG_DBG_RL(&rl, "vlan_prio: %d\n", flower.key.vlan_prio); | |
921 | } | |
922 | flower.key.encap_eth_type = flower.key.eth_type; | |
923 | flower.key.eth_type = htons(ETH_TYPE_VLAN); | |
924 | } else if (mask->vlans[0].tci == htons(0xffff) && | |
925 | ntohs(key->vlans[0].tci) == 0) { | |
926 | /* exact && no vlan */ | |
927 | } else { | |
928 | /* partial mask */ | |
929 | return EOPNOTSUPP; | |
930 | } | |
931 | } else if (mask->vlans[1].tci) { | |
932 | return EOPNOTSUPP; | |
933 | } | |
934 | memset(mask->vlans, 0, sizeof mask->vlans); | |
935 | ||
936 | flower.key.dst_mac = key->dl_dst; | |
937 | flower.mask.dst_mac = mask->dl_dst; | |
938 | flower.key.src_mac = key->dl_src; | |
939 | flower.mask.src_mac = mask->dl_src; | |
940 | memset(&mask->dl_dst, 0, sizeof mask->dl_dst); | |
941 | memset(&mask->dl_src, 0, sizeof mask->dl_src); | |
942 | mask->dl_type = 0; | |
943 | mask->in_port.odp_port = 0; | |
944 | ||
945 | if (is_ip_any(key)) { | |
946 | flower.key.ip_proto = key->nw_proto; | |
947 | flower.mask.ip_proto = mask->nw_proto; | |
ab7ecf26 PB |
948 | flower.key.ip_ttl = key->nw_ttl; |
949 | flower.mask.ip_ttl = mask->nw_ttl; | |
8f283af8 | 950 | |
83e86606 RD |
951 | if (mask->nw_frag) { |
952 | if (key->nw_frag & FLOW_NW_FRAG_ANY) | |
953 | flower.key.flags |= TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT; | |
954 | if (!(key->nw_frag & FLOW_NW_FRAG_LATER)) | |
955 | flower.key.flags |= TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST; | |
956 | ||
957 | flower.mask.flags |= TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT; | |
958 | flower.mask.flags |= TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST; | |
959 | mask->nw_frag = 0; | |
960 | } | |
961 | ||
2b1d9fa9 PB |
962 | if (key->nw_proto == IPPROTO_TCP) { |
963 | flower.key.tcp_dst = key->tp_dst; | |
964 | flower.mask.tcp_dst = mask->tp_dst; | |
965 | flower.key.tcp_src = key->tp_src; | |
966 | flower.mask.tcp_src = mask->tp_src; | |
f2698407 PB |
967 | flower.key.tcp_flags = key->tcp_flags; |
968 | flower.mask.tcp_flags = mask->tcp_flags; | |
2b1d9fa9 PB |
969 | mask->tp_src = 0; |
970 | mask->tp_dst = 0; | |
f2698407 | 971 | mask->tcp_flags = 0; |
2b1d9fa9 PB |
972 | } else if (key->nw_proto == IPPROTO_UDP) { |
973 | flower.key.udp_dst = key->tp_dst; | |
974 | flower.mask.udp_dst = mask->tp_dst; | |
975 | flower.key.udp_src = key->tp_src; | |
976 | flower.mask.udp_src = mask->tp_src; | |
977 | mask->tp_src = 0; | |
978 | mask->tp_dst = 0; | |
979 | } else if (key->nw_proto == IPPROTO_SCTP) { | |
980 | flower.key.sctp_dst = key->tp_dst; | |
981 | flower.mask.sctp_dst = mask->tp_dst; | |
982 | flower.key.sctp_src = key->tp_src; | |
983 | flower.mask.sctp_src = mask->tp_src; | |
8f283af8 PB |
984 | mask->tp_src = 0; |
985 | mask->tp_dst = 0; | |
986 | } | |
987 | ||
8f283af8 PB |
988 | mask->nw_tos = 0; |
989 | mask->nw_proto = 0; | |
ab7ecf26 | 990 | mask->nw_ttl = 0; |
8f283af8 PB |
991 | |
992 | if (key->dl_type == htons(ETH_P_IP)) { | |
993 | flower.key.ipv4.ipv4_src = key->nw_src; | |
994 | flower.mask.ipv4.ipv4_src = mask->nw_src; | |
995 | flower.key.ipv4.ipv4_dst = key->nw_dst; | |
996 | flower.mask.ipv4.ipv4_dst = mask->nw_dst; | |
997 | mask->nw_src = 0; | |
998 | mask->nw_dst = 0; | |
999 | } else if (key->dl_type == htons(ETH_P_IPV6)) { | |
1000 | flower.key.ipv6.ipv6_src = key->ipv6_src; | |
1001 | flower.mask.ipv6.ipv6_src = mask->ipv6_src; | |
1002 | flower.key.ipv6.ipv6_dst = key->ipv6_dst; | |
1003 | flower.mask.ipv6.ipv6_dst = mask->ipv6_dst; | |
1004 | memset(&mask->ipv6_src, 0, sizeof mask->ipv6_src); | |
1005 | memset(&mask->ipv6_dst, 0, sizeof mask->ipv6_dst); | |
1006 | } | |
1007 | } | |
1008 | ||
1009 | err = test_key_and_mask(match); | |
1010 | if (err) { | |
1011 | return err; | |
1012 | } | |
1013 | ||
1014 | NL_ATTR_FOR_EACH(nla, left, actions, actions_len) { | |
1015 | if (nl_attr_type(nla) == OVS_ACTION_ATTR_OUTPUT) { | |
1016 | odp_port_t port = nl_attr_get_odp_port(nla); | |
dfaf79dd | 1017 | struct netdev *outdev = netdev_ports_get(port, info->dpif_class); |
8f283af8 PB |
1018 | |
1019 | flower.ifindex_out = netdev_get_ifindex(outdev); | |
1020 | flower.set.tp_dst = info->tp_dst_port; | |
1021 | netdev_close(outdev); | |
1022 | } else if (nl_attr_type(nla) == OVS_ACTION_ATTR_PUSH_VLAN) { | |
1023 | const struct ovs_action_push_vlan *vlan_push = nl_attr_get(nla); | |
1024 | ||
1025 | flower.vlan_push_id = vlan_tci_to_vid(vlan_push->vlan_tci); | |
1026 | flower.vlan_push_prio = vlan_tci_to_pcp(vlan_push->vlan_tci); | |
1027 | } else if (nl_attr_type(nla) == OVS_ACTION_ATTR_POP_VLAN) { | |
1028 | flower.vlan_pop = 1; | |
1029 | } else if (nl_attr_type(nla) == OVS_ACTION_ATTR_SET) { | |
1030 | const struct nlattr *set = nl_attr_get(nla); | |
1031 | const size_t set_len = nl_attr_get_size(nla); | |
1032 | ||
1033 | err = parse_put_flow_set_action(&flower, set, set_len); | |
1034 | if (err) { | |
1035 | return err; | |
1036 | } | |
1ae83bb2 PB |
1037 | } else if (nl_attr_type(nla) == OVS_ACTION_ATTR_SET_MASKED) { |
1038 | const struct nlattr *set = nl_attr_get(nla); | |
1039 | const size_t set_len = nl_attr_get_size(nla); | |
1040 | ||
1041 | err = parse_put_flow_set_masked_action(&flower, set, set_len, | |
1042 | true); | |
1043 | if (err) { | |
1044 | return err; | |
1045 | } | |
8f283af8 PB |
1046 | } else { |
1047 | VLOG_DBG_RL(&rl, "unsupported put action type: %d", | |
1048 | nl_attr_type(nla)); | |
1049 | return EOPNOTSUPP; | |
1050 | } | |
1051 | } | |
1052 | ||
1053 | handle = get_ufid_tc_mapping(ufid, &prio, NULL); | |
1054 | if (handle && prio) { | |
1055 | VLOG_DBG_RL(&rl, "updating old handle: %d prio: %d", handle, prio); | |
1056 | tc_del_filter(ifindex, prio, handle); | |
1057 | } | |
1058 | ||
1059 | if (!prio) { | |
1060 | prio = get_prio_for_tc_flower(&flower); | |
1061 | if (prio == 0) { | |
1062 | VLOG_ERR_RL(&rl, "couldn't get tc prio: %s", ovs_strerror(ENOSPC)); | |
1063 | return ENOSPC; | |
1064 | } | |
1065 | } | |
1066 | ||
1067 | flower.act_cookie.data = ufid; | |
1068 | flower.act_cookie.len = sizeof *ufid; | |
1069 | ||
1070 | err = tc_replace_flower(ifindex, prio, handle, &flower); | |
1071 | if (!err) { | |
1072 | add_ufid_tc_mapping(ufid, flower.prio, flower.handle, netdev, ifindex); | |
1073 | } | |
1074 | ||
1075 | return err; | |
18ebd48c PB |
1076 | } |
1077 | ||
1078 | int | |
1079 | netdev_tc_flow_get(struct netdev *netdev OVS_UNUSED, | |
7ecdef27 PB |
1080 | struct match *match, |
1081 | struct nlattr **actions, | |
1082 | const ovs_u128 *ufid, | |
1083 | struct dpif_flow_stats *stats, | |
1084 | struct ofpbuf *buf) | |
18ebd48c | 1085 | { |
7ecdef27 PB |
1086 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20); |
1087 | struct netdev *dev; | |
1088 | struct tc_flower flower; | |
1089 | odp_port_t in_port; | |
1090 | int prio = 0; | |
1091 | int ifindex; | |
1092 | int handle; | |
1093 | int err; | |
1094 | ||
1095 | handle = get_ufid_tc_mapping(ufid, &prio, &dev); | |
1096 | if (!handle) { | |
1097 | return ENOENT; | |
1098 | } | |
1099 | ||
1100 | ifindex = netdev_get_ifindex(dev); | |
1101 | if (ifindex < 0) { | |
0164f10c | 1102 | VLOG_ERR_RL(&error_rl, "flow_get: failed to get ifindex for %s: %s", |
7ecdef27 PB |
1103 | netdev_get_name(dev), ovs_strerror(-ifindex)); |
1104 | netdev_close(dev); | |
1105 | return -ifindex; | |
1106 | } | |
1107 | ||
1108 | VLOG_DBG_RL(&rl, "flow get (dev %s prio %d handle %d)", | |
1109 | netdev_get_name(dev), prio, handle); | |
1110 | err = tc_get_flower(ifindex, prio, handle, &flower); | |
1111 | netdev_close(dev); | |
1112 | if (err) { | |
1113 | VLOG_ERR_RL(&error_rl, "flow get failed (dev %s prio %d handle %d): %s", | |
1114 | netdev_get_name(dev), prio, handle, ovs_strerror(err)); | |
1115 | return err; | |
1116 | } | |
1117 | ||
1118 | in_port = netdev_ifindex_to_odp_port(ifindex); | |
1119 | parse_tc_flower_to_match(&flower, match, actions, stats, buf); | |
1120 | ||
1121 | match->wc.masks.in_port.odp_port = u32_to_odp(UINT32_MAX); | |
1122 | match->flow.in_port.odp_port = in_port; | |
1123 | ||
1124 | return 0; | |
18ebd48c PB |
1125 | } |
1126 | ||
1127 | int | |
1128 | netdev_tc_flow_del(struct netdev *netdev OVS_UNUSED, | |
30b6b047 PB |
1129 | const ovs_u128 *ufid, |
1130 | struct dpif_flow_stats *stats) | |
18ebd48c | 1131 | { |
52f793b8 | 1132 | struct tc_flower flower; |
30b6b047 PB |
1133 | struct netdev *dev; |
1134 | int prio = 0; | |
1135 | int ifindex; | |
1136 | int handle; | |
1137 | int error; | |
1138 | ||
1139 | handle = get_ufid_tc_mapping(ufid, &prio, &dev); | |
1140 | if (!handle) { | |
1141 | return ENOENT; | |
1142 | } | |
1143 | ||
1144 | ifindex = netdev_get_ifindex(dev); | |
1145 | if (ifindex < 0) { | |
0164f10c | 1146 | VLOG_ERR_RL(&error_rl, "flow_del: failed to get ifindex for %s: %s", |
30b6b047 PB |
1147 | netdev_get_name(dev), ovs_strerror(-ifindex)); |
1148 | netdev_close(dev); | |
1149 | return -ifindex; | |
1150 | } | |
1151 | ||
52f793b8 PA |
1152 | if (stats) { |
1153 | memset(stats, 0, sizeof *stats); | |
1154 | if (!tc_get_flower(ifindex, prio, handle, &flower)) { | |
1155 | stats->n_packets = get_32aligned_u64(&flower.stats.n_packets); | |
1156 | stats->n_bytes = get_32aligned_u64(&flower.stats.n_bytes); | |
1157 | stats->used = flower.lastused; | |
1158 | } | |
1159 | } | |
1160 | ||
30b6b047 PB |
1161 | error = tc_del_filter(ifindex, prio, handle); |
1162 | del_ufid_tc_mapping(ufid); | |
1163 | ||
1164 | netdev_close(dev); | |
1165 | ||
30b6b047 | 1166 | return error; |
18ebd48c PB |
1167 | } |
1168 | ||
1169 | int | |
adbbe97f | 1170 | netdev_tc_init_flow_api(struct netdev *netdev) |
18ebd48c | 1171 | { |
adbbe97f PB |
1172 | int ifindex; |
1173 | int error; | |
1174 | ||
1175 | ifindex = netdev_get_ifindex(netdev); | |
1176 | if (ifindex < 0) { | |
0164f10c | 1177 | VLOG_ERR_RL(&error_rl, "init: failed to get ifindex for %s: %s", |
adbbe97f PB |
1178 | netdev_get_name(netdev), ovs_strerror(-ifindex)); |
1179 | return -ifindex; | |
1180 | } | |
1181 | ||
1182 | error = tc_add_del_ingress_qdisc(ifindex, true); | |
1183 | ||
1184 | if (error && error != EEXIST) { | |
1185 | VLOG_ERR("failed adding ingress qdisc required for offloading: %s", | |
1186 | ovs_strerror(error)); | |
1187 | return error; | |
1188 | } | |
1189 | ||
1190 | VLOG_INFO("added ingress qdisc to %s", netdev_get_name(netdev)); | |
1191 | ||
18ebd48c PB |
1192 | return 0; |
1193 | } |