]>
Commit | Line | Data |
---|---|---|
c1c5c723 | 1 | /* |
ef3767f5 | 2 | * Copyright (c) 2009-2017 Nicira, Inc. |
f98e418f | 3 | * Copyright (c) 2016 Mellanox Technologies, Ltd. |
c1c5c723 PB |
4 | * |
5 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
6 | * you may not use this file except in compliance with the License. | |
7 | * You may obtain a copy of the License at: | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | ||
18 | #include <config.h> | |
19 | #include "tc.h" | |
ef3767f5 | 20 | |
c1c5c723 | 21 | #include <errno.h> |
f98e418f RD |
22 | #include <linux/if_ether.h> |
23 | #include <linux/rtnetlink.h> | |
8ada482b | 24 | #include <linux/tc_act/tc_csum.h> |
f98e418f RD |
25 | #include <linux/tc_act/tc_gact.h> |
26 | #include <linux/tc_act/tc_mirred.h> | |
8ada482b | 27 | #include <linux/tc_act/tc_pedit.h> |
f98e418f RD |
28 | #include <linux/tc_act/tc_tunnel_key.h> |
29 | #include <linux/tc_act/tc_vlan.h> | |
30 | #include <linux/gen_stats.h> | |
31 | #include <net/if.h> | |
8c1e74d1 | 32 | #include <unistd.h> |
ef3767f5 | 33 | |
f98e418f | 34 | #include "byte-order.h" |
c1c5c723 PB |
35 | #include "netlink-socket.h" |
36 | #include "netlink.h" | |
37 | #include "openvswitch/ofpbuf.h" | |
8ada482b | 38 | #include "openvswitch/util.h" |
c1c5c723 | 39 | #include "openvswitch/vlog.h" |
f98e418f RD |
40 | #include "packets.h" |
41 | #include "timeval.h" | |
ef3767f5 | 42 | #include "unaligned.h" |
c1c5c723 | 43 | |
8ada482b PB |
44 | #define MAX_PEDIT_OFFSETS 32 |
45 | ||
c1c5c723 PB |
46 | VLOG_DEFINE_THIS_MODULE(tc); |
47 | ||
f98e418f RD |
48 | static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(60, 5); |
49 | ||
691d20cb PB |
50 | enum tc_offload_policy { |
51 | TC_POLICY_NONE, | |
52 | TC_POLICY_SKIP_SW, | |
53 | TC_POLICY_SKIP_HW | |
54 | }; | |
55 | ||
56 | static enum tc_offload_policy tc_policy = TC_POLICY_NONE; | |
57 | ||
8ada482b PB |
58 | struct tc_pedit_key_ex { |
59 | enum pedit_header_type htype; | |
60 | enum pedit_cmd cmd; | |
61 | }; | |
62 | ||
63 | struct flower_key_to_pedit { | |
64 | enum pedit_header_type htype; | |
8ada482b | 65 | int offset; |
fbaf1bf9 | 66 | int flower_offset; |
8ada482b PB |
67 | int size; |
68 | }; | |
69 | ||
70 | static struct flower_key_to_pedit flower_pedit_map[] = { | |
71 | { | |
72 | TCA_PEDIT_KEY_EX_HDR_TYPE_IP4, | |
73 | 12, | |
74 | offsetof(struct tc_flower_key, ipv4.ipv4_src), | |
75 | MEMBER_SIZEOF(struct tc_flower_key, ipv4.ipv4_src) | |
76 | }, { | |
77 | TCA_PEDIT_KEY_EX_HDR_TYPE_IP4, | |
78 | 16, | |
79 | offsetof(struct tc_flower_key, ipv4.ipv4_dst), | |
80 | MEMBER_SIZEOF(struct tc_flower_key, ipv4.ipv4_dst) | |
81 | }, { | |
82 | TCA_PEDIT_KEY_EX_HDR_TYPE_IP4, | |
83 | 8, | |
84 | offsetof(struct tc_flower_key, ipv4.rewrite_ttl), | |
85 | MEMBER_SIZEOF(struct tc_flower_key, ipv4.rewrite_ttl) | |
86 | }, { | |
87 | TCA_PEDIT_KEY_EX_HDR_TYPE_IP6, | |
88 | 8, | |
89 | offsetof(struct tc_flower_key, ipv6.ipv6_src), | |
90 | MEMBER_SIZEOF(struct tc_flower_key, ipv6.ipv6_src) | |
91 | }, { | |
92 | TCA_PEDIT_KEY_EX_HDR_TYPE_IP6, | |
93 | 24, | |
94 | offsetof(struct tc_flower_key, ipv6.ipv6_dst), | |
95 | MEMBER_SIZEOF(struct tc_flower_key, ipv6.ipv6_dst) | |
96 | }, { | |
97 | TCA_PEDIT_KEY_EX_HDR_TYPE_ETH, | |
98 | 6, | |
99 | offsetof(struct tc_flower_key, src_mac), | |
100 | MEMBER_SIZEOF(struct tc_flower_key, src_mac) | |
101 | }, { | |
102 | TCA_PEDIT_KEY_EX_HDR_TYPE_ETH, | |
103 | 0, | |
104 | offsetof(struct tc_flower_key, dst_mac), | |
105 | MEMBER_SIZEOF(struct tc_flower_key, dst_mac) | |
106 | }, { | |
107 | TCA_PEDIT_KEY_EX_HDR_TYPE_ETH, | |
108 | 12, | |
109 | offsetof(struct tc_flower_key, eth_type), | |
110 | MEMBER_SIZEOF(struct tc_flower_key, eth_type) | |
111 | }, { | |
112 | TCA_PEDIT_KEY_EX_HDR_TYPE_TCP, | |
113 | 0, | |
114 | offsetof(struct tc_flower_key, tcp_src), | |
115 | MEMBER_SIZEOF(struct tc_flower_key, tcp_src) | |
116 | }, { | |
117 | TCA_PEDIT_KEY_EX_HDR_TYPE_TCP, | |
118 | 2, | |
119 | offsetof(struct tc_flower_key, tcp_dst), | |
120 | MEMBER_SIZEOF(struct tc_flower_key, tcp_dst) | |
121 | }, { | |
122 | TCA_PEDIT_KEY_EX_HDR_TYPE_UDP, | |
123 | 0, | |
124 | offsetof(struct tc_flower_key, udp_src), | |
125 | MEMBER_SIZEOF(struct tc_flower_key, udp_src) | |
126 | }, { | |
127 | TCA_PEDIT_KEY_EX_HDR_TYPE_UDP, | |
128 | 2, | |
129 | offsetof(struct tc_flower_key, udp_dst), | |
130 | MEMBER_SIZEOF(struct tc_flower_key, udp_dst) | |
131 | }, | |
132 | }; | |
133 | ||
d6118e62 PB |
134 | static inline int |
135 | csum_update_flag(struct tc_flower *flower, | |
136 | enum pedit_header_type htype); | |
137 | ||
c1c5c723 PB |
138 | struct tcmsg * |
139 | tc_make_request(int ifindex, int type, unsigned int flags, | |
140 | struct ofpbuf *request) | |
141 | { | |
142 | struct tcmsg *tcmsg; | |
143 | ||
144 | ofpbuf_init(request, 512); | |
145 | nl_msg_put_nlmsghdr(request, sizeof *tcmsg, type, NLM_F_REQUEST | flags); | |
146 | tcmsg = ofpbuf_put_zeros(request, sizeof *tcmsg); | |
147 | tcmsg->tcm_family = AF_UNSPEC; | |
148 | tcmsg->tcm_ifindex = ifindex; | |
149 | /* Caller should fill in tcmsg->tcm_handle. */ | |
150 | /* Caller should fill in tcmsg->tcm_parent. */ | |
151 | ||
152 | return tcmsg; | |
153 | } | |
154 | ||
155 | int | |
156 | tc_transact(struct ofpbuf *request, struct ofpbuf **replyp) | |
157 | { | |
158 | int error = nl_transact(NETLINK_ROUTE, request, replyp); | |
159 | ofpbuf_uninit(request); | |
160 | return error; | |
161 | } | |
162 | ||
163 | /* Adds or deletes a root ingress qdisc on device with specified ifindex. | |
164 | * | |
165 | * This function is equivalent to running the following when 'add' is true: | |
166 | * /sbin/tc qdisc add dev <devname> handle ffff: ingress | |
167 | * | |
168 | * This function is equivalent to running the following when 'add' is false: | |
169 | * /sbin/tc qdisc del dev <devname> handle ffff: ingress | |
170 | * | |
171 | * Where dev <devname> is the device with specified ifindex name. | |
172 | * | |
173 | * The configuration and stats may be seen with the following command: | |
174 | * /sbin/tc -s qdisc show dev <devname> | |
175 | * | |
176 | * Returns 0 if successful, otherwise a positive errno value. | |
177 | */ | |
178 | int | |
179 | tc_add_del_ingress_qdisc(int ifindex, bool add) | |
180 | { | |
181 | struct ofpbuf request; | |
182 | struct tcmsg *tcmsg; | |
183 | int error; | |
184 | int type = add ? RTM_NEWQDISC : RTM_DELQDISC; | |
185 | int flags = add ? NLM_F_EXCL | NLM_F_CREATE : 0; | |
186 | ||
187 | tcmsg = tc_make_request(ifindex, type, flags, &request); | |
209832d5 | 188 | tcmsg->tcm_handle = TC_H_MAKE(TC_H_INGRESS, 0); |
c1c5c723 PB |
189 | tcmsg->tcm_parent = TC_H_INGRESS; |
190 | nl_msg_put_string(&request, TCA_KIND, "ingress"); | |
191 | nl_msg_put_unspec(&request, TCA_OPTIONS, NULL, 0); | |
192 | ||
193 | error = tc_transact(&request, NULL); | |
194 | if (error) { | |
195 | /* If we're deleting the qdisc, don't worry about some of the | |
196 | * error conditions. */ | |
197 | if (!add && (error == ENOENT || error == EINVAL)) { | |
198 | return 0; | |
199 | } | |
200 | return error; | |
201 | } | |
202 | ||
203 | return 0; | |
204 | } | |
f98e418f RD |
205 | |
206 | static const struct nl_policy tca_policy[] = { | |
207 | [TCA_KIND] = { .type = NL_A_STRING, .optional = false, }, | |
208 | [TCA_OPTIONS] = { .type = NL_A_NESTED, .optional = false, }, | |
209 | [TCA_STATS] = { .type = NL_A_UNSPEC, | |
210 | .min_len = sizeof(struct tc_stats), .optional = true, }, | |
211 | [TCA_STATS2] = { .type = NL_A_NESTED, .optional = true, }, | |
212 | }; | |
213 | ||
214 | static const struct nl_policy tca_flower_policy[] = { | |
215 | [TCA_FLOWER_CLASSID] = { .type = NL_A_U32, .optional = true, }, | |
216 | [TCA_FLOWER_INDEV] = { .type = NL_A_STRING, .max_len = IFNAMSIZ, | |
217 | .optional = true, }, | |
218 | [TCA_FLOWER_KEY_ETH_SRC] = { .type = NL_A_UNSPEC, | |
219 | .min_len = ETH_ALEN, .optional = true, }, | |
220 | [TCA_FLOWER_KEY_ETH_DST] = { .type = NL_A_UNSPEC, | |
221 | .min_len = ETH_ALEN, .optional = true, }, | |
222 | [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .type = NL_A_UNSPEC, | |
223 | .min_len = ETH_ALEN, | |
224 | .optional = true, }, | |
225 | [TCA_FLOWER_KEY_ETH_DST_MASK] = { .type = NL_A_UNSPEC, | |
226 | .min_len = ETH_ALEN, | |
227 | .optional = true, }, | |
228 | [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NL_A_U16, .optional = false, }, | |
229 | [TCA_FLOWER_FLAGS] = { .type = NL_A_U32, .optional = false, }, | |
230 | [TCA_FLOWER_ACT] = { .type = NL_A_NESTED, .optional = false, }, | |
231 | [TCA_FLOWER_KEY_IP_PROTO] = { .type = NL_A_U8, .optional = true, }, | |
232 | [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NL_A_U32, .optional = true, }, | |
233 | [TCA_FLOWER_KEY_IPV4_DST] = {.type = NL_A_U32, .optional = true, }, | |
234 | [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NL_A_U32, .optional = true, }, | |
235 | [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NL_A_U32, .optional = true, }, | |
236 | [TCA_FLOWER_KEY_IPV6_SRC] = { .type = NL_A_UNSPEC, | |
237 | .min_len = sizeof(struct in6_addr), | |
238 | .optional = true, }, | |
239 | [TCA_FLOWER_KEY_IPV6_DST] = { .type = NL_A_UNSPEC, | |
240 | .min_len = sizeof(struct in6_addr), | |
241 | .optional = true, }, | |
242 | [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .type = NL_A_UNSPEC, | |
243 | .min_len = sizeof(struct in6_addr), | |
244 | .optional = true, }, | |
245 | [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .type = NL_A_UNSPEC, | |
246 | .min_len = sizeof(struct in6_addr), | |
247 | .optional = true, }, | |
248 | [TCA_FLOWER_KEY_TCP_SRC] = { .type = NL_A_U16, .optional = true, }, | |
249 | [TCA_FLOWER_KEY_TCP_DST] = { .type = NL_A_U16, .optional = true, }, | |
250 | [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NL_A_U16, .optional = true, }, | |
251 | [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NL_A_U16, .optional = true, }, | |
252 | [TCA_FLOWER_KEY_UDP_SRC] = { .type = NL_A_U16, .optional = true, }, | |
253 | [TCA_FLOWER_KEY_UDP_DST] = { .type = NL_A_U16, .optional = true, }, | |
254 | [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NL_A_U16, .optional = true, }, | |
255 | [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NL_A_U16, .optional = true, }, | |
4862b4e5 VB |
256 | [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NL_A_U16, .optional = true, }, |
257 | [TCA_FLOWER_KEY_SCTP_DST] = { .type = NL_A_U16, .optional = true, }, | |
258 | [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NL_A_U16, .optional = true, }, | |
259 | [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NL_A_U16, .optional = true, }, | |
f98e418f RD |
260 | [TCA_FLOWER_KEY_VLAN_ID] = { .type = NL_A_U16, .optional = true, }, |
261 | [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NL_A_U8, .optional = true, }, | |
262 | [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NL_A_U16, .optional = true, }, | |
263 | [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NL_A_U32, .optional = true, }, | |
264 | [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NL_A_U32, .optional = true, }, | |
265 | [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NL_A_U32, .optional = true, }, | |
266 | [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NL_A_U32, | |
267 | .optional = true, }, | |
268 | [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NL_A_U32, | |
269 | .optional = true, }, | |
270 | [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .type = NL_A_UNSPEC, | |
271 | .min_len = sizeof(struct in6_addr), | |
272 | .optional = true, }, | |
273 | [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .type = NL_A_UNSPEC, | |
274 | .min_len = sizeof(struct in6_addr), | |
275 | .optional = true, }, | |
276 | [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .type = NL_A_UNSPEC, | |
277 | .min_len = sizeof(struct in6_addr), | |
278 | .optional = true, }, | |
279 | [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .type = NL_A_UNSPEC, | |
280 | .min_len = sizeof(struct in6_addr), | |
281 | .optional = true, }, | |
282 | [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NL_A_U16, | |
283 | .optional = true, }, | |
83e86606 RD |
284 | [TCA_FLOWER_KEY_FLAGS] = { .type = NL_A_BE32, .optional = true, }, |
285 | [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NL_A_BE32, .optional = true, }, | |
0b4b5203 PB |
286 | [TCA_FLOWER_KEY_IP_TTL] = { .type = NL_A_U8, |
287 | .optional = true, }, | |
288 | [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NL_A_U8, | |
289 | .optional = true, }, | |
cd081043 PB |
290 | [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NL_A_U16, |
291 | .optional = true, }, | |
292 | [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NL_A_U16, | |
293 | .optional = true, }, | |
f98e418f RD |
294 | }; |
295 | ||
296 | static void | |
297 | nl_parse_flower_eth(struct nlattr **attrs, struct tc_flower *flower) | |
298 | { | |
299 | const struct eth_addr *eth; | |
300 | ||
301 | if (attrs[TCA_FLOWER_KEY_ETH_SRC_MASK]) { | |
302 | eth = nl_attr_get_unspec(attrs[TCA_FLOWER_KEY_ETH_SRC], ETH_ALEN); | |
303 | memcpy(&flower->key.src_mac, eth, sizeof flower->key.src_mac); | |
304 | ||
305 | eth = nl_attr_get_unspec(attrs[TCA_FLOWER_KEY_ETH_SRC_MASK], ETH_ALEN); | |
306 | memcpy(&flower->mask.src_mac, eth, sizeof flower->mask.src_mac); | |
307 | } | |
308 | if (attrs[TCA_FLOWER_KEY_ETH_DST_MASK]) { | |
309 | eth = nl_attr_get_unspec(attrs[TCA_FLOWER_KEY_ETH_DST], ETH_ALEN); | |
310 | memcpy(&flower->key.dst_mac, eth, sizeof flower->key.dst_mac); | |
311 | ||
312 | eth = nl_attr_get_unspec(attrs[TCA_FLOWER_KEY_ETH_DST_MASK], ETH_ALEN); | |
313 | memcpy(&flower->mask.dst_mac, eth, sizeof flower->mask.dst_mac); | |
314 | } | |
315 | } | |
316 | ||
317 | static void | |
318 | nl_parse_flower_vlan(struct nlattr **attrs, struct tc_flower *flower) | |
319 | { | |
320 | if (flower->key.eth_type != htons(ETH_TYPE_VLAN)) { | |
321 | return; | |
322 | } | |
323 | ||
324 | flower->key.encap_eth_type = | |
325 | nl_attr_get_be16(attrs[TCA_FLOWER_KEY_ETH_TYPE]); | |
326 | ||
327 | if (attrs[TCA_FLOWER_KEY_VLAN_ID]) { | |
328 | flower->key.vlan_id = | |
329 | nl_attr_get_u16(attrs[TCA_FLOWER_KEY_VLAN_ID]); | |
330 | } | |
331 | if (attrs[TCA_FLOWER_KEY_VLAN_PRIO]) { | |
332 | flower->key.vlan_prio = | |
333 | nl_attr_get_u8(attrs[TCA_FLOWER_KEY_VLAN_PRIO]); | |
334 | } | |
335 | } | |
336 | ||
337 | static void | |
338 | nl_parse_flower_tunnel(struct nlattr **attrs, struct tc_flower *flower) | |
339 | { | |
340 | if (attrs[TCA_FLOWER_KEY_ENC_KEY_ID]) { | |
341 | ovs_be32 id = nl_attr_get_be32(attrs[TCA_FLOWER_KEY_ENC_KEY_ID]); | |
342 | ||
343 | flower->tunnel.id = be32_to_be64(id); | |
344 | } | |
345 | if (attrs[TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK]) { | |
346 | flower->tunnel.ipv4.ipv4_src = | |
347 | nl_attr_get_be32(attrs[TCA_FLOWER_KEY_ENC_IPV4_SRC]); | |
348 | } | |
349 | if (attrs[TCA_FLOWER_KEY_ENC_IPV4_DST_MASK]) { | |
350 | flower->tunnel.ipv4.ipv4_dst = | |
351 | nl_attr_get_be32(attrs[TCA_FLOWER_KEY_ENC_IPV4_DST]); | |
352 | } | |
353 | if (attrs[TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK]) { | |
354 | flower->tunnel.ipv6.ipv6_src = | |
355 | nl_attr_get_in6_addr(attrs[TCA_FLOWER_KEY_ENC_IPV6_SRC]); | |
356 | } | |
357 | if (attrs[TCA_FLOWER_KEY_ENC_IPV6_DST_MASK]) { | |
358 | flower->tunnel.ipv6.ipv6_dst = | |
359 | nl_attr_get_in6_addr(attrs[TCA_FLOWER_KEY_ENC_IPV6_DST]); | |
360 | } | |
361 | if (attrs[TCA_FLOWER_KEY_ENC_UDP_DST_PORT]) { | |
362 | flower->tunnel.tp_dst = | |
363 | nl_attr_get_be16(attrs[TCA_FLOWER_KEY_ENC_UDP_DST_PORT]); | |
364 | } | |
365 | } | |
366 | ||
367 | static void | |
368 | nl_parse_flower_ip(struct nlattr **attrs, struct tc_flower *flower) { | |
369 | uint8_t ip_proto = 0; | |
370 | struct tc_flower_key *key = &flower->key; | |
371 | struct tc_flower_key *mask = &flower->mask; | |
372 | ||
373 | if (attrs[TCA_FLOWER_KEY_IP_PROTO]) { | |
374 | ip_proto = nl_attr_get_u8(attrs[TCA_FLOWER_KEY_IP_PROTO]); | |
375 | key->ip_proto = ip_proto; | |
376 | mask->ip_proto = UINT8_MAX; | |
377 | } | |
378 | ||
83e86606 | 379 | if (attrs[TCA_FLOWER_KEY_FLAGS_MASK]) { |
7e0f69b5 IS |
380 | key->flags = ntohl(nl_attr_get_be32(attrs[TCA_FLOWER_KEY_FLAGS])); |
381 | mask->flags = | |
382 | ntohl(nl_attr_get_be32(attrs[TCA_FLOWER_KEY_FLAGS_MASK])); | |
83e86606 RD |
383 | } |
384 | ||
f98e418f RD |
385 | if (attrs[TCA_FLOWER_KEY_IPV4_SRC_MASK]) { |
386 | key->ipv4.ipv4_src = | |
387 | nl_attr_get_be32(attrs[TCA_FLOWER_KEY_IPV4_SRC]); | |
388 | mask->ipv4.ipv4_src = | |
389 | nl_attr_get_be32(attrs[TCA_FLOWER_KEY_IPV4_SRC_MASK]); | |
390 | } | |
391 | if (attrs[TCA_FLOWER_KEY_IPV4_DST_MASK]) { | |
392 | key->ipv4.ipv4_dst = | |
393 | nl_attr_get_be32(attrs[TCA_FLOWER_KEY_IPV4_DST]); | |
394 | mask->ipv4.ipv4_dst = | |
395 | nl_attr_get_be32(attrs[TCA_FLOWER_KEY_IPV4_DST_MASK]); | |
396 | } | |
397 | if (attrs[TCA_FLOWER_KEY_IPV6_SRC_MASK]) { | |
398 | struct nlattr *attr = attrs[TCA_FLOWER_KEY_IPV6_SRC]; | |
399 | struct nlattr *attr_mask = attrs[TCA_FLOWER_KEY_IPV6_SRC_MASK]; | |
400 | ||
401 | key->ipv6.ipv6_src = nl_attr_get_in6_addr(attr); | |
402 | mask->ipv6.ipv6_src = nl_attr_get_in6_addr(attr_mask); | |
403 | } | |
404 | if (attrs[TCA_FLOWER_KEY_IPV6_DST_MASK]) { | |
405 | struct nlattr *attr = attrs[TCA_FLOWER_KEY_IPV6_DST]; | |
406 | struct nlattr *attr_mask = attrs[TCA_FLOWER_KEY_IPV6_DST_MASK]; | |
407 | ||
408 | key->ipv6.ipv6_dst = nl_attr_get_in6_addr(attr); | |
409 | mask->ipv6.ipv6_dst = nl_attr_get_in6_addr(attr_mask); | |
410 | } | |
411 | ||
412 | if (ip_proto == IPPROTO_TCP) { | |
413 | if (attrs[TCA_FLOWER_KEY_TCP_SRC_MASK]) { | |
2b1d9fa9 | 414 | key->tcp_src = |
f98e418f | 415 | nl_attr_get_be16(attrs[TCA_FLOWER_KEY_TCP_SRC]); |
2b1d9fa9 | 416 | mask->tcp_src = |
f98e418f RD |
417 | nl_attr_get_be16(attrs[TCA_FLOWER_KEY_TCP_SRC_MASK]); |
418 | } | |
419 | if (attrs[TCA_FLOWER_KEY_TCP_DST_MASK]) { | |
2b1d9fa9 | 420 | key->tcp_dst = |
f98e418f | 421 | nl_attr_get_be16(attrs[TCA_FLOWER_KEY_TCP_DST]); |
2b1d9fa9 | 422 | mask->tcp_dst = |
f98e418f RD |
423 | nl_attr_get_be16(attrs[TCA_FLOWER_KEY_TCP_DST_MASK]); |
424 | } | |
cd081043 PB |
425 | if (attrs[TCA_FLOWER_KEY_TCP_FLAGS_MASK]) { |
426 | key->tcp_flags = | |
427 | nl_attr_get_be16(attrs[TCA_FLOWER_KEY_TCP_FLAGS]); | |
428 | mask->tcp_flags = | |
429 | nl_attr_get_be16(attrs[TCA_FLOWER_KEY_TCP_FLAGS_MASK]); | |
430 | } | |
f98e418f RD |
431 | } else if (ip_proto == IPPROTO_UDP) { |
432 | if (attrs[TCA_FLOWER_KEY_UDP_SRC_MASK]) { | |
2b1d9fa9 PB |
433 | key->udp_src = nl_attr_get_be16(attrs[TCA_FLOWER_KEY_UDP_SRC]); |
434 | mask->udp_src = | |
f98e418f RD |
435 | nl_attr_get_be16(attrs[TCA_FLOWER_KEY_UDP_SRC_MASK]); |
436 | } | |
437 | if (attrs[TCA_FLOWER_KEY_UDP_DST_MASK]) { | |
2b1d9fa9 PB |
438 | key->udp_dst = nl_attr_get_be16(attrs[TCA_FLOWER_KEY_UDP_DST]); |
439 | mask->udp_dst = | |
f98e418f RD |
440 | nl_attr_get_be16(attrs[TCA_FLOWER_KEY_UDP_DST_MASK]); |
441 | } | |
4862b4e5 VB |
442 | } else if (ip_proto == IPPROTO_SCTP) { |
443 | if (attrs[TCA_FLOWER_KEY_SCTP_SRC_MASK]) { | |
2b1d9fa9 PB |
444 | key->sctp_src = nl_attr_get_be16(attrs[TCA_FLOWER_KEY_SCTP_SRC]); |
445 | mask->sctp_src = | |
4862b4e5 VB |
446 | nl_attr_get_be16(attrs[TCA_FLOWER_KEY_SCTP_SRC_MASK]); |
447 | } | |
448 | if (attrs[TCA_FLOWER_KEY_SCTP_DST_MASK]) { | |
2b1d9fa9 PB |
449 | key->sctp_dst = nl_attr_get_be16(attrs[TCA_FLOWER_KEY_SCTP_DST]); |
450 | mask->sctp_dst = | |
4862b4e5 VB |
451 | nl_attr_get_be16(attrs[TCA_FLOWER_KEY_SCTP_DST_MASK]); |
452 | } | |
f98e418f | 453 | } |
0b4b5203 PB |
454 | |
455 | if (attrs[TCA_FLOWER_KEY_IP_TTL_MASK]) { | |
456 | key->ip_ttl = nl_attr_get_u8(attrs[TCA_FLOWER_KEY_IP_TTL]); | |
457 | mask->ip_ttl = nl_attr_get_u8(attrs[TCA_FLOWER_KEY_IP_TTL_MASK]); | |
458 | } | |
f98e418f RD |
459 | } |
460 | ||
8ada482b PB |
461 | static const struct nl_policy pedit_policy[] = { |
462 | [TCA_PEDIT_PARMS_EX] = { .type = NL_A_UNSPEC, | |
463 | .min_len = sizeof(struct tc_pedit), | |
464 | .optional = false, }, | |
465 | [TCA_PEDIT_KEYS_EX] = { .type = NL_A_NESTED, | |
466 | .optional = false, }, | |
467 | }; | |
468 | ||
469 | static int | |
470 | nl_parse_act_pedit(struct nlattr *options, struct tc_flower *flower) | |
471 | { | |
472 | struct nlattr *pe_attrs[ARRAY_SIZE(pedit_policy)]; | |
473 | const struct tc_pedit *pe; | |
474 | const struct tc_pedit_key *keys; | |
475 | const struct nlattr *nla, *keys_ex, *ex_type; | |
476 | const void *keys_attr; | |
477 | char *rewrite_key = (void *) &flower->rewrite.key; | |
478 | char *rewrite_mask = (void *) &flower->rewrite.mask; | |
479 | size_t keys_ex_size, left; | |
d6118e62 | 480 | int type, i = 0, err; |
8ada482b PB |
481 | |
482 | if (!nl_parse_nested(options, pedit_policy, pe_attrs, | |
483 | ARRAY_SIZE(pedit_policy))) { | |
484 | VLOG_ERR_RL(&error_rl, "failed to parse pedit action options"); | |
485 | return EPROTO; | |
486 | } | |
487 | ||
488 | pe = nl_attr_get_unspec(pe_attrs[TCA_PEDIT_PARMS_EX], sizeof *pe); | |
489 | keys = pe->keys; | |
490 | keys_attr = pe_attrs[TCA_PEDIT_KEYS_EX]; | |
491 | keys_ex = nl_attr_get(keys_attr); | |
492 | keys_ex_size = nl_attr_get_size(keys_attr); | |
493 | ||
494 | NL_ATTR_FOR_EACH (nla, left, keys_ex, keys_ex_size) { | |
495 | if (i >= pe->nkeys) { | |
496 | break; | |
497 | } | |
498 | ||
408671c4 | 499 | if (nl_attr_type(nla) != TCA_PEDIT_KEY_EX) { |
8ada482b PB |
500 | VLOG_ERR_RL(&error_rl, "unable to parse legacy pedit type: %d", |
501 | nl_attr_type(nla)); | |
502 | return EOPNOTSUPP; | |
503 | } | |
504 | ||
505 | ex_type = nl_attr_find_nested(nla, TCA_PEDIT_KEY_EX_HTYPE); | |
506 | type = nl_attr_get_u16(ex_type); | |
507 | ||
d6118e62 PB |
508 | err = csum_update_flag(flower, type); |
509 | if (err) { | |
510 | return err; | |
511 | } | |
512 | ||
8ada482b PB |
513 | for (int j = 0; j < ARRAY_SIZE(flower_pedit_map); j++) { |
514 | struct flower_key_to_pedit *m = &flower_pedit_map[j]; | |
515 | int flower_off = m->flower_offset; | |
516 | int sz = m->size; | |
517 | int mf = m->offset; | |
518 | ||
519 | if (m->htype != type) { | |
520 | continue; | |
521 | } | |
522 | ||
523 | /* check overlap between current pedit key, which is always | |
524 | * 4 bytes (range [off, off + 3]), and a map entry in | |
525 | * flower_pedit_map (range [mf, mf + sz - 1]) */ | |
526 | if ((keys->off >= mf && keys->off < mf + sz) | |
527 | || (keys->off + 3 >= mf && keys->off + 3 < mf + sz)) { | |
528 | int diff = flower_off + (keys->off - mf); | |
529 | uint32_t *dst = (void *) (rewrite_key + diff); | |
530 | uint32_t *dst_m = (void *) (rewrite_mask + diff); | |
531 | uint32_t mask = ~(keys->mask); | |
532 | uint32_t zero_bits; | |
533 | ||
534 | if (keys->off < mf) { | |
535 | zero_bits = 8 * (mf - keys->off); | |
536 | mask &= UINT32_MAX << zero_bits; | |
537 | } else if (keys->off + 4 > mf + m->size) { | |
538 | zero_bits = 8 * (keys->off + 4 - mf - m->size); | |
539 | mask &= UINT32_MAX >> zero_bits; | |
540 | } | |
541 | ||
542 | *dst_m |= mask; | |
543 | *dst |= keys->val & mask; | |
544 | } | |
545 | } | |
546 | ||
547 | keys++; | |
548 | i++; | |
549 | } | |
550 | ||
551 | flower->rewrite.rewrite = true; | |
552 | ||
553 | return 0; | |
554 | } | |
555 | ||
f98e418f RD |
556 | static const struct nl_policy tunnel_key_policy[] = { |
557 | [TCA_TUNNEL_KEY_PARMS] = { .type = NL_A_UNSPEC, | |
558 | .min_len = sizeof(struct tc_tunnel_key), | |
559 | .optional = false, }, | |
560 | [TCA_TUNNEL_KEY_ENC_IPV4_SRC] = { .type = NL_A_U32, .optional = true, }, | |
561 | [TCA_TUNNEL_KEY_ENC_IPV4_DST] = { .type = NL_A_U32, .optional = true, }, | |
562 | [TCA_TUNNEL_KEY_ENC_IPV6_SRC] = { .type = NL_A_UNSPEC, | |
563 | .min_len = sizeof(struct in6_addr), | |
564 | .optional = true, }, | |
565 | [TCA_TUNNEL_KEY_ENC_IPV6_DST] = { .type = NL_A_UNSPEC, | |
566 | .min_len = sizeof(struct in6_addr), | |
567 | .optional = true, }, | |
568 | [TCA_TUNNEL_KEY_ENC_KEY_ID] = { .type = NL_A_U32, .optional = true, }, | |
569 | [TCA_TUNNEL_KEY_ENC_DST_PORT] = { .type = NL_A_U16, .optional = true, }, | |
570 | }; | |
571 | ||
572 | static int | |
573 | nl_parse_act_tunnel_key(struct nlattr *options, struct tc_flower *flower) | |
574 | { | |
575 | struct nlattr *tun_attrs[ARRAY_SIZE(tunnel_key_policy)]; | |
576 | const struct nlattr *tun_parms; | |
577 | const struct tc_tunnel_key *tun; | |
578 | ||
579 | if (!nl_parse_nested(options, tunnel_key_policy, tun_attrs, | |
580 | ARRAY_SIZE(tunnel_key_policy))) { | |
581 | VLOG_ERR_RL(&error_rl, "failed to parse tunnel_key action options"); | |
582 | return EPROTO; | |
583 | } | |
584 | ||
585 | tun_parms = tun_attrs[TCA_TUNNEL_KEY_PARMS]; | |
586 | tun = nl_attr_get_unspec(tun_parms, sizeof *tun); | |
587 | if (tun->t_action == TCA_TUNNEL_KEY_ACT_SET) { | |
588 | struct nlattr *id = tun_attrs[TCA_TUNNEL_KEY_ENC_KEY_ID]; | |
589 | struct nlattr *dst_port = tun_attrs[TCA_TUNNEL_KEY_ENC_DST_PORT]; | |
590 | struct nlattr *ipv4_src = tun_attrs[TCA_TUNNEL_KEY_ENC_IPV4_SRC]; | |
591 | struct nlattr *ipv4_dst = tun_attrs[TCA_TUNNEL_KEY_ENC_IPV4_DST]; | |
592 | struct nlattr *ipv6_src = tun_attrs[TCA_TUNNEL_KEY_ENC_IPV6_SRC]; | |
593 | struct nlattr *ipv6_dst = tun_attrs[TCA_TUNNEL_KEY_ENC_IPV6_DST]; | |
594 | ||
595 | flower->set.set = true; | |
596 | flower->set.ipv4.ipv4_src = ipv4_src ? nl_attr_get_be32(ipv4_src) : 0; | |
597 | flower->set.ipv4.ipv4_dst = ipv4_dst ? nl_attr_get_be32(ipv4_dst) : 0; | |
598 | if (ipv6_src) { | |
599 | flower->set.ipv6.ipv6_src = nl_attr_get_in6_addr(ipv6_src); | |
600 | } | |
601 | if (ipv6_dst) { | |
602 | flower->set.ipv6.ipv6_dst = nl_attr_get_in6_addr(ipv6_dst); | |
603 | } | |
604 | flower->set.id = id ? be32_to_be64(nl_attr_get_be32(id)) : 0; | |
605 | flower->set.tp_dst = dst_port ? nl_attr_get_be16(dst_port) : 0; | |
606 | } else if (tun->t_action == TCA_TUNNEL_KEY_ACT_RELEASE) { | |
607 | flower->tunnel.tunnel = true; | |
608 | } else { | |
609 | VLOG_ERR_RL(&error_rl, "unknown tunnel actions: %d, %d", | |
610 | tun->action, tun->t_action); | |
611 | return EINVAL; | |
612 | } | |
613 | return 0; | |
614 | } | |
615 | ||
616 | static const struct nl_policy gact_policy[] = { | |
617 | [TCA_GACT_PARMS] = { .type = NL_A_UNSPEC, | |
618 | .min_len = sizeof(struct tc_gact), | |
619 | .optional = false, }, | |
620 | [TCA_GACT_TM] = { .type = NL_A_UNSPEC, | |
621 | .min_len = sizeof(struct tcf_t), | |
622 | .optional = false, }, | |
623 | }; | |
624 | ||
8c1e74d1 PB |
625 | static int |
626 | get_user_hz(void) | |
627 | { | |
628 | static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; | |
629 | static int user_hz = 100; | |
630 | ||
631 | if (ovsthread_once_start(&once)) { | |
632 | user_hz = sysconf(_SC_CLK_TCK); | |
633 | ovsthread_once_done(&once); | |
634 | } | |
635 | ||
636 | return user_hz; | |
637 | } | |
f98e418f RD |
638 | |
639 | static void | |
640 | nl_parse_tcf(const struct tcf_t *tm, struct tc_flower *flower) | |
641 | { | |
8c1e74d1 | 642 | flower->lastused = time_msec() - (tm->lastuse * 1000 / get_user_hz()); |
f98e418f RD |
643 | } |
644 | ||
645 | static int | |
646 | nl_parse_act_drop(struct nlattr *options, struct tc_flower *flower) | |
647 | { | |
648 | struct nlattr *gact_attrs[ARRAY_SIZE(gact_policy)]; | |
649 | const struct tc_gact *p; | |
650 | struct nlattr *gact_parms; | |
651 | const struct tcf_t *tm; | |
652 | ||
653 | if (!nl_parse_nested(options, gact_policy, gact_attrs, | |
654 | ARRAY_SIZE(gact_policy))) { | |
655 | VLOG_ERR_RL(&error_rl, "failed to parse gact action options"); | |
656 | return EPROTO; | |
657 | } | |
658 | ||
659 | gact_parms = gact_attrs[TCA_GACT_PARMS]; | |
660 | p = nl_attr_get_unspec(gact_parms, sizeof *p); | |
661 | ||
662 | if (p->action != TC_ACT_SHOT) { | |
663 | VLOG_ERR_RL(&error_rl, "unknown gact action: %d", p->action); | |
664 | return EINVAL; | |
665 | } | |
666 | ||
667 | tm = nl_attr_get_unspec(gact_attrs[TCA_GACT_TM], sizeof *tm); | |
668 | nl_parse_tcf(tm, flower); | |
669 | ||
670 | return 0; | |
671 | } | |
672 | ||
673 | static const struct nl_policy mirred_policy[] = { | |
674 | [TCA_MIRRED_PARMS] = { .type = NL_A_UNSPEC, | |
675 | .min_len = sizeof(struct tc_mirred), | |
676 | .optional = false, }, | |
677 | [TCA_MIRRED_TM] = { .type = NL_A_UNSPEC, | |
678 | .min_len = sizeof(struct tcf_t), | |
679 | .optional = false, }, | |
680 | }; | |
681 | ||
682 | static int | |
683 | nl_parse_act_mirred(struct nlattr *options, struct tc_flower *flower) | |
684 | { | |
685 | ||
686 | struct nlattr *mirred_attrs[ARRAY_SIZE(mirred_policy)]; | |
687 | const struct tc_mirred *m; | |
688 | const struct nlattr *mirred_parms; | |
689 | const struct tcf_t *tm; | |
690 | struct nlattr *mirred_tm; | |
691 | ||
692 | if (!nl_parse_nested(options, mirred_policy, mirred_attrs, | |
693 | ARRAY_SIZE(mirred_policy))) { | |
694 | VLOG_ERR_RL(&error_rl, "failed to parse mirred action options"); | |
695 | return EPROTO; | |
696 | } | |
697 | ||
698 | mirred_parms = mirred_attrs[TCA_MIRRED_PARMS]; | |
699 | m = nl_attr_get_unspec(mirred_parms, sizeof *m); | |
700 | ||
701 | if (m->action != TC_ACT_STOLEN || m->eaction != TCA_EGRESS_REDIR) { | |
702 | VLOG_ERR_RL(&error_rl, "unknown mirred action: %d, %d, %d", | |
703 | m->action, m->eaction, m->ifindex); | |
704 | return EINVAL; | |
705 | } | |
706 | ||
707 | flower->ifindex_out = m->ifindex; | |
708 | ||
709 | mirred_tm = mirred_attrs[TCA_MIRRED_TM]; | |
710 | tm = nl_attr_get_unspec(mirred_tm, sizeof *tm); | |
711 | nl_parse_tcf(tm, flower); | |
712 | ||
713 | return 0; | |
714 | } | |
715 | ||
716 | static const struct nl_policy vlan_policy[] = { | |
717 | [TCA_VLAN_PARMS] = { .type = NL_A_UNSPEC, | |
718 | .min_len = sizeof(struct tc_vlan), | |
719 | .optional = false, }, | |
720 | [TCA_VLAN_PUSH_VLAN_ID] = { .type = NL_A_U16, .optional = true, }, | |
721 | [TCA_VLAN_PUSH_VLAN_PROTOCOL] = { .type = NL_A_U16, .optional = true, }, | |
722 | [TCA_VLAN_PUSH_VLAN_PRIORITY] = { .type = NL_A_U8, .optional = true, }, | |
723 | }; | |
724 | ||
725 | static int | |
726 | nl_parse_act_vlan(struct nlattr *options, struct tc_flower *flower) | |
727 | { | |
728 | struct nlattr *vlan_attrs[ARRAY_SIZE(vlan_policy)]; | |
729 | const struct tc_vlan *v; | |
730 | const struct nlattr *vlan_parms; | |
731 | ||
732 | if (!nl_parse_nested(options, vlan_policy, vlan_attrs, | |
733 | ARRAY_SIZE(vlan_policy))) { | |
734 | VLOG_ERR_RL(&error_rl, "failed to parse vlan action options"); | |
735 | return EPROTO; | |
736 | } | |
737 | ||
738 | vlan_parms = vlan_attrs[TCA_VLAN_PARMS]; | |
739 | v = nl_attr_get_unspec(vlan_parms, sizeof *v); | |
740 | if (v->v_action == TCA_VLAN_ACT_PUSH) { | |
741 | struct nlattr *vlan_id = vlan_attrs[TCA_VLAN_PUSH_VLAN_ID]; | |
742 | struct nlattr *vlan_prio = vlan_attrs[TCA_VLAN_PUSH_VLAN_PRIORITY]; | |
743 | ||
744 | flower->vlan_push_id = nl_attr_get_u16(vlan_id); | |
745 | flower->vlan_push_prio = vlan_prio ? nl_attr_get_u8(vlan_prio) : 0; | |
746 | } else if (v->v_action == TCA_VLAN_ACT_POP) { | |
747 | flower->vlan_pop = 1; | |
748 | } else { | |
749 | VLOG_ERR_RL(&error_rl, "unknown vlan action: %d, %d", | |
750 | v->action, v->v_action); | |
751 | return EINVAL; | |
752 | } | |
753 | return 0; | |
754 | } | |
755 | ||
d6118e62 PB |
756 | static const struct nl_policy csum_policy[] = { |
757 | [TCA_CSUM_PARMS] = { .type = NL_A_UNSPEC, | |
758 | .min_len = sizeof(struct tc_csum), | |
759 | .optional = false, }, | |
760 | }; | |
761 | ||
762 | static int | |
763 | nl_parse_act_csum(struct nlattr *options, struct tc_flower *flower) | |
764 | { | |
765 | struct nlattr *csum_attrs[ARRAY_SIZE(csum_policy)]; | |
766 | const struct tc_csum *c; | |
767 | const struct nlattr *csum_parms; | |
768 | ||
769 | if (!nl_parse_nested(options, csum_policy, csum_attrs, | |
770 | ARRAY_SIZE(csum_policy))) { | |
771 | VLOG_ERR_RL(&error_rl, "failed to parse csum action options"); | |
772 | return EPROTO; | |
773 | } | |
774 | ||
775 | csum_parms = csum_attrs[TCA_CSUM_PARMS]; | |
776 | c = nl_attr_get_unspec(csum_parms, sizeof *c); | |
777 | ||
778 | /* sanity checks */ | |
779 | if (c->update_flags != flower->csum_update_flags) { | |
780 | VLOG_WARN_RL(&error_rl, | |
781 | "expected different act csum flags: 0x%x != 0x%x", | |
782 | flower->csum_update_flags, c->update_flags); | |
783 | return EINVAL; | |
784 | } | |
785 | flower->csum_update_flags = 0; /* so we know csum was handled */ | |
786 | ||
787 | if (flower->needs_full_ip_proto_mask | |
788 | && flower->mask.ip_proto != UINT8_MAX) { | |
789 | VLOG_WARN_RL(&error_rl, "expected full matching on flower ip_proto"); | |
790 | return EINVAL; | |
791 | } | |
792 | ||
793 | return 0; | |
794 | } | |
795 | ||
f98e418f RD |
796 | static const struct nl_policy act_policy[] = { |
797 | [TCA_ACT_KIND] = { .type = NL_A_STRING, .optional = false, }, | |
798 | [TCA_ACT_COOKIE] = { .type = NL_A_UNSPEC, .optional = true, }, | |
799 | [TCA_ACT_OPTIONS] = { .type = NL_A_NESTED, .optional = false, }, | |
800 | [TCA_ACT_STATS] = { .type = NL_A_NESTED, .optional = false, }, | |
801 | }; | |
802 | ||
803 | static const struct nl_policy stats_policy[] = { | |
804 | [TCA_STATS_BASIC] = { .type = NL_A_UNSPEC, | |
805 | .min_len = sizeof(struct gnet_stats_basic), | |
806 | .optional = false, }, | |
807 | }; | |
808 | ||
809 | static int | |
810 | nl_parse_single_action(struct nlattr *action, struct tc_flower *flower) | |
811 | { | |
812 | struct nlattr *act_options; | |
813 | struct nlattr *act_stats; | |
814 | struct nlattr *act_cookie; | |
815 | const char *act_kind; | |
816 | struct nlattr *action_attrs[ARRAY_SIZE(act_policy)]; | |
817 | struct nlattr *stats_attrs[ARRAY_SIZE(stats_policy)]; | |
818 | struct ovs_flow_stats *stats = &flower->stats; | |
819 | const struct gnet_stats_basic *bs; | |
40c5aa11 | 820 | int err = 0; |
f98e418f RD |
821 | |
822 | if (!nl_parse_nested(action, act_policy, action_attrs, | |
823 | ARRAY_SIZE(act_policy))) { | |
824 | VLOG_ERR_RL(&error_rl, "failed to parse single action options"); | |
825 | return EPROTO; | |
826 | } | |
827 | ||
828 | act_kind = nl_attr_get_string(action_attrs[TCA_ACT_KIND]); | |
829 | act_options = action_attrs[TCA_ACT_OPTIONS]; | |
830 | act_cookie = action_attrs[TCA_ACT_COOKIE]; | |
831 | ||
832 | if (!strcmp(act_kind, "gact")) { | |
40c5aa11 | 833 | err = nl_parse_act_drop(act_options, flower); |
f98e418f | 834 | } else if (!strcmp(act_kind, "mirred")) { |
40c5aa11 | 835 | err = nl_parse_act_mirred(act_options, flower); |
f98e418f | 836 | } else if (!strcmp(act_kind, "vlan")) { |
40c5aa11 | 837 | err = nl_parse_act_vlan(act_options, flower); |
f98e418f | 838 | } else if (!strcmp(act_kind, "tunnel_key")) { |
40c5aa11 | 839 | err = nl_parse_act_tunnel_key(act_options, flower); |
8ada482b | 840 | } else if (!strcmp(act_kind, "pedit")) { |
40c5aa11 | 841 | err = nl_parse_act_pedit(act_options, flower); |
8ada482b | 842 | } else if (!strcmp(act_kind, "csum")) { |
d6118e62 | 843 | nl_parse_act_csum(act_options, flower); |
f98e418f RD |
844 | } else { |
845 | VLOG_ERR_RL(&error_rl, "unknown tc action kind: %s", act_kind); | |
40c5aa11 RD |
846 | err = EINVAL; |
847 | } | |
848 | ||
849 | if (err) { | |
850 | return err; | |
f98e418f RD |
851 | } |
852 | ||
853 | if (act_cookie) { | |
854 | flower->act_cookie.data = nl_attr_get(act_cookie); | |
855 | flower->act_cookie.len = nl_attr_get_size(act_cookie); | |
856 | } | |
857 | ||
858 | act_stats = action_attrs[TCA_ACT_STATS]; | |
859 | ||
860 | if (!nl_parse_nested(act_stats, stats_policy, stats_attrs, | |
861 | ARRAY_SIZE(stats_policy))) { | |
862 | VLOG_ERR_RL(&error_rl, "failed to parse action stats policy"); | |
863 | return EPROTO; | |
864 | } | |
865 | ||
866 | bs = nl_attr_get_unspec(stats_attrs[TCA_STATS_BASIC], sizeof *bs); | |
867 | put_32aligned_u64(&stats->n_packets, bs->packets); | |
868 | put_32aligned_u64(&stats->n_bytes, bs->bytes); | |
869 | ||
870 | return 0; | |
871 | } | |
872 | ||
873 | #define TCA_ACT_MIN_PRIO 1 | |
874 | ||
875 | static int | |
876 | nl_parse_flower_actions(struct nlattr **attrs, struct tc_flower *flower) | |
877 | { | |
878 | const struct nlattr *actions = attrs[TCA_FLOWER_ACT]; | |
879 | static struct nl_policy actions_orders_policy[TCA_ACT_MAX_PRIO + 1] = {}; | |
880 | struct nlattr *actions_orders[ARRAY_SIZE(actions_orders_policy)]; | |
881 | const int max_size = ARRAY_SIZE(actions_orders_policy); | |
882 | ||
883 | for (int i = TCA_ACT_MIN_PRIO; i < max_size; i++) { | |
884 | actions_orders_policy[i].type = NL_A_NESTED; | |
885 | actions_orders_policy[i].optional = true; | |
886 | } | |
887 | ||
888 | if (!nl_parse_nested(actions, actions_orders_policy, actions_orders, | |
889 | ARRAY_SIZE(actions_orders_policy))) { | |
890 | VLOG_ERR_RL(&error_rl, "failed to parse flower order of actions"); | |
891 | return EPROTO; | |
892 | } | |
893 | ||
894 | for (int i = TCA_ACT_MIN_PRIO; i < max_size; i++) { | |
895 | if (actions_orders[i]) { | |
896 | int err = nl_parse_single_action(actions_orders[i], flower); | |
897 | ||
898 | if (err) { | |
899 | return err; | |
900 | } | |
901 | } | |
902 | } | |
903 | ||
d6118e62 PB |
904 | if (flower->csum_update_flags) { |
905 | VLOG_WARN_RL(&error_rl, | |
906 | "expected act csum with flags: 0x%x", | |
907 | flower->csum_update_flags); | |
908 | return EINVAL; | |
909 | } | |
910 | ||
f98e418f RD |
911 | return 0; |
912 | } | |
913 | ||
914 | static int | |
915 | nl_parse_flower_options(struct nlattr *nl_options, struct tc_flower *flower) | |
916 | { | |
917 | struct nlattr *attrs[ARRAY_SIZE(tca_flower_policy)]; | |
918 | ||
919 | if (!nl_parse_nested(nl_options, tca_flower_policy, | |
920 | attrs, ARRAY_SIZE(tca_flower_policy))) { | |
921 | VLOG_ERR_RL(&error_rl, "failed to parse flower classifier options"); | |
922 | return EPROTO; | |
923 | } | |
924 | ||
925 | nl_parse_flower_eth(attrs, flower); | |
926 | nl_parse_flower_vlan(attrs, flower); | |
927 | nl_parse_flower_ip(attrs, flower); | |
928 | nl_parse_flower_tunnel(attrs, flower); | |
929 | return nl_parse_flower_actions(attrs, flower); | |
930 | } | |
931 | ||
932 | int | |
933 | parse_netlink_to_tc_flower(struct ofpbuf *reply, struct tc_flower *flower) | |
934 | { | |
935 | struct tcmsg *tc; | |
936 | struct nlattr *ta[ARRAY_SIZE(tca_policy)]; | |
937 | const char *kind; | |
938 | ||
939 | if (NLMSG_HDRLEN + sizeof *tc > reply->size) { | |
940 | return EPROTO; | |
941 | } | |
942 | ||
943 | memset(flower, 0, sizeof *flower); | |
944 | ||
945 | tc = ofpbuf_at_assert(reply, NLMSG_HDRLEN, sizeof *tc); | |
946 | flower->handle = tc->tcm_handle; | |
947 | flower->key.eth_type = (OVS_FORCE ovs_be16) tc_get_minor(tc->tcm_info); | |
948 | flower->mask.eth_type = OVS_BE16_MAX; | |
949 | flower->prio = tc_get_major(tc->tcm_info); | |
950 | ||
951 | if (!flower->handle) { | |
952 | return EAGAIN; | |
953 | } | |
954 | ||
955 | if (!nl_policy_parse(reply, NLMSG_HDRLEN + sizeof *tc, | |
956 | tca_policy, ta, ARRAY_SIZE(ta))) { | |
957 | VLOG_ERR_RL(&error_rl, "failed to parse tca policy"); | |
958 | return EPROTO; | |
959 | } | |
960 | ||
961 | kind = nl_attr_get_string(ta[TCA_KIND]); | |
962 | if (strcmp(kind, "flower")) { | |
963 | VLOG_ERR_RL(&error_rl, "failed to parse filter: %s", kind); | |
964 | return EPROTO; | |
965 | } | |
966 | ||
967 | return nl_parse_flower_options(ta[TCA_OPTIONS], flower); | |
968 | } | |
969 | ||
970 | int | |
971 | tc_dump_flower_start(int ifindex, struct nl_dump *dump) | |
972 | { | |
973 | struct ofpbuf request; | |
974 | struct tcmsg *tcmsg; | |
975 | ||
976 | tcmsg = tc_make_request(ifindex, RTM_GETTFILTER, NLM_F_DUMP, &request); | |
977 | tcmsg->tcm_parent = TC_INGRESS_PARENT; | |
978 | tcmsg->tcm_info = TC_H_UNSPEC; | |
979 | tcmsg->tcm_handle = 0; | |
980 | ||
981 | nl_dump_start(dump, NETLINK_ROUTE, &request); | |
982 | ofpbuf_uninit(&request); | |
983 | ||
984 | return 0; | |
985 | } | |
986 | ||
987 | int | |
988 | tc_flush(int ifindex) | |
989 | { | |
990 | struct ofpbuf request; | |
991 | struct tcmsg *tcmsg; | |
992 | ||
993 | tcmsg = tc_make_request(ifindex, RTM_DELTFILTER, NLM_F_ACK, &request); | |
994 | tcmsg->tcm_parent = TC_INGRESS_PARENT; | |
995 | tcmsg->tcm_info = TC_H_UNSPEC; | |
996 | ||
997 | return tc_transact(&request, NULL); | |
998 | } | |
999 | ||
1000 | int | |
1001 | tc_del_filter(int ifindex, int prio, int handle) | |
1002 | { | |
1003 | struct ofpbuf request; | |
1004 | struct tcmsg *tcmsg; | |
1005 | struct ofpbuf *reply; | |
1006 | int error; | |
1007 | ||
1008 | tcmsg = tc_make_request(ifindex, RTM_DELTFILTER, NLM_F_ECHO, &request); | |
1009 | tcmsg->tcm_parent = TC_INGRESS_PARENT; | |
1010 | tcmsg->tcm_info = tc_make_handle(prio, 0); | |
1011 | tcmsg->tcm_handle = handle; | |
1012 | ||
1013 | error = tc_transact(&request, &reply); | |
1014 | if (!error) { | |
1015 | ofpbuf_delete(reply); | |
1016 | } | |
1017 | return error; | |
1018 | } | |
1019 | ||
1020 | int | |
1021 | tc_get_flower(int ifindex, int prio, int handle, struct tc_flower *flower) | |
1022 | { | |
1023 | struct ofpbuf request; | |
1024 | struct tcmsg *tcmsg; | |
1025 | struct ofpbuf *reply; | |
1026 | int error; | |
1027 | ||
1028 | tcmsg = tc_make_request(ifindex, RTM_GETTFILTER, NLM_F_ECHO, &request); | |
1029 | tcmsg->tcm_parent = TC_INGRESS_PARENT; | |
1030 | tcmsg->tcm_info = tc_make_handle(prio, 0); | |
1031 | tcmsg->tcm_handle = handle; | |
1032 | ||
1033 | error = tc_transact(&request, &reply); | |
1034 | if (error) { | |
1035 | return error; | |
1036 | } | |
1037 | ||
1038 | error = parse_netlink_to_tc_flower(reply, flower); | |
1039 | ofpbuf_delete(reply); | |
1040 | return error; | |
1041 | } | |
1042 | ||
691d20cb PB |
1043 | static int |
1044 | tc_get_tc_cls_policy(enum tc_offload_policy policy) | |
1045 | { | |
1046 | if (policy == TC_POLICY_SKIP_HW) { | |
1047 | return TCA_CLS_FLAGS_SKIP_HW; | |
1048 | } else if (policy == TC_POLICY_SKIP_SW) { | |
1049 | return TCA_CLS_FLAGS_SKIP_SW; | |
1050 | } | |
1051 | ||
1052 | return 0; | |
1053 | } | |
1054 | ||
8ada482b PB |
1055 | static void |
1056 | nl_msg_put_act_csum(struct ofpbuf *request, uint32_t flags) | |
1057 | { | |
1058 | size_t offset; | |
1059 | ||
1060 | nl_msg_put_string(request, TCA_ACT_KIND, "csum"); | |
1061 | offset = nl_msg_start_nested(request, TCA_ACT_OPTIONS); | |
1062 | { | |
1063 | struct tc_csum parm = { .action = TC_ACT_PIPE, | |
1064 | .update_flags = flags }; | |
1065 | ||
1066 | nl_msg_put_unspec(request, TCA_CSUM_PARMS, &parm, sizeof parm); | |
1067 | } | |
1068 | nl_msg_end_nested(request, offset); | |
1069 | } | |
1070 | ||
1071 | static void | |
1072 | nl_msg_put_act_pedit(struct ofpbuf *request, struct tc_pedit *parm, | |
1073 | struct tc_pedit_key_ex *ex) | |
1074 | { | |
e13bbbab | 1075 | size_t ksize = sizeof *parm + parm->nkeys * sizeof(struct tc_pedit_key); |
8ada482b PB |
1076 | size_t offset, offset_keys_ex, offset_key; |
1077 | int i; | |
1078 | ||
1079 | nl_msg_put_string(request, TCA_ACT_KIND, "pedit"); | |
1080 | offset = nl_msg_start_nested(request, TCA_ACT_OPTIONS); | |
1081 | { | |
1082 | parm->action = TC_ACT_PIPE; | |
1083 | ||
1084 | nl_msg_put_unspec(request, TCA_PEDIT_PARMS_EX, parm, ksize); | |
1085 | offset_keys_ex = nl_msg_start_nested(request, TCA_PEDIT_KEYS_EX); | |
1086 | for (i = 0; i < parm->nkeys; i++, ex++) { | |
1087 | offset_key = nl_msg_start_nested(request, TCA_PEDIT_KEY_EX); | |
1088 | nl_msg_put_u16(request, TCA_PEDIT_KEY_EX_HTYPE, ex->htype); | |
1089 | nl_msg_put_u16(request, TCA_PEDIT_KEY_EX_CMD, ex->cmd); | |
1090 | nl_msg_end_nested(request, offset_key); | |
1091 | } | |
1092 | nl_msg_end_nested(request, offset_keys_ex); | |
1093 | } | |
1094 | nl_msg_end_nested(request, offset); | |
1095 | } | |
1096 | ||
f98e418f RD |
1097 | static void |
1098 | nl_msg_put_act_push_vlan(struct ofpbuf *request, uint16_t vid, uint8_t prio) | |
1099 | { | |
1100 | size_t offset; | |
1101 | ||
1102 | nl_msg_put_string(request, TCA_ACT_KIND, "vlan"); | |
1103 | offset = nl_msg_start_nested(request, TCA_ACT_OPTIONS); | |
1104 | { | |
1105 | struct tc_vlan parm = { .action = TC_ACT_PIPE, | |
1106 | .v_action = TCA_VLAN_ACT_PUSH }; | |
1107 | ||
1108 | nl_msg_put_unspec(request, TCA_VLAN_PARMS, &parm, sizeof parm); | |
1109 | nl_msg_put_u16(request, TCA_VLAN_PUSH_VLAN_ID, vid); | |
1110 | nl_msg_put_u8(request, TCA_VLAN_PUSH_VLAN_PRIORITY, prio); | |
1111 | } | |
1112 | nl_msg_end_nested(request, offset); | |
1113 | } | |
1114 | ||
1115 | static void | |
1116 | nl_msg_put_act_pop_vlan(struct ofpbuf *request) | |
1117 | { | |
1118 | size_t offset; | |
1119 | ||
1120 | nl_msg_put_string(request, TCA_ACT_KIND, "vlan"); | |
1121 | offset = nl_msg_start_nested(request, TCA_ACT_OPTIONS); | |
1122 | { | |
1123 | struct tc_vlan parm = { .action = TC_ACT_PIPE, | |
1124 | .v_action = TCA_VLAN_ACT_POP }; | |
1125 | ||
1126 | nl_msg_put_unspec(request, TCA_VLAN_PARMS, &parm, sizeof parm); | |
1127 | } | |
1128 | nl_msg_end_nested(request, offset); | |
1129 | } | |
1130 | ||
1131 | static void | |
1132 | nl_msg_put_act_tunnel_key_release(struct ofpbuf *request) | |
1133 | { | |
1134 | size_t offset; | |
1135 | ||
1136 | nl_msg_put_string(request, TCA_ACT_KIND, "tunnel_key"); | |
1137 | offset = nl_msg_start_nested(request, TCA_ACT_OPTIONS); | |
1138 | { | |
1139 | struct tc_tunnel_key tun = { .action = TC_ACT_PIPE, | |
1140 | .t_action = TCA_TUNNEL_KEY_ACT_RELEASE }; | |
1141 | ||
1142 | nl_msg_put_unspec(request, TCA_TUNNEL_KEY_PARMS, &tun, sizeof tun); | |
1143 | } | |
1144 | nl_msg_end_nested(request, offset); | |
1145 | } | |
1146 | ||
1147 | static void | |
1148 | nl_msg_put_act_tunnel_key_set(struct ofpbuf *request, ovs_be64 id, | |
1149 | ovs_be32 ipv4_src, ovs_be32 ipv4_dst, | |
1150 | struct in6_addr *ipv6_src, | |
1151 | struct in6_addr *ipv6_dst, | |
1152 | ovs_be16 tp_dst) | |
1153 | { | |
1154 | size_t offset; | |
1155 | ||
1156 | nl_msg_put_string(request, TCA_ACT_KIND, "tunnel_key"); | |
1157 | offset = nl_msg_start_nested(request, TCA_ACT_OPTIONS); | |
1158 | { | |
1159 | struct tc_tunnel_key tun = { .action = TC_ACT_PIPE, | |
1160 | .t_action = TCA_TUNNEL_KEY_ACT_SET }; | |
1161 | ||
1162 | nl_msg_put_unspec(request, TCA_TUNNEL_KEY_PARMS, &tun, sizeof tun); | |
1163 | ||
1164 | ovs_be32 id32 = be64_to_be32(id); | |
1165 | nl_msg_put_be32(request, TCA_TUNNEL_KEY_ENC_KEY_ID, id32); | |
1166 | if (ipv4_dst) { | |
1167 | nl_msg_put_be32(request, TCA_TUNNEL_KEY_ENC_IPV4_SRC, ipv4_src); | |
1168 | nl_msg_put_be32(request, TCA_TUNNEL_KEY_ENC_IPV4_DST, ipv4_dst); | |
1169 | } else if (!is_all_zeros(ipv6_dst, sizeof *ipv6_dst)) { | |
1170 | nl_msg_put_in6_addr(request, TCA_TUNNEL_KEY_ENC_IPV6_DST, | |
1171 | ipv6_dst); | |
1172 | nl_msg_put_in6_addr(request, TCA_TUNNEL_KEY_ENC_IPV6_SRC, | |
1173 | ipv6_src); | |
1174 | } | |
1175 | nl_msg_put_be16(request, TCA_TUNNEL_KEY_ENC_DST_PORT, tp_dst); | |
1176 | } | |
1177 | nl_msg_end_nested(request, offset); | |
1178 | } | |
1179 | ||
1180 | static void | |
1181 | nl_msg_put_act_drop(struct ofpbuf *request) | |
1182 | { | |
1183 | size_t offset; | |
1184 | ||
1185 | nl_msg_put_string(request, TCA_ACT_KIND, "gact"); | |
1186 | offset = nl_msg_start_nested(request, TCA_ACT_OPTIONS); | |
1187 | { | |
1188 | struct tc_gact p = { .action = TC_ACT_SHOT }; | |
1189 | ||
1190 | nl_msg_put_unspec(request, TCA_GACT_PARMS, &p, sizeof p); | |
1191 | } | |
1192 | nl_msg_end_nested(request, offset); | |
1193 | } | |
1194 | ||
1195 | static void | |
1196 | nl_msg_put_act_redirect(struct ofpbuf *request, int ifindex) | |
1197 | { | |
1198 | size_t offset; | |
1199 | ||
1200 | nl_msg_put_string(request, TCA_ACT_KIND, "mirred"); | |
1201 | offset = nl_msg_start_nested(request, TCA_ACT_OPTIONS); | |
1202 | { | |
1203 | struct tc_mirred m = { .action = TC_ACT_STOLEN, | |
1204 | .eaction = TCA_EGRESS_REDIR, | |
1205 | .ifindex = ifindex }; | |
1206 | ||
1207 | nl_msg_put_unspec(request, TCA_MIRRED_PARMS, &m, sizeof m); | |
1208 | } | |
1209 | nl_msg_end_nested(request, offset); | |
1210 | } | |
1211 | ||
1212 | static inline void | |
1213 | nl_msg_put_act_cookie(struct ofpbuf *request, struct tc_cookie *ck) { | |
1214 | if (ck->len) { | |
1215 | nl_msg_put_unspec(request, TCA_ACT_COOKIE, ck->data, ck->len); | |
1216 | } | |
1217 | } | |
1218 | ||
8ada482b PB |
1219 | /* Given flower, a key_to_pedit map entry, calculates the rest, |
1220 | * where: | |
1221 | * | |
1222 | * mask, data - pointers of where read the first word of flower->key/mask. | |
1223 | * current_offset - which offset to use for the first pedit action. | |
1224 | * cnt - max pedits actions to use. | |
1225 | * first_word_mask/last_word_mask - the mask to use for the first/last read | |
1226 | * (as we read entire words). */ | |
f98e418f | 1227 | static void |
8ada482b PB |
1228 | calc_offsets(struct tc_flower *flower, struct flower_key_to_pedit *m, |
1229 | int *cur_offset, int *cnt, uint32_t *last_word_mask, | |
1230 | uint32_t *first_word_mask, uint32_t **mask, uint32_t **data) | |
1231 | { | |
1232 | int start_offset, max_offset, total_size; | |
1233 | int diff, right_zero_bits, left_zero_bits; | |
1234 | char *rewrite_key = (void *) &flower->rewrite.key; | |
1235 | char *rewrite_mask = (void *) &flower->rewrite.mask; | |
1236 | ||
1237 | max_offset = m->offset + m->size; | |
1238 | start_offset = ROUND_DOWN(m->offset, 4); | |
1239 | diff = m->offset - start_offset; | |
1240 | total_size = max_offset - start_offset; | |
1241 | right_zero_bits = 8 * (4 - (max_offset % 4)); | |
1242 | left_zero_bits = 8 * (m->offset - start_offset); | |
1243 | ||
1244 | *cur_offset = start_offset; | |
1245 | *cnt = (total_size / 4) + (total_size % 4 ? 1 : 0); | |
1246 | *last_word_mask = UINT32_MAX >> right_zero_bits; | |
1247 | *first_word_mask = UINT32_MAX << left_zero_bits; | |
1248 | *data = (void *) (rewrite_key + m->flower_offset - diff); | |
1249 | *mask = (void *) (rewrite_mask + m->flower_offset - diff); | |
1250 | } | |
1251 | ||
d6118e62 | 1252 | static inline int |
8ada482b PB |
1253 | csum_update_flag(struct tc_flower *flower, |
1254 | enum pedit_header_type htype) { | |
d6118e62 PB |
1255 | /* Explictily specifiy the csum flags so HW can return EOPNOTSUPP |
1256 | * if it doesn't support a checksum recalculation of some headers. | |
1257 | * And since OVS allows a flow such as | |
1258 | * eth(dst=<mac>),eth_type(0x0800) actions=set(ipv4(src=<new_ip>)) | |
1259 | * we need to force a more specific flow as this can, for example, | |
1260 | * need a recalculation of icmp checksum if the packet that passes | |
1261 | * is icmp and tcp checksum if its tcp. */ | |
1262 | ||
1263 | switch (htype) { | |
1264 | case TCA_PEDIT_KEY_EX_HDR_TYPE_IP4: | |
8ada482b | 1265 | flower->csum_update_flags |= TCA_CSUM_UPDATE_FLAG_IPV4HDR; |
eeb0ca88 | 1266 | /* Fall through. */ |
d6118e62 PB |
1267 | case TCA_PEDIT_KEY_EX_HDR_TYPE_IP6: |
1268 | case TCA_PEDIT_KEY_EX_HDR_TYPE_TCP: | |
1269 | case TCA_PEDIT_KEY_EX_HDR_TYPE_UDP: | |
8ada482b | 1270 | if (flower->key.ip_proto == IPPROTO_TCP) { |
d6118e62 | 1271 | flower->needs_full_ip_proto_mask = true; |
8ada482b PB |
1272 | flower->csum_update_flags |= TCA_CSUM_UPDATE_FLAG_TCP; |
1273 | } else if (flower->key.ip_proto == IPPROTO_UDP) { | |
d6118e62 | 1274 | flower->needs_full_ip_proto_mask = true; |
8ada482b PB |
1275 | flower->csum_update_flags |= TCA_CSUM_UPDATE_FLAG_UDP; |
1276 | } else if (flower->key.ip_proto == IPPROTO_ICMP | |
1277 | || flower->key.ip_proto == IPPROTO_ICMPV6) { | |
d6118e62 | 1278 | flower->needs_full_ip_proto_mask = true; |
8ada482b | 1279 | flower->csum_update_flags |= TCA_CSUM_UPDATE_FLAG_ICMP; |
d6118e62 PB |
1280 | } else { |
1281 | VLOG_WARN_RL(&error_rl, | |
1282 | "can't offload rewrite of IP/IPV6 with ip_proto: %d", | |
1283 | flower->key.ip_proto); | |
1284 | break; | |
8ada482b | 1285 | } |
eeb0ca88 | 1286 | /* Fall through. */ |
d6118e62 PB |
1287 | case TCA_PEDIT_KEY_EX_HDR_TYPE_ETH: |
1288 | return 0; /* success */ | |
1289 | ||
1290 | case TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK: | |
1291 | case __PEDIT_HDR_TYPE_MAX: | |
1292 | default: | |
1293 | break; | |
8ada482b | 1294 | } |
d6118e62 PB |
1295 | |
1296 | return EOPNOTSUPP; | |
8ada482b PB |
1297 | } |
1298 | ||
1299 | static int | |
1300 | nl_msg_put_flower_rewrite_pedits(struct ofpbuf *request, | |
1301 | struct tc_flower *flower) | |
1302 | { | |
1303 | struct { | |
1304 | struct tc_pedit sel; | |
1305 | struct tc_pedit_key keys[MAX_PEDIT_OFFSETS]; | |
1306 | struct tc_pedit_key_ex keys_ex[MAX_PEDIT_OFFSETS]; | |
1307 | } sel = { | |
1308 | .sel = { | |
1309 | .nkeys = 0 | |
1310 | } | |
1311 | }; | |
d6118e62 | 1312 | int i, j, err; |
8ada482b PB |
1313 | |
1314 | for (i = 0; i < ARRAY_SIZE(flower_pedit_map); i++) { | |
1315 | struct flower_key_to_pedit *m = &flower_pedit_map[i]; | |
1316 | struct tc_pedit_key *pedit_key = NULL; | |
1317 | struct tc_pedit_key_ex *pedit_key_ex = NULL; | |
1318 | uint32_t *mask, *data, first_word_mask, last_word_mask; | |
1319 | int cnt = 0, cur_offset = 0; | |
1320 | ||
1321 | if (!m->size) { | |
1322 | continue; | |
1323 | } | |
1324 | ||
1325 | calc_offsets(flower, m, &cur_offset, &cnt, &last_word_mask, | |
1326 | &first_word_mask, &mask, &data); | |
1327 | ||
1328 | for (j = 0; j < cnt; j++, mask++, data++, cur_offset += 4) { | |
1329 | uint32_t mask_word = *mask; | |
1330 | ||
1331 | if (j == 0) { | |
1332 | mask_word &= first_word_mask; | |
1333 | } | |
1334 | if (j == cnt - 1) { | |
1335 | mask_word &= last_word_mask; | |
1336 | } | |
1337 | if (!mask_word) { | |
1338 | continue; | |
1339 | } | |
1340 | if (sel.sel.nkeys == MAX_PEDIT_OFFSETS) { | |
1341 | VLOG_WARN_RL(&error_rl, "reached too many pedit offsets: %d", | |
1342 | MAX_PEDIT_OFFSETS); | |
1343 | return EOPNOTSUPP; | |
1344 | } | |
1345 | ||
1346 | pedit_key = &sel.keys[sel.sel.nkeys]; | |
1347 | pedit_key_ex = &sel.keys_ex[sel.sel.nkeys]; | |
1348 | pedit_key_ex->cmd = TCA_PEDIT_KEY_EX_CMD_SET; | |
1349 | pedit_key_ex->htype = m->htype; | |
1350 | pedit_key->off = cur_offset; | |
1351 | pedit_key->mask = ~mask_word; | |
1352 | pedit_key->val = *data & mask_word; | |
1353 | sel.sel.nkeys++; | |
d6118e62 PB |
1354 | |
1355 | err = csum_update_flag(flower, m->htype); | |
1356 | if (err) { | |
1357 | return err; | |
1358 | } | |
1359 | ||
1360 | if (flower->needs_full_ip_proto_mask) { | |
1361 | flower->mask.ip_proto = UINT8_MAX; | |
1362 | } | |
8ada482b PB |
1363 | } |
1364 | } | |
1365 | nl_msg_put_act_pedit(request, &sel.sel, sel.keys_ex); | |
1366 | ||
1367 | return 0; | |
1368 | } | |
1369 | ||
1370 | static int | |
f98e418f RD |
1371 | nl_msg_put_flower_acts(struct ofpbuf *request, struct tc_flower *flower) |
1372 | { | |
1373 | size_t offset; | |
1374 | size_t act_offset; | |
1375 | ||
1376 | offset = nl_msg_start_nested(request, TCA_FLOWER_ACT); | |
1377 | { | |
1378 | uint16_t act_index = 1; | |
8ada482b PB |
1379 | int error; |
1380 | ||
1381 | if (flower->rewrite.rewrite) { | |
1382 | act_offset = nl_msg_start_nested(request, act_index++); | |
1383 | error = nl_msg_put_flower_rewrite_pedits(request, flower); | |
1384 | if (error) { | |
1385 | return error; | |
1386 | } | |
1387 | nl_msg_end_nested(request, act_offset); | |
f98e418f | 1388 | |
a7ce5b85 PB |
1389 | if (flower->csum_update_flags) { |
1390 | act_offset = nl_msg_start_nested(request, act_index++); | |
1391 | nl_msg_put_act_csum(request, flower->csum_update_flags); | |
1392 | nl_msg_end_nested(request, act_offset); | |
1393 | } | |
8ada482b | 1394 | } |
23e670c4 JH |
1395 | if (flower->tunnel.tunnel) { |
1396 | act_offset = nl_msg_start_nested(request, act_index++); | |
1397 | nl_msg_put_act_tunnel_key_release(request); | |
1398 | nl_msg_end_nested(request, act_offset); | |
1399 | } | |
f98e418f RD |
1400 | if (flower->set.set) { |
1401 | act_offset = nl_msg_start_nested(request, act_index++); | |
1402 | nl_msg_put_act_tunnel_key_set(request, flower->set.id, | |
1403 | flower->set.ipv4.ipv4_src, | |
1404 | flower->set.ipv4.ipv4_dst, | |
1405 | &flower->set.ipv6.ipv6_src, | |
1406 | &flower->set.ipv6.ipv6_dst, | |
1407 | flower->set.tp_dst); | |
1408 | nl_msg_end_nested(request, act_offset); | |
1409 | } | |
f98e418f RD |
1410 | if (flower->vlan_pop) { |
1411 | act_offset = nl_msg_start_nested(request, act_index++); | |
1412 | nl_msg_put_act_pop_vlan(request); | |
1413 | nl_msg_end_nested(request, act_offset); | |
1414 | } | |
1415 | if (flower->vlan_push_id) { | |
1416 | act_offset = nl_msg_start_nested(request, act_index++); | |
1417 | nl_msg_put_act_push_vlan(request, | |
1418 | flower->vlan_push_id, | |
1419 | flower->vlan_push_prio); | |
1420 | nl_msg_end_nested(request, act_offset); | |
1421 | } | |
1422 | if (flower->ifindex_out) { | |
1423 | act_offset = nl_msg_start_nested(request, act_index++); | |
1424 | nl_msg_put_act_redirect(request, flower->ifindex_out); | |
1425 | nl_msg_put_act_cookie(request, &flower->act_cookie); | |
1426 | nl_msg_end_nested(request, act_offset); | |
1427 | } else { | |
1428 | act_offset = nl_msg_start_nested(request, act_index++); | |
1429 | nl_msg_put_act_drop(request); | |
1430 | nl_msg_put_act_cookie(request, &flower->act_cookie); | |
1431 | nl_msg_end_nested(request, act_offset); | |
1432 | } | |
1433 | } | |
1434 | nl_msg_end_nested(request, offset); | |
8ada482b PB |
1435 | |
1436 | return 0; | |
f98e418f RD |
1437 | } |
1438 | ||
1439 | static void | |
1440 | nl_msg_put_masked_value(struct ofpbuf *request, uint16_t type, | |
1441 | uint16_t mask_type, const void *data, | |
1442 | const void *mask_data, size_t len) | |
1443 | { | |
1444 | if (mask_type != TCA_FLOWER_UNSPEC) { | |
1445 | if (is_all_zeros(mask_data, len)) { | |
1446 | return; | |
1447 | } | |
1448 | nl_msg_put_unspec(request, mask_type, mask_data, len); | |
1449 | } | |
1450 | nl_msg_put_unspec(request, type, data, len); | |
1451 | } | |
1452 | ||
1453 | static void | |
1454 | nl_msg_put_flower_tunnel(struct ofpbuf *request, struct tc_flower *flower) | |
1455 | { | |
1456 | ovs_be32 ipv4_src = flower->tunnel.ipv4.ipv4_src; | |
1457 | ovs_be32 ipv4_dst = flower->tunnel.ipv4.ipv4_dst; | |
1458 | struct in6_addr *ipv6_src = &flower->tunnel.ipv6.ipv6_src; | |
1459 | struct in6_addr *ipv6_dst = &flower->tunnel.ipv6.ipv6_dst; | |
1460 | ovs_be16 tp_dst = flower->tunnel.tp_dst; | |
1461 | ovs_be32 id = be64_to_be32(flower->tunnel.id); | |
1462 | ||
1463 | nl_msg_put_be32(request, TCA_FLOWER_KEY_ENC_KEY_ID, id); | |
1464 | if (ipv4_dst) { | |
1465 | nl_msg_put_be32(request, TCA_FLOWER_KEY_ENC_IPV4_SRC, ipv4_src); | |
1466 | nl_msg_put_be32(request, TCA_FLOWER_KEY_ENC_IPV4_DST, ipv4_dst); | |
1467 | } else if (!is_all_zeros(ipv6_dst, sizeof *ipv6_dst)) { | |
1468 | nl_msg_put_in6_addr(request, TCA_FLOWER_KEY_ENC_IPV6_SRC, ipv6_src); | |
1469 | nl_msg_put_in6_addr(request, TCA_FLOWER_KEY_ENC_IPV6_DST, ipv6_dst); | |
1470 | } | |
1471 | nl_msg_put_be16(request, TCA_FLOWER_KEY_ENC_UDP_DST_PORT, tp_dst); | |
1472 | } | |
1473 | ||
bb170644 PB |
1474 | #define FLOWER_PUT_MASKED_VALUE(member, type) \ |
1475 | nl_msg_put_masked_value(request, type, type##_MASK, &flower->key.member, \ | |
1476 | &flower->mask.member, sizeof flower->key.member) | |
1477 | ||
8ada482b | 1478 | static int |
f98e418f RD |
1479 | nl_msg_put_flower_options(struct ofpbuf *request, struct tc_flower *flower) |
1480 | { | |
8ada482b | 1481 | |
f98e418f RD |
1482 | uint16_t host_eth_type = ntohs(flower->key.eth_type); |
1483 | bool is_vlan = (host_eth_type == ETH_TYPE_VLAN); | |
8ada482b PB |
1484 | int err; |
1485 | ||
d6118e62 PB |
1486 | /* need to parse acts first as some acts require changing the matching |
1487 | * see csum_update_flag() */ | |
8ada482b PB |
1488 | err = nl_msg_put_flower_acts(request, flower); |
1489 | if (err) { | |
1490 | return err; | |
1491 | } | |
f98e418f RD |
1492 | |
1493 | if (is_vlan) { | |
1494 | host_eth_type = ntohs(flower->key.encap_eth_type); | |
1495 | } | |
1496 | ||
bb170644 PB |
1497 | FLOWER_PUT_MASKED_VALUE(dst_mac, TCA_FLOWER_KEY_ETH_DST); |
1498 | FLOWER_PUT_MASKED_VALUE(src_mac, TCA_FLOWER_KEY_ETH_SRC); | |
f98e418f RD |
1499 | |
1500 | if (host_eth_type == ETH_P_IP || host_eth_type == ETH_P_IPV6) { | |
1501 | if (flower->mask.ip_proto && flower->key.ip_proto) { | |
1502 | nl_msg_put_u8(request, TCA_FLOWER_KEY_IP_PROTO, | |
1503 | flower->key.ip_proto); | |
1504 | } | |
1505 | ||
83e86606 | 1506 | if (flower->mask.flags) { |
7e0f69b5 | 1507 | nl_msg_put_be32(request, TCA_FLOWER_KEY_FLAGS, |
83e86606 | 1508 | htonl(flower->key.flags)); |
7e0f69b5 | 1509 | nl_msg_put_be32(request, TCA_FLOWER_KEY_FLAGS_MASK, |
83e86606 RD |
1510 | htonl(flower->mask.flags)); |
1511 | } | |
1512 | ||
f98e418f | 1513 | if (flower->key.ip_proto == IPPROTO_UDP) { |
2b1d9fa9 PB |
1514 | FLOWER_PUT_MASKED_VALUE(udp_src, TCA_FLOWER_KEY_UDP_SRC); |
1515 | FLOWER_PUT_MASKED_VALUE(udp_dst, TCA_FLOWER_KEY_UDP_DST); | |
f98e418f | 1516 | } else if (flower->key.ip_proto == IPPROTO_TCP) { |
2b1d9fa9 PB |
1517 | FLOWER_PUT_MASKED_VALUE(tcp_src, TCA_FLOWER_KEY_TCP_SRC); |
1518 | FLOWER_PUT_MASKED_VALUE(tcp_dst, TCA_FLOWER_KEY_TCP_DST); | |
cd081043 | 1519 | FLOWER_PUT_MASKED_VALUE(tcp_flags, TCA_FLOWER_KEY_TCP_FLAGS); |
4862b4e5 | 1520 | } else if (flower->key.ip_proto == IPPROTO_SCTP) { |
2b1d9fa9 PB |
1521 | FLOWER_PUT_MASKED_VALUE(sctp_src, TCA_FLOWER_KEY_SCTP_SRC); |
1522 | FLOWER_PUT_MASKED_VALUE(sctp_dst, TCA_FLOWER_KEY_SCTP_DST); | |
f98e418f RD |
1523 | } |
1524 | } | |
1525 | ||
1526 | if (host_eth_type == ETH_P_IP) { | |
bb170644 PB |
1527 | FLOWER_PUT_MASKED_VALUE(ipv4.ipv4_src, TCA_FLOWER_KEY_IPV4_SRC); |
1528 | FLOWER_PUT_MASKED_VALUE(ipv4.ipv4_dst, TCA_FLOWER_KEY_IPV4_DST); | |
0b4b5203 | 1529 | FLOWER_PUT_MASKED_VALUE(ip_ttl, TCA_FLOWER_KEY_IP_TTL); |
f98e418f | 1530 | } else if (host_eth_type == ETH_P_IPV6) { |
bb170644 PB |
1531 | FLOWER_PUT_MASKED_VALUE(ipv6.ipv6_src, TCA_FLOWER_KEY_IPV6_SRC); |
1532 | FLOWER_PUT_MASKED_VALUE(ipv6.ipv6_dst, TCA_FLOWER_KEY_IPV6_DST); | |
f98e418f RD |
1533 | } |
1534 | ||
1535 | nl_msg_put_be16(request, TCA_FLOWER_KEY_ETH_TYPE, flower->key.eth_type); | |
1536 | ||
1537 | if (is_vlan) { | |
1538 | if (flower->key.vlan_id || flower->key.vlan_prio) { | |
1539 | nl_msg_put_u16(request, TCA_FLOWER_KEY_VLAN_ID, | |
1540 | flower->key.vlan_id); | |
1541 | nl_msg_put_u8(request, TCA_FLOWER_KEY_VLAN_PRIO, | |
1542 | flower->key.vlan_prio); | |
1543 | } | |
1544 | if (flower->key.encap_eth_type) { | |
1545 | nl_msg_put_be16(request, TCA_FLOWER_KEY_VLAN_ETH_TYPE, | |
1546 | flower->key.encap_eth_type); | |
1547 | } | |
1548 | } | |
1549 | ||
691d20cb | 1550 | nl_msg_put_u32(request, TCA_FLOWER_FLAGS, tc_get_tc_cls_policy(tc_policy)); |
f98e418f RD |
1551 | |
1552 | if (flower->tunnel.tunnel) { | |
1553 | nl_msg_put_flower_tunnel(request, flower); | |
1554 | } | |
1555 | ||
8ada482b | 1556 | return 0; |
f98e418f RD |
1557 | } |
1558 | ||
1559 | int | |
1560 | tc_replace_flower(int ifindex, uint16_t prio, uint32_t handle, | |
1561 | struct tc_flower *flower) | |
1562 | { | |
1563 | struct ofpbuf request; | |
1564 | struct tcmsg *tcmsg; | |
1565 | struct ofpbuf *reply; | |
1566 | int error = 0; | |
1567 | size_t basic_offset; | |
1568 | uint16_t eth_type = (OVS_FORCE uint16_t) flower->key.eth_type; | |
1569 | ||
1570 | tcmsg = tc_make_request(ifindex, RTM_NEWTFILTER, | |
1571 | NLM_F_CREATE | NLM_F_ECHO, &request); | |
1572 | tcmsg->tcm_parent = TC_INGRESS_PARENT; | |
1573 | tcmsg->tcm_info = tc_make_handle(prio, eth_type); | |
1574 | tcmsg->tcm_handle = handle; | |
1575 | ||
1576 | nl_msg_put_string(&request, TCA_KIND, "flower"); | |
1577 | basic_offset = nl_msg_start_nested(&request, TCA_OPTIONS); | |
1578 | { | |
8ada482b PB |
1579 | error = nl_msg_put_flower_options(&request, flower); |
1580 | ||
1581 | if (error) { | |
1582 | ofpbuf_uninit(&request); | |
1583 | return error; | |
1584 | } | |
f98e418f RD |
1585 | } |
1586 | nl_msg_end_nested(&request, basic_offset); | |
1587 | ||
1588 | error = tc_transact(&request, &reply); | |
1589 | if (!error) { | |
1590 | struct tcmsg *tc = | |
1591 | ofpbuf_at_assert(reply, NLMSG_HDRLEN, sizeof *tc); | |
1592 | ||
1593 | flower->prio = tc_get_major(tc->tcm_info); | |
1594 | flower->handle = tc->tcm_handle; | |
1595 | ofpbuf_delete(reply); | |
1596 | } | |
1597 | ||
1598 | return error; | |
1599 | } | |
691d20cb PB |
1600 | |
1601 | void | |
1602 | tc_set_policy(const char *policy) | |
1603 | { | |
1604 | if (!policy) { | |
1605 | return; | |
1606 | } | |
1607 | ||
1608 | if (!strcmp(policy, "skip_sw")) { | |
1609 | tc_policy = TC_POLICY_SKIP_SW; | |
1610 | } else if (!strcmp(policy, "skip_hw")) { | |
1611 | tc_policy = TC_POLICY_SKIP_HW; | |
1612 | } else if (!strcmp(policy, "none")) { | |
1613 | tc_policy = TC_POLICY_NONE; | |
1614 | } else { | |
1615 | VLOG_WARN("tc: Invalid policy '%s'", policy); | |
1616 | return; | |
1617 | } | |
1618 | ||
1619 | VLOG_INFO("tc: Using policy '%s'", policy); | |
1620 | } |