2 * Copyright (C) 2014-2015 Vincenzo Maffione
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #if defined(__FreeBSD__)
30 #include <sys/cdefs.h> /* prerequisite */
32 #include <sys/types.h>
33 #include <sys/errno.h>
34 #include <sys/param.h> /* defines used in kernel.h */
35 #include <sys/kernel.h> /* types used in module initialization */
36 #include <sys/sockio.h>
37 #include <sys/malloc.h>
38 #include <sys/socketvar.h> /* struct socket */
39 #include <sys/socket.h> /* sockaddrs */
41 #include <net/if_var.h>
42 #include <machine/bus.h> /* bus_dmamap_* */
43 #include <sys/endian.h>
49 #elif defined(__APPLE__)
51 #warning OSX support is only partial
56 #error Unsupported platform
58 #endif /* unsupported */
60 #include <net/netmap.h>
61 #include <dev/netmap/netmap_kern.h>
65 /* This routine is called by bdg_mismatch_datapath() when it finishes
66 * accumulating bytes for a segment, in order to fix some fields in the
67 * segment headers (which still contain the same content as the header
68 * of the original GSO packet). 'pkt' points to the beginning of the IP
69 * header of the segment, while 'len' is the length of the IP packet.
72 gso_fix_segment(uint8_t *pkt, size_t len, u_int ipv4, u_int iphlen, u_int tcp,
73 u_int idx, u_int segmented_bytes, u_int last_segment)
75 struct nm_iphdr *iph = (struct nm_iphdr *)(pkt);
76 struct nm_ipv6hdr *ip6h = (struct nm_ipv6hdr *)(pkt);
77 uint16_t *check = NULL;
78 uint8_t *check_data = NULL;
81 /* Set the IPv4 "Total Length" field. */
82 iph->tot_len = htobe16(len);
83 ND("ip total length %u", be16toh(ip->tot_len));
85 /* Set the IPv4 "Identification" field. */
86 iph->id = htobe16(be16toh(iph->id) + idx);
87 ND("ip identification %u", be16toh(iph->id));
89 /* Compute and insert the IPv4 header checksum. */
91 iph->check = nm_os_csum_ipv4(iph);
92 ND("IP csum %x", be16toh(iph->check));
94 /* Set the IPv6 "Payload Len" field. */
95 ip6h->payload_len = htobe16(len-iphlen);
99 struct nm_tcphdr *tcph = (struct nm_tcphdr *)(pkt + iphlen);
101 /* Set the TCP sequence number. */
102 tcph->seq = htobe32(be32toh(tcph->seq) + segmented_bytes);
103 ND("tcp seq %u", be32toh(tcph->seq));
105 /* Zero the PSH and FIN TCP flags if this is not the last
108 tcph->flags &= ~(0x8 | 0x1);
109 ND("last_segment %u", last_segment);
111 check = &tcph->check;
112 check_data = (uint8_t *)tcph;
114 struct nm_udphdr *udph = (struct nm_udphdr *)(pkt + iphlen);
116 /* Set the UDP 'Length' field. */
117 udph->len = htobe16(len-iphlen);
119 check = &udph->check;
120 check_data = (uint8_t *)udph;
123 /* Compute and insert TCP/UDP checksum. */
126 nm_os_csum_tcpudp_ipv4(iph, check_data, len-iphlen, check);
128 nm_os_csum_tcpudp_ipv6(ip6h, check_data, len-iphlen, check);
130 ND("TCP/UDP csum %x", be16toh(*check));
134 vnet_hdr_is_bad(struct nm_vnet_hdr *vh)
136 uint8_t gso_type = vh->gso_type & ~VIRTIO_NET_HDR_GSO_ECN;
139 (gso_type != VIRTIO_NET_HDR_GSO_NONE &&
140 gso_type != VIRTIO_NET_HDR_GSO_TCPV4 &&
141 gso_type != VIRTIO_NET_HDR_GSO_UDP &&
142 gso_type != VIRTIO_NET_HDR_GSO_TCPV6)
144 (vh->flags & ~(VIRTIO_NET_HDR_F_NEEDS_CSUM
145 | VIRTIO_NET_HDR_F_DATA_VALID))
149 /* The VALE mismatch datapath implementation. */
151 bdg_mismatch_datapath(struct netmap_vp_adapter *na,
152 struct netmap_vp_adapter *dst_na,
153 const struct nm_bdg_fwd *ft_p,
154 struct netmap_ring *dst_ring,
155 u_int *j, u_int lim, u_int *howmany)
157 struct netmap_slot *dst_slot = NULL;
158 struct nm_vnet_hdr *vh = NULL;
159 const struct nm_bdg_fwd *ft_end = ft_p + ft_p->ft_frags;
161 /* Source and destination pointers. */
163 size_t src_len, dst_len;
165 /* Indices and counters for the destination ring. */
167 u_int j_cur = j_start;
170 if (unlikely(ft_p == ft_end)) {
171 RD(3, "No source slots to process");
175 /* Init source and dest pointers. */
177 src_len = ft_p->ft_len;
178 dst_slot = &dst_ring->slot[j_cur];
179 dst = NMB(&dst_na->up, dst_slot);
182 /* If the source port uses the offloadings, while destination doesn't,
183 * we grab the source virtio-net header and do the offloadings here.
185 if (na->up.virt_hdr_len && !dst_na->up.virt_hdr_len) {
186 vh = (struct nm_vnet_hdr *)src;
187 /* Initial sanity check on the source virtio-net header. If
188 * something seems wrong, just drop the packet. */
189 if (src_len < na->up.virt_hdr_len) {
190 RD(3, "Short src vnet header, dropping");
193 if (vnet_hdr_is_bad(vh)) {
194 RD(3, "Bad src vnet header, dropping");
199 /* We are processing the first input slot and there is a mismatch
200 * between source and destination virt_hdr_len (SHL and DHL).
201 * When the a client is using virtio-net headers, the header length
203 * - 10: the header corresponds to the struct nm_vnet_hdr
204 * - 12: the first 10 bytes correspond to the struct
205 * virtio_net_hdr, and the last 2 bytes store the
206 * "mergeable buffers" info, which is an optional
207 * hint that can be zeroed for compatibility
209 * The destination header is therefore built according to the
212 * SHL | DHL | destination header
213 * -----------------------------
216 * 10 | 0 | doesn't exist
217 * 10 | 12 | first 10 bytes are copied from source header, last 2 are zero
218 * 12 | 0 | doesn't exist
219 * 12 | 10 | copied from the first 10 bytes of source header
221 bzero(dst, dst_na->up.virt_hdr_len);
222 if (na->up.virt_hdr_len && dst_na->up.virt_hdr_len)
223 memcpy(dst, src, sizeof(struct nm_vnet_hdr));
224 /* Skip the virtio-net headers. */
225 src += na->up.virt_hdr_len;
226 src_len -= na->up.virt_hdr_len;
227 dst += dst_na->up.virt_hdr_len;
228 dst_len = dst_na->up.virt_hdr_len + src_len;
230 /* Here it could be dst_len == 0 (which implies src_len == 0),
231 * so we avoid passing a zero length fragment.
236 src_len = ft_p->ft_len;
240 if (vh && vh->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
242 /* Length of the GSO packet header. */
243 u_int gso_hdr_len = 0;
244 /* Pointer to the GSO packet header. Assume it is in a single fragment. */
245 uint8_t *gso_hdr = NULL;
246 /* Index of the current segment. */
248 /* Payload data bytes segmented so far (e.g. TCP data bytes). */
249 u_int segmented_bytes = 0;
250 /* Is this an IPv4 or IPv6 GSO packet? */
252 /* Length of the IP header (20 if IPv4, 40 if IPv6). */
254 /* Length of the Ethernet header (18 if 802.1q, otherwise 14). */
256 /* Is this a TCP or an UDP GSO packet? */
257 u_int tcp = ((vh->gso_type & ~VIRTIO_NET_HDR_GSO_ECN)
258 == VIRTIO_NET_HDR_GSO_UDP) ? 0 : 1;
260 /* Segment the GSO packet contained into the input slots (frags). */
264 if (dst_slots >= *howmany) {
265 /* We still have work to do, but we've run out of
266 * dst slots, so we have to drop the packet. */
267 RD(3, "Not enough slots, dropping GSO packet");
271 /* Grab the GSO header if we don't have it. */
277 /* Look at the 'Ethertype' field to see if this packet
278 * is IPv4 or IPv6, taking into account VLAN
281 if (src_len < ethhlen) {
282 RD(3, "Short GSO fragment [eth], dropping");
285 ethertype = be16toh(*((uint16_t *)
286 (gso_hdr + ethhlen - 2)));
287 if (ethertype != 0x8100) /* not 802.1q */
292 case 0x0800: /* IPv4 */
294 struct nm_iphdr *iph = (struct nm_iphdr *)
297 if (src_len < ethhlen + 20) {
298 RD(3, "Short GSO fragment "
303 iphlen = 4 * (iph->version_ihl & 0x0F);
306 case 0x86DD: /* IPv6 */
311 RD(3, "Unsupported ethertype, "
312 "dropping GSO packet");
315 ND(3, "type=%04x", ethertype);
317 if (src_len < ethhlen + iphlen) {
318 RD(3, "Short GSO fragment [IP], dropping");
322 /* Compute gso_hdr_len. For TCP we need to read the
323 * content of the 'Data Offset' field.
326 struct nm_tcphdr *tcph = (struct nm_tcphdr *)
327 (gso_hdr + ethhlen + iphlen);
329 if (src_len < ethhlen + iphlen + 20) {
330 RD(3, "Short GSO fragment "
334 gso_hdr_len = ethhlen + iphlen +
335 4 * (tcph->doff >> 4);
337 gso_hdr_len = ethhlen + iphlen + 8; /* UDP */
340 if (src_len < gso_hdr_len) {
341 RD(3, "Short GSO fragment [TCP/UDP], dropping");
345 ND(3, "gso_hdr_len %u gso_mtu %d", gso_hdr_len,
348 /* Advance source pointers. */
350 src_len -= gso_hdr_len;
356 src_len = ft_p->ft_len;
360 /* Fill in the header of the current segment. */
361 if (gso_bytes == 0) {
362 memcpy(dst, gso_hdr, gso_hdr_len);
363 gso_bytes = gso_hdr_len;
366 /* Fill in data and update source and dest pointers. */
368 if (gso_bytes + copy > dst_na->mfs)
369 copy = dst_na->mfs - gso_bytes;
370 memcpy(dst + gso_bytes, src, copy);
375 /* A segment is complete or we have processed all the
376 the GSO payload bytes. */
377 if (gso_bytes >= dst_na->mfs ||
378 (src_len == 0 && ft_p + 1 == ft_end)) {
379 /* After raw segmentation, we must fix some header
380 * fields and compute checksums, in a protocol dependent
382 gso_fix_segment(dst + ethhlen, gso_bytes - ethhlen,
384 gso_idx, segmented_bytes,
385 src_len == 0 && ft_p + 1 == ft_end);
387 ND("frame %u completed with %d bytes", gso_idx, (int)gso_bytes);
388 dst_slot->len = gso_bytes;
391 segmented_bytes += gso_bytes - gso_hdr_len;
396 /* Next destination slot. */
397 j_cur = nm_next(j_cur, lim);
398 dst_slot = &dst_ring->slot[j_cur];
399 dst = NMB(&dst_na->up, dst_slot);
402 /* Next input slot. */
408 src_len = ft_p->ft_len;
411 ND(3, "%d bytes segmented", segmented_bytes);
414 /* Address of a checksum field into a destination slot. */
415 uint16_t *check = NULL;
416 /* Accumulator for an unfolded checksum. */
419 /* Process a non-GSO packet. */
421 /* Init 'check' if necessary. */
422 if (vh && (vh->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)) {
423 if (unlikely(vh->csum_offset + vh->csum_start > src_len))
424 D("invalid checksum request");
426 check = (uint16_t *)(dst + vh->csum_start +
430 while (ft_p != ft_end) {
431 /* Init/update the packet checksum if needed. */
432 if (vh && (vh->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)) {
434 csum = nm_os_csum_raw(src + vh->csum_start,
435 src_len - vh->csum_start, 0);
437 csum = nm_os_csum_raw(src, src_len, csum);
440 /* Round to a multiple of 64 */
441 src_len = (src_len + 63) & ~63;
443 if (ft_p->ft_flags & NS_INDIRECT) {
444 if (copyin(src, dst, src_len)) {
445 /* Invalid user pointer, pretend len is 0. */
449 memcpy(dst, src, (int)src_len);
451 dst_slot->len = dst_len;
454 /* Next destination slot. */
455 j_cur = nm_next(j_cur, lim);
456 dst_slot = &dst_ring->slot[j_cur];
457 dst = NMB(&dst_na->up, dst_slot);
459 /* Next source slot. */
462 dst_len = src_len = ft_p->ft_len;
465 /* Finalize (fold) the checksum if needed. */
466 if (check && vh && (vh->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)) {
467 *check = nm_os_csum_fold(csum);
469 ND(3, "using %u dst_slots", dst_slots);
471 /* A second pass on the destination slots to set the slot flags,
472 * using the right number of destination slots.
474 while (j_start != j_cur) {
475 dst_slot = &dst_ring->slot[j_start];
476 dst_slot->flags = (dst_slots << 8)| NS_MOREFRAG;
477 j_start = nm_next(j_start, lim);
479 /* Clear NS_MOREFRAG flag on last entry. */
480 dst_slot->flags = (dst_slots << 8);
483 /* Update howmany and j. This is to commit the use of
484 * those slots in the destination ring. */
485 if (unlikely(dst_slots > *howmany)) {
486 D("Slot allocation error: This is a bug");
489 *howmany -= dst_slots;