]>
Commit | Line | Data |
---|---|---|
6b241d64 PS |
1 | /* |
2 | * Copyright (c) 2016 Nicira, Inc. | |
68da36fe | 3 | * Copyright (c) 2016 Red Hat, Inc. |
6b241d64 PS |
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 | ||
aca40d4f TLSC |
20 | #include "netdev-native-tnl.h" |
21 | ||
6b241d64 PS |
22 | #include <errno.h> |
23 | #include <fcntl.h> | |
24 | #include <sys/socket.h> | |
25 | #include <net/if.h> | |
67eaddc0 | 26 | #include <netinet/in.h> |
aca40d4f | 27 | #include <netinet/ip.h> |
6b241d64 PS |
28 | #include <netinet/ip6.h> |
29 | #include <sys/ioctl.h> | |
30 | ||
31 | #include <errno.h> | |
32 | #include <stdlib.h> | |
33 | #include <sys/time.h> | |
34 | ||
6b241d64 PS |
35 | #include "byte-order.h" |
36 | #include "csum.h" | |
6b241d64 | 37 | #include "dp-packet.h" |
aca40d4f | 38 | #include "netdev.h" |
6b241d64 PS |
39 | #include "netdev-vport.h" |
40 | #include "netdev-vport-private.h" | |
41 | #include "odp-netlink.h" | |
6b241d64 | 42 | #include "packets.h" |
aca40d4f | 43 | #include "seq.h" |
6b241d64 PS |
44 | #include "unaligned.h" |
45 | #include "unixctl.h" | |
aca40d4f | 46 | #include "openvswitch/vlog.h" |
6b241d64 PS |
47 | |
48 | VLOG_DEFINE_THIS_MODULE(native_tnl); | |
49 | static struct vlog_rate_limit err_rl = VLOG_RATE_LIMIT_INIT(60, 5); | |
50 | ||
51 | #define VXLAN_HLEN (sizeof(struct udp_header) + \ | |
52 | sizeof(struct vxlanhdr)) | |
53 | ||
54 | #define GENEVE_BASE_HLEN (sizeof(struct udp_header) + \ | |
55 | sizeof(struct genevehdr)) | |
56 | ||
57 | uint16_t tnl_udp_port_min = 32768; | |
58 | uint16_t tnl_udp_port_max = 61000; | |
59 | ||
60 | void * | |
61 | netdev_tnl_ip_extract_tnl_md(struct dp_packet *packet, struct flow_tnl *tnl, | |
62 | unsigned int *hlen) | |
63 | { | |
64 | void *nh; | |
65 | struct ip_header *ip; | |
66 | struct ovs_16aligned_ip6_hdr *ip6; | |
67 | void *l4; | |
68 | int l3_size; | |
69 | ||
70 | nh = dp_packet_l3(packet); | |
71 | ip = nh; | |
72 | ip6 = nh; | |
73 | l4 = dp_packet_l4(packet); | |
74 | ||
75 | if (!nh || !l4) { | |
76 | return NULL; | |
77 | } | |
78 | ||
79 | *hlen = sizeof(struct eth_header); | |
80 | ||
81 | l3_size = dp_packet_size(packet) - | |
82 | ((char *)nh - (char *)dp_packet_data(packet)); | |
83 | ||
84 | if (IP_VER(ip->ip_ihl_ver) == 4) { | |
85 | ||
86 | ovs_be32 ip_src, ip_dst; | |
87 | ||
1a2bb118 SC |
88 | if (OVS_UNLIKELY(!dp_packet_ip_checksum_valid(packet))) { |
89 | if (csum(ip, IP_IHL(ip->ip_ihl_ver) * 4)) { | |
90 | VLOG_WARN_RL(&err_rl, "ip packet has invalid checksum"); | |
91 | return NULL; | |
92 | } | |
6b241d64 PS |
93 | } |
94 | ||
95 | if (ntohs(ip->ip_tot_len) > l3_size) { | |
96 | VLOG_WARN_RL(&err_rl, "ip packet is truncated (IP length %d, actual %d)", | |
97 | ntohs(ip->ip_tot_len), l3_size); | |
98 | return NULL; | |
99 | } | |
100 | if (IP_IHL(ip->ip_ihl_ver) * 4 > sizeof(struct ip_header)) { | |
101 | VLOG_WARN_RL(&err_rl, "ip options not supported on tunnel packets " | |
102 | "(%d bytes)", IP_IHL(ip->ip_ihl_ver) * 4); | |
103 | return NULL; | |
104 | } | |
105 | ||
106 | ip_src = get_16aligned_be32(&ip->ip_src); | |
107 | ip_dst = get_16aligned_be32(&ip->ip_dst); | |
108 | ||
109 | tnl->ip_src = ip_src; | |
110 | tnl->ip_dst = ip_dst; | |
111 | tnl->ip_tos = ip->ip_tos; | |
112 | tnl->ip_ttl = ip->ip_ttl; | |
113 | ||
114 | *hlen += IP_HEADER_LEN; | |
115 | ||
116 | } else if (IP_VER(ip->ip_ihl_ver) == 6) { | |
98c086db | 117 | ovs_be32 tc_flow = get_16aligned_be32(&ip6->ip6_flow); |
6b241d64 PS |
118 | |
119 | memcpy(tnl->ipv6_src.s6_addr, ip6->ip6_src.be16, sizeof ip6->ip6_src); | |
120 | memcpy(tnl->ipv6_dst.s6_addr, ip6->ip6_dst.be16, sizeof ip6->ip6_dst); | |
98c086db PS |
121 | |
122 | tnl->ip_tos = ntohl(tc_flow) >> 20; | |
6b241d64 PS |
123 | tnl->ip_ttl = ip6->ip6_hlim; |
124 | ||
125 | *hlen += IPV6_HEADER_LEN; | |
126 | ||
127 | } else { | |
128 | VLOG_WARN_RL(&err_rl, "ipv4 packet has invalid version (%d)", | |
129 | IP_VER(ip->ip_ihl_ver)); | |
130 | return NULL; | |
131 | } | |
132 | ||
133 | return l4; | |
134 | } | |
135 | ||
136 | /* Pushes the 'size' bytes of 'header' into the headroom of 'packet', | |
137 | * reallocating the packet if necessary. 'header' should contain an Ethernet | |
138 | * header, followed by an IPv4 header (without options), and an L4 header. | |
139 | * | |
140 | * This function sets the IP header's ip_tot_len field (which should be zeroed | |
141 | * as part of 'header') and puts its value into '*ip_tot_size' as well. Also | |
142 | * updates IP header checksum. | |
143 | * | |
144 | * Return pointer to the L4 header added to 'packet'. */ | |
145 | void * | |
146 | netdev_tnl_push_ip_header(struct dp_packet *packet, | |
147 | const void *header, int size, int *ip_tot_size) | |
148 | { | |
149 | struct eth_header *eth; | |
150 | struct ip_header *ip; | |
151 | struct ovs_16aligned_ip6_hdr *ip6; | |
152 | ||
153 | eth = dp_packet_push_uninit(packet, size); | |
154 | *ip_tot_size = dp_packet_size(packet) - sizeof (struct eth_header); | |
155 | ||
156 | memcpy(eth, header, size); | |
157 | ||
158 | if (netdev_tnl_is_header_ipv6(header)) { | |
159 | ip6 = netdev_tnl_ipv6_hdr(eth); | |
160 | *ip_tot_size -= IPV6_HEADER_LEN; | |
161 | ip6->ip6_plen = htons(*ip_tot_size); | |
162 | return ip6 + 1; | |
163 | } else { | |
164 | ip = netdev_tnl_ip_hdr(eth); | |
165 | ip->ip_tot_len = htons(*ip_tot_size); | |
166 | ip->ip_csum = recalc_csum16(ip->ip_csum, 0, ip->ip_tot_len); | |
167 | *ip_tot_size -= IP_HEADER_LEN; | |
168 | return ip + 1; | |
169 | } | |
170 | } | |
171 | ||
172 | static void * | |
173 | udp_extract_tnl_md(struct dp_packet *packet, struct flow_tnl *tnl, | |
174 | unsigned int *hlen) | |
175 | { | |
176 | struct udp_header *udp; | |
177 | ||
178 | udp = netdev_tnl_ip_extract_tnl_md(packet, tnl, hlen); | |
179 | if (!udp) { | |
180 | return NULL; | |
181 | } | |
182 | ||
183 | if (udp->udp_csum) { | |
1a2bb118 SC |
184 | if (OVS_UNLIKELY(!dp_packet_l4_checksum_valid(packet))) { |
185 | uint32_t csum; | |
186 | if (netdev_tnl_is_header_ipv6(dp_packet_data(packet))) { | |
187 | csum = packet_csum_pseudoheader6(dp_packet_l3(packet)); | |
188 | } else { | |
189 | csum = packet_csum_pseudoheader(dp_packet_l3(packet)); | |
190 | } | |
191 | ||
192 | csum = csum_continue(csum, udp, dp_packet_size(packet) - | |
193 | ((const unsigned char *)udp - | |
194 | (const unsigned char *)dp_packet_l2(packet) | |
195 | )); | |
196 | if (csum_finish(csum)) { | |
197 | return NULL; | |
198 | } | |
6b241d64 PS |
199 | } |
200 | tnl->flags |= FLOW_TNL_F_CSUM; | |
201 | } | |
202 | ||
203 | tnl->tp_src = udp->udp_src; | |
204 | tnl->tp_dst = udp->udp_dst; | |
205 | ||
206 | return udp + 1; | |
207 | } | |
208 | ||
209 | ||
210 | void | |
211 | netdev_tnl_push_udp_header(struct dp_packet *packet, | |
212 | const struct ovs_action_push_tnl *data) | |
213 | { | |
214 | struct udp_header *udp; | |
215 | int ip_tot_size; | |
216 | ||
217 | udp = netdev_tnl_push_ip_header(packet, data->header, data->header_len, &ip_tot_size); | |
218 | ||
219 | /* set udp src port */ | |
220 | udp->udp_src = netdev_tnl_get_src_port(packet); | |
221 | udp->udp_len = htons(ip_tot_size); | |
222 | ||
223 | if (udp->udp_csum) { | |
224 | uint32_t csum; | |
225 | if (netdev_tnl_is_header_ipv6(dp_packet_data(packet))) { | |
226 | csum = packet_csum_pseudoheader6(netdev_tnl_ipv6_hdr(dp_packet_data(packet))); | |
227 | } else { | |
228 | csum = packet_csum_pseudoheader(netdev_tnl_ip_hdr(dp_packet_data(packet))); | |
229 | } | |
230 | ||
231 | csum = csum_continue(csum, udp, ip_tot_size); | |
232 | udp->udp_csum = csum_finish(csum); | |
233 | ||
234 | if (!udp->udp_csum) { | |
235 | udp->udp_csum = htons(0xffff); | |
236 | } | |
237 | } | |
238 | } | |
239 | ||
240 | static void * | |
4975aa3e PS |
241 | eth_build_header(struct ovs_action_push_tnl *data, |
242 | const struct netdev_tnl_build_header_params *params) | |
6b241d64 | 243 | { |
4975aa3e PS |
244 | uint16_t eth_proto = params->is_ipv6 ? ETH_TYPE_IPV6 : ETH_TYPE_IP; |
245 | struct eth_header *eth; | |
6b241d64 | 246 | |
4975aa3e | 247 | memset(data->header, 0, sizeof data->header); |
6b241d64 | 248 | |
4975aa3e PS |
249 | eth = (struct eth_header *)data->header; |
250 | eth->eth_dst = params->dmac; | |
251 | eth->eth_src = params->smac; | |
252 | eth->eth_type = htons(eth_proto); | |
253 | data->header_len = sizeof(struct eth_header); | |
254 | return eth + 1; | |
255 | } | |
6b241d64 | 256 | |
4975aa3e PS |
257 | void * |
258 | netdev_tnl_ip_build_header(struct ovs_action_push_tnl *data, | |
259 | const struct netdev_tnl_build_header_params *params, | |
260 | uint8_t next_proto) | |
261 | { | |
262 | void *l3; | |
263 | ||
264 | l3 = eth_build_header(data, params); | |
265 | if (!params->is_ipv6) { | |
266 | ovs_be32 ip_src = in6_addr_get_mapped_ipv4(params->s_ip); | |
267 | struct ip_header *ip; | |
268 | ||
269 | ip = (struct ip_header *) l3; | |
270 | ||
271 | ip->ip_ihl_ver = IP_IHL_VER(5, 4); | |
272 | ip->ip_tos = params->flow->tunnel.ip_tos; | |
273 | ip->ip_ttl = params->flow->tunnel.ip_ttl; | |
274 | ip->ip_proto = next_proto; | |
275 | put_16aligned_be32(&ip->ip_src, ip_src); | |
276 | put_16aligned_be32(&ip->ip_dst, params->flow->tunnel.ip_dst); | |
277 | ||
278 | ip->ip_frag_off = (params->flow->tunnel.flags & FLOW_TNL_F_DONT_FRAGMENT) ? | |
279 | htons(IP_DF) : 0; | |
280 | ||
ece9c294 | 281 | /* Checksum has already been zeroed by eth_build_header. */ |
4975aa3e PS |
282 | ip->ip_csum = csum(ip, sizeof *ip); |
283 | ||
284 | data->header_len += IP_HEADER_LEN; | |
285 | return ip + 1; | |
6b241d64 | 286 | } else { |
4975aa3e PS |
287 | struct ovs_16aligned_ip6_hdr *ip6; |
288 | ||
289 | ip6 = (struct ovs_16aligned_ip6_hdr *) l3; | |
290 | ||
98c086db PS |
291 | put_16aligned_be32(&ip6->ip6_flow, htonl(6 << 28) | |
292 | htonl(params->flow->tunnel.ip_tos << 20)); | |
4975aa3e PS |
293 | ip6->ip6_hlim = params->flow->tunnel.ip_ttl; |
294 | ip6->ip6_nxt = next_proto; | |
295 | memcpy(&ip6->ip6_src, params->s_ip, sizeof(ovs_be32[4])); | |
296 | memcpy(&ip6->ip6_dst, ¶ms->flow->tunnel.ipv6_dst, sizeof(ovs_be32[4])); | |
297 | ||
298 | data->header_len += IPV6_HEADER_LEN; | |
299 | return ip6 + 1; | |
6b241d64 | 300 | } |
4975aa3e PS |
301 | } |
302 | ||
303 | static void * | |
304 | udp_build_header(struct netdev_tunnel_config *tnl_cfg, | |
305 | struct ovs_action_push_tnl *data, | |
306 | const struct netdev_tnl_build_header_params *params) | |
307 | { | |
308 | struct udp_header *udp; | |
6b241d64 | 309 | |
4975aa3e | 310 | udp = netdev_tnl_ip_build_header(data, params, IPPROTO_UDP); |
6b241d64 PS |
311 | udp->udp_dst = tnl_cfg->dst_port; |
312 | ||
4975aa3e | 313 | if (params->is_ipv6 || params->flow->tunnel.flags & FLOW_TNL_F_CSUM) { |
6b241d64 PS |
314 | /* Write a value in now to mark that we should compute the checksum |
315 | * later. 0xffff is handy because it is transparent to the | |
316 | * calculation. */ | |
317 | udp->udp_csum = htons(0xffff); | |
318 | } | |
4975aa3e | 319 | data->header_len += sizeof *udp; |
6b241d64 PS |
320 | return udp + 1; |
321 | } | |
322 | ||
323 | static int | |
324 | gre_header_len(ovs_be16 flags) | |
325 | { | |
326 | int hlen = 4; | |
327 | ||
328 | if (flags & htons(GRE_CSUM)) { | |
329 | hlen += 4; | |
330 | } | |
331 | if (flags & htons(GRE_KEY)) { | |
332 | hlen += 4; | |
333 | } | |
334 | if (flags & htons(GRE_SEQ)) { | |
335 | hlen += 4; | |
336 | } | |
337 | return hlen; | |
338 | } | |
339 | ||
340 | static int | |
341 | parse_gre_header(struct dp_packet *packet, | |
342 | struct flow_tnl *tnl) | |
343 | { | |
344 | const struct gre_base_hdr *greh; | |
345 | ovs_16aligned_be32 *options; | |
346 | int hlen; | |
347 | unsigned int ulen; | |
348 | ||
349 | greh = netdev_tnl_ip_extract_tnl_md(packet, tnl, &ulen); | |
350 | if (!greh) { | |
351 | return -EINVAL; | |
352 | } | |
353 | ||
354 | if (greh->flags & ~(htons(GRE_CSUM | GRE_KEY | GRE_SEQ))) { | |
355 | return -EINVAL; | |
356 | } | |
357 | ||
358 | if (greh->protocol != htons(ETH_TYPE_TEB)) { | |
359 | return -EINVAL; | |
360 | } | |
361 | ||
362 | hlen = ulen + gre_header_len(greh->flags); | |
363 | if (hlen > dp_packet_size(packet)) { | |
364 | return -EINVAL; | |
365 | } | |
366 | ||
367 | options = (ovs_16aligned_be32 *)(greh + 1); | |
368 | if (greh->flags & htons(GRE_CSUM)) { | |
369 | ovs_be16 pkt_csum; | |
370 | ||
371 | pkt_csum = csum(greh, dp_packet_size(packet) - | |
372 | ((const unsigned char *)greh - | |
373 | (const unsigned char *)dp_packet_l2(packet))); | |
374 | if (pkt_csum) { | |
375 | return -EINVAL; | |
376 | } | |
377 | tnl->flags = FLOW_TNL_F_CSUM; | |
378 | options++; | |
379 | } | |
380 | ||
381 | if (greh->flags & htons(GRE_KEY)) { | |
3d75c660 | 382 | tnl->tun_id = be32_to_be64(get_16aligned_be32(options)); |
6b241d64 PS |
383 | tnl->flags |= FLOW_TNL_F_KEY; |
384 | options++; | |
385 | } | |
386 | ||
387 | if (greh->flags & htons(GRE_SEQ)) { | |
388 | options++; | |
389 | } | |
390 | ||
391 | return hlen; | |
392 | } | |
393 | ||
1c8f98d9 | 394 | struct dp_packet * |
6b241d64 PS |
395 | netdev_gre_pop_header(struct dp_packet *packet) |
396 | { | |
397 | struct pkt_metadata *md = &packet->md; | |
398 | struct flow_tnl *tnl = &md->tunnel; | |
399 | int hlen = sizeof(struct eth_header) + 4; | |
400 | ||
401 | hlen += netdev_tnl_is_header_ipv6(dp_packet_data(packet)) ? | |
402 | IPV6_HEADER_LEN : IP_HEADER_LEN; | |
403 | ||
404 | pkt_metadata_init_tnl(md); | |
405 | if (hlen > dp_packet_size(packet)) { | |
1c8f98d9 | 406 | goto err; |
6b241d64 PS |
407 | } |
408 | ||
409 | hlen = parse_gre_header(packet, tnl); | |
410 | if (hlen < 0) { | |
1c8f98d9 | 411 | goto err; |
6b241d64 PS |
412 | } |
413 | ||
414 | dp_packet_reset_packet(packet, hlen); | |
415 | ||
1c8f98d9 PS |
416 | return packet; |
417 | err: | |
418 | dp_packet_delete(packet); | |
419 | return NULL; | |
6b241d64 PS |
420 | } |
421 | ||
422 | void | |
423 | netdev_gre_push_header(struct dp_packet *packet, | |
424 | const struct ovs_action_push_tnl *data) | |
425 | { | |
426 | struct gre_base_hdr *greh; | |
427 | int ip_tot_size; | |
428 | ||
429 | greh = netdev_tnl_push_ip_header(packet, data->header, data->header_len, &ip_tot_size); | |
430 | ||
431 | if (greh->flags & htons(GRE_CSUM)) { | |
432 | ovs_be16 *csum_opt = (ovs_be16 *) (greh + 1); | |
433 | *csum_opt = csum(greh, ip_tot_size); | |
434 | } | |
435 | } | |
436 | ||
437 | int | |
438 | netdev_gre_build_header(const struct netdev *netdev, | |
439 | struct ovs_action_push_tnl *data, | |
4975aa3e | 440 | const struct netdev_tnl_build_header_params *params) |
6b241d64 PS |
441 | { |
442 | struct netdev_vport *dev = netdev_vport_cast(netdev); | |
443 | struct netdev_tunnel_config *tnl_cfg; | |
6b241d64 PS |
444 | struct gre_base_hdr *greh; |
445 | ovs_16aligned_be32 *options; | |
4975aa3e | 446 | unsigned int hlen; |
6b241d64 PS |
447 | |
448 | /* XXX: RCUfy tnl_cfg. */ | |
449 | ovs_mutex_lock(&dev->mutex); | |
450 | tnl_cfg = &dev->tnl_cfg; | |
451 | ||
4975aa3e | 452 | greh = netdev_tnl_ip_build_header(data, params, IPPROTO_GRE); |
6b241d64 PS |
453 | |
454 | greh->protocol = htons(ETH_TYPE_TEB); | |
455 | greh->flags = 0; | |
456 | ||
457 | options = (ovs_16aligned_be32 *) (greh + 1); | |
4975aa3e | 458 | if (params->flow->tunnel.flags & FLOW_TNL_F_CSUM) { |
6b241d64 PS |
459 | greh->flags |= htons(GRE_CSUM); |
460 | put_16aligned_be32(options, 0); | |
461 | options++; | |
462 | } | |
463 | ||
464 | if (tnl_cfg->out_key_present) { | |
465 | greh->flags |= htons(GRE_KEY); | |
3d75c660 | 466 | put_16aligned_be32(options, be64_to_be32(params->flow->tunnel.tun_id)); |
6b241d64 PS |
467 | options++; |
468 | } | |
469 | ||
470 | ovs_mutex_unlock(&dev->mutex); | |
471 | ||
472 | hlen = (uint8_t *) options - (uint8_t *) greh; | |
473 | ||
4975aa3e | 474 | data->header_len += hlen; |
6b241d64 PS |
475 | data->tnl_type = OVS_VPORT_TYPE_GRE; |
476 | return 0; | |
477 | } | |
478 | ||
1c8f98d9 | 479 | struct dp_packet * |
6b241d64 PS |
480 | netdev_vxlan_pop_header(struct dp_packet *packet) |
481 | { | |
482 | struct pkt_metadata *md = &packet->md; | |
483 | struct flow_tnl *tnl = &md->tunnel; | |
484 | struct vxlanhdr *vxh; | |
485 | unsigned int hlen; | |
486 | ||
487 | pkt_metadata_init_tnl(md); | |
488 | if (VXLAN_HLEN > dp_packet_l4_size(packet)) { | |
1c8f98d9 | 489 | goto err; |
6b241d64 PS |
490 | } |
491 | ||
492 | vxh = udp_extract_tnl_md(packet, tnl, &hlen); | |
493 | if (!vxh) { | |
1c8f98d9 | 494 | goto err; |
6b241d64 PS |
495 | } |
496 | ||
497 | if (get_16aligned_be32(&vxh->vx_flags) != htonl(VXLAN_FLAGS) || | |
498 | (get_16aligned_be32(&vxh->vx_vni) & htonl(0xff))) { | |
499 | VLOG_WARN_RL(&err_rl, "invalid vxlan flags=%#x vni=%#x\n", | |
500 | ntohl(get_16aligned_be32(&vxh->vx_flags)), | |
501 | ntohl(get_16aligned_be32(&vxh->vx_vni))); | |
1c8f98d9 | 502 | goto err; |
6b241d64 PS |
503 | } |
504 | tnl->tun_id = htonll(ntohl(get_16aligned_be32(&vxh->vx_vni)) >> 8); | |
505 | tnl->flags |= FLOW_TNL_F_KEY; | |
506 | ||
507 | dp_packet_reset_packet(packet, hlen + VXLAN_HLEN); | |
508 | ||
1c8f98d9 PS |
509 | return packet; |
510 | err: | |
511 | dp_packet_delete(packet); | |
512 | return NULL; | |
6b241d64 PS |
513 | } |
514 | ||
515 | int | |
516 | netdev_vxlan_build_header(const struct netdev *netdev, | |
517 | struct ovs_action_push_tnl *data, | |
4975aa3e | 518 | const struct netdev_tnl_build_header_params *params) |
6b241d64 PS |
519 | { |
520 | struct netdev_vport *dev = netdev_vport_cast(netdev); | |
521 | struct netdev_tunnel_config *tnl_cfg; | |
522 | struct vxlanhdr *vxh; | |
6b241d64 PS |
523 | |
524 | /* XXX: RCUfy tnl_cfg. */ | |
525 | ovs_mutex_lock(&dev->mutex); | |
526 | tnl_cfg = &dev->tnl_cfg; | |
527 | ||
4975aa3e | 528 | vxh = udp_build_header(tnl_cfg, data, params); |
6b241d64 PS |
529 | |
530 | put_16aligned_be32(&vxh->vx_flags, htonl(VXLAN_FLAGS)); | |
4975aa3e | 531 | put_16aligned_be32(&vxh->vx_vni, htonl(ntohll(params->flow->tunnel.tun_id) << 8)); |
6b241d64 PS |
532 | |
533 | ovs_mutex_unlock(&dev->mutex); | |
4975aa3e | 534 | data->header_len += sizeof *vxh; |
6b241d64 PS |
535 | data->tnl_type = OVS_VPORT_TYPE_VXLAN; |
536 | return 0; | |
537 | } | |
538 | ||
1c8f98d9 | 539 | struct dp_packet * |
6b241d64 PS |
540 | netdev_geneve_pop_header(struct dp_packet *packet) |
541 | { | |
542 | struct pkt_metadata *md = &packet->md; | |
543 | struct flow_tnl *tnl = &md->tunnel; | |
544 | struct genevehdr *gnh; | |
545 | unsigned int hlen, opts_len, ulen; | |
546 | ||
547 | pkt_metadata_init_tnl(md); | |
548 | if (GENEVE_BASE_HLEN > dp_packet_l4_size(packet)) { | |
549 | VLOG_WARN_RL(&err_rl, "geneve packet too small: min header=%u packet size=%"PRIuSIZE"\n", | |
550 | (unsigned int)GENEVE_BASE_HLEN, dp_packet_l4_size(packet)); | |
1c8f98d9 | 551 | goto err; |
6b241d64 PS |
552 | } |
553 | ||
554 | gnh = udp_extract_tnl_md(packet, tnl, &ulen); | |
555 | if (!gnh) { | |
1c8f98d9 | 556 | goto err; |
6b241d64 PS |
557 | } |
558 | ||
559 | opts_len = gnh->opt_len * 4; | |
560 | hlen = ulen + GENEVE_BASE_HLEN + opts_len; | |
561 | if (hlen > dp_packet_size(packet)) { | |
562 | VLOG_WARN_RL(&err_rl, "geneve packet too small: header len=%u packet size=%u\n", | |
563 | hlen, dp_packet_size(packet)); | |
1c8f98d9 | 564 | goto err; |
6b241d64 PS |
565 | } |
566 | ||
567 | if (gnh->ver != 0) { | |
568 | VLOG_WARN_RL(&err_rl, "unknown geneve version: %"PRIu8"\n", gnh->ver); | |
1c8f98d9 | 569 | goto err; |
6b241d64 PS |
570 | } |
571 | ||
572 | if (gnh->proto_type != htons(ETH_TYPE_TEB)) { | |
573 | VLOG_WARN_RL(&err_rl, "unknown geneve encapsulated protocol: %#x\n", | |
574 | ntohs(gnh->proto_type)); | |
1c8f98d9 | 575 | goto err; |
6b241d64 PS |
576 | } |
577 | ||
578 | tnl->flags |= gnh->oam ? FLOW_TNL_F_OAM : 0; | |
579 | tnl->tun_id = htonll(ntohl(get_16aligned_be32(&gnh->vni)) >> 8); | |
580 | tnl->flags |= FLOW_TNL_F_KEY; | |
581 | ||
582 | memcpy(tnl->metadata.opts.gnv, gnh->options, opts_len); | |
583 | tnl->metadata.present.len = opts_len; | |
584 | tnl->flags |= FLOW_TNL_F_UDPIF; | |
585 | ||
586 | dp_packet_reset_packet(packet, hlen); | |
587 | ||
1c8f98d9 PS |
588 | return packet; |
589 | err: | |
590 | dp_packet_delete(packet); | |
591 | return NULL; | |
6b241d64 PS |
592 | } |
593 | ||
594 | int | |
595 | netdev_geneve_build_header(const struct netdev *netdev, | |
596 | struct ovs_action_push_tnl *data, | |
4975aa3e | 597 | const struct netdev_tnl_build_header_params *params) |
6b241d64 PS |
598 | { |
599 | struct netdev_vport *dev = netdev_vport_cast(netdev); | |
600 | struct netdev_tunnel_config *tnl_cfg; | |
601 | struct genevehdr *gnh; | |
602 | int opt_len; | |
603 | bool crit_opt; | |
6b241d64 PS |
604 | |
605 | /* XXX: RCUfy tnl_cfg. */ | |
606 | ovs_mutex_lock(&dev->mutex); | |
607 | tnl_cfg = &dev->tnl_cfg; | |
608 | ||
4975aa3e | 609 | gnh = udp_build_header(tnl_cfg, data, params); |
6b241d64 | 610 | |
4975aa3e | 611 | put_16aligned_be32(&gnh->vni, htonl(ntohll(params->flow->tunnel.tun_id) << 8)); |
6b241d64 PS |
612 | |
613 | ovs_mutex_unlock(&dev->mutex); | |
614 | ||
4975aa3e | 615 | opt_len = tun_metadata_to_geneve_header(¶ms->flow->tunnel, |
6b241d64 PS |
616 | gnh->options, &crit_opt); |
617 | ||
618 | gnh->opt_len = opt_len / 4; | |
4975aa3e | 619 | gnh->oam = !!(params->flow->tunnel.flags & FLOW_TNL_F_OAM); |
6b241d64 PS |
620 | gnh->critical = crit_opt ? 1 : 0; |
621 | gnh->proto_type = htons(ETH_TYPE_TEB); | |
622 | ||
4975aa3e | 623 | data->header_len += sizeof *gnh + opt_len; |
6b241d64 PS |
624 | data->tnl_type = OVS_VPORT_TYPE_GENEVE; |
625 | return 0; | |
626 | } | |
627 | ||
628 | \f | |
629 | void | |
630 | netdev_tnl_egress_port_range(struct unixctl_conn *conn, int argc, | |
631 | const char *argv[], void *aux OVS_UNUSED) | |
632 | { | |
633 | int val1, val2; | |
634 | ||
635 | if (argc < 3) { | |
636 | struct ds ds = DS_EMPTY_INITIALIZER; | |
637 | ||
638 | ds_put_format(&ds, "Tunnel UDP source port range: %"PRIu16"-%"PRIu16"\n", | |
639 | tnl_udp_port_min, tnl_udp_port_max); | |
640 | ||
641 | unixctl_command_reply(conn, ds_cstr(&ds)); | |
642 | ds_destroy(&ds); | |
643 | return; | |
644 | } | |
645 | ||
646 | if (argc != 3) { | |
647 | return; | |
648 | } | |
649 | ||
650 | val1 = atoi(argv[1]); | |
651 | if (val1 <= 0 || val1 > UINT16_MAX) { | |
652 | unixctl_command_reply(conn, "Invalid min."); | |
653 | return; | |
654 | } | |
655 | val2 = atoi(argv[2]); | |
656 | if (val2 <= 0 || val2 > UINT16_MAX) { | |
657 | unixctl_command_reply(conn, "Invalid max."); | |
658 | return; | |
659 | } | |
660 | ||
661 | if (val1 > val2) { | |
662 | tnl_udp_port_min = val2; | |
663 | tnl_udp_port_max = val1; | |
664 | } else { | |
665 | tnl_udp_port_min = val1; | |
666 | tnl_udp_port_max = val2; | |
667 | } | |
668 | seq_change(tnl_conf_seq); | |
669 | ||
670 | unixctl_command_reply(conn, "OK"); | |
671 | } |