| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/**************************************************************************** |
| 3 |
* Driver for Solarflare network controllers and boards |
| 4 |
* Copyright 2005-2006 Fen Systems Ltd. |
| 5 |
* Copyright 2005-2013 Solarflare Communications Inc. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <linux/socket.h> |
| 9 |
#include <linux/in.h> |
| 10 |
#include <linux/slab.h> |
| 11 |
#include <linux/ip.h> |
| 12 |
#include <linux/ipv6.h> |
| 13 |
#include <linux/tcp.h> |
| 14 |
#include <linux/udp.h> |
| 15 |
#include <linux/prefetch.h> |
| 16 |
#include <linux/moduleparam.h> |
| 17 |
#include <linux/iommu.h> |
| 18 |
#include <net/ip.h> |
| 19 |
#include <net/checksum.h> |
| 20 |
#include <net/xdp.h> |
| 21 |
#include <linux/bpf_trace.h> |
| 22 |
#include "net_driver.h" |
| 23 |
#include "efx.h" |
| 24 |
#include "rx_common.h" |
| 25 |
#include "filter.h" |
| 26 |
#include "nic.h" |
| 27 |
#include "selftest.h" |
| 28 |
#include "workarounds.h" |
| 29 |
|
| 30 |
/* Preferred number of descriptors to fill at once */ |
| 31 |
#define EFX_RX_PREFERRED_BATCH 8U |
| 32 |
|
| 33 |
/* Maximum rx prefix used by any architecture. */ |
| 34 |
#define EFX_MAX_RX_PREFIX_SIZE 16 |
| 35 |
|
| 36 |
/* Size of buffer allocated for skb header area. */ |
| 37 |
#define EFX_SKB_HEADERS 128u |
| 38 |
|
| 39 |
/* Each packet can consume up to ceil(max_frame_len / buffer_size) buffers */ |
| 40 |
#define EFX_RX_MAX_FRAGS DIV_ROUND_UP(EFX_MAX_FRAME_LEN(EFX_MAX_MTU), \ |
| 41 |
EFX_RX_USR_BUF_SIZE) |
| 42 |
|
| 43 |
static void efx_rx_packet__check_len(struct efx_rx_queue *rx_queue, |
| 44 |
struct efx_rx_buffer *rx_buf, |
| 45 |
int len) |
| 46 |
{ |
| 47 |
struct efx_nic *efx = rx_queue->efx; |
| 48 |
unsigned max_len = rx_buf->len - efx->type->rx_buffer_padding; |
| 49 |
|
| 50 |
if (likely(len <= max_len)) |
| 51 |
return; |
| 52 |
|
| 53 |
/* The packet must be discarded, but this is only a fatal error |
| 54 |
* if the caller indicated it was |
| 55 |
*/ |
| 56 |
rx_buf->flags |= EFX_RX_PKT_DISCARD; |
| 57 |
|
| 58 |
if (net_ratelimit()) |
| 59 |
netif_err(efx, rx_err, efx->net_dev, |
| 60 |
"RX queue %d overlength RX event (%#x > %#x)\n", |
| 61 |
efx_rx_queue_index(rx_queue), len, max_len); |
| 62 |
|
| 63 |
efx_rx_queue_channel(rx_queue)->n_rx_overlength++; |
| 64 |
} |
| 65 |
|
| 66 |
/* Allocate and construct an SKB around page fragments */ |
| 67 |
static struct sk_buff *efx_rx_mk_skb(struct efx_channel *channel, |
| 68 |
struct efx_rx_buffer *rx_buf, |
| 69 |
unsigned int n_frags, |
| 70 |
u8 *eh, int hdr_len) |
| 71 |
{ |
| 72 |
struct efx_nic *efx = channel->efx; |
| 73 |
struct sk_buff *skb; |
| 74 |
|
| 75 |
/* Allocate an SKB to store the headers */ |
| 76 |
skb = netdev_alloc_skb(efx->net_dev, |
| 77 |
efx->rx_ip_align + efx->rx_prefix_size + |
| 78 |
hdr_len); |
| 79 |
if (unlikely(skb == NULL)) { |
| 80 |
atomic_inc(&efx->n_rx_noskb_drops); |
| 81 |
return NULL; |
| 82 |
} |
| 83 |
|
| 84 |
EFX_WARN_ON_ONCE_PARANOID(rx_buf->len < hdr_len); |
| 85 |
|
| 86 |
memcpy(skb->data + efx->rx_ip_align, eh - efx->rx_prefix_size, |
| 87 |
efx->rx_prefix_size + hdr_len); |
| 88 |
skb_reserve(skb, efx->rx_ip_align + efx->rx_prefix_size); |
| 89 |
__skb_put(skb, hdr_len); |
| 90 |
|
| 91 |
/* Append the remaining page(s) onto the frag list */ |
| 92 |
if (rx_buf->len > hdr_len) { |
| 93 |
rx_buf->page_offset += hdr_len; |
| 94 |
rx_buf->len -= hdr_len; |
| 95 |
|
| 96 |
for (;;) { |
| 97 |
skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, |
| 98 |
rx_buf->page, rx_buf->page_offset, |
| 99 |
rx_buf->len, efx->rx_buffer_truesize); |
| 100 |
rx_buf->page = NULL; |
| 101 |
|
| 102 |
if (skb_shinfo(skb)->nr_frags == n_frags) |
| 103 |
break; |
| 104 |
|
| 105 |
rx_buf = efx_rx_buf_next(&channel->rx_queue, rx_buf); |
| 106 |
} |
| 107 |
} else { |
| 108 |
__free_pages(rx_buf->page, efx->rx_buffer_order); |
| 109 |
rx_buf->page = NULL; |
| 110 |
n_frags = 0; |
| 111 |
} |
| 112 |
|
| 113 |
/* Move past the ethernet header */ |
| 114 |
skb->protocol = eth_type_trans(skb, efx->net_dev); |
| 115 |
|
| 116 |
skb_mark_napi_id(skb, &channel->napi_str); |
| 117 |
|
| 118 |
return skb; |
| 119 |
} |
| 120 |
|
| 121 |
void efx_rx_packet(struct efx_rx_queue *rx_queue, unsigned int index, |
| 122 |
unsigned int n_frags, unsigned int len, u16 flags) |
| 123 |
{ |
| 124 |
struct efx_nic *efx = rx_queue->efx; |
| 125 |
struct efx_channel *channel = efx_rx_queue_channel(rx_queue); |
| 126 |
struct efx_rx_buffer *rx_buf; |
| 127 |
|
| 128 |
rx_buf = efx_rx_buffer(rx_queue, index); |
| 129 |
rx_buf->flags |= flags; |
| 130 |
|
| 131 |
/* Validate the number of fragments and completed length */ |
| 132 |
if (n_frags == 1) { |
| 133 |
if (!(flags & EFX_RX_PKT_PREFIX_LEN)) |
| 134 |
efx_rx_packet__check_len(rx_queue, rx_buf, len); |
| 135 |
} else if (unlikely(n_frags > EFX_RX_MAX_FRAGS) || |
| 136 |
unlikely(len <= (n_frags - 1) * efx->rx_dma_len) || |
| 137 |
unlikely(len > n_frags * efx->rx_dma_len) || |
| 138 |
unlikely(!efx->rx_scatter)) { |
| 139 |
/* If this isn't an explicit discard request, either |
| 140 |
* the hardware or the driver is broken. |
| 141 |
*/ |
| 142 |
WARN_ON(!(len == 0 && rx_buf->flags & EFX_RX_PKT_DISCARD)); |
| 143 |
rx_buf->flags |= EFX_RX_PKT_DISCARD; |
| 144 |
} |
| 145 |
|
| 146 |
netif_vdbg(efx, rx_status, efx->net_dev, |
| 147 |
"RX queue %d received ids %x-%x len %d %s%s\n", |
| 148 |
efx_rx_queue_index(rx_queue), index, |
| 149 |
(index + n_frags - 1) & rx_queue->ptr_mask, len, |
| 150 |
(rx_buf->flags & EFX_RX_PKT_CSUMMED) ? " [SUMMED]" : "", |
| 151 |
(rx_buf->flags & EFX_RX_PKT_DISCARD) ? " [DISCARD]" : ""); |
| 152 |
|
| 153 |
/* Discard packet, if instructed to do so. Process the |
| 154 |
* previous receive first. |
| 155 |
*/ |
| 156 |
if (unlikely(rx_buf->flags & EFX_RX_PKT_DISCARD)) { |
| 157 |
efx_rx_flush_packet(channel); |
| 158 |
efx_discard_rx_packet(channel, rx_buf, n_frags); |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
if (n_frags == 1 && !(flags & EFX_RX_PKT_PREFIX_LEN)) |
| 163 |
rx_buf->len = len; |
| 164 |
|
| 165 |
/* Release and/or sync the DMA mapping - assumes all RX buffers |
| 166 |
* consumed in-order per RX queue. |
| 167 |
*/ |
| 168 |
efx_sync_rx_buffer(efx, rx_buf, rx_buf->len); |
| 169 |
|
| 170 |
/* Prefetch nice and early so data will (hopefully) be in cache by |
| 171 |
* the time we look at it. |
| 172 |
*/ |
| 173 |
prefetch(efx_rx_buf_va(rx_buf)); |
| 174 |
|
| 175 |
rx_buf->page_offset += efx->rx_prefix_size; |
| 176 |
rx_buf->len -= efx->rx_prefix_size; |
| 177 |
|
| 178 |
if (n_frags > 1) { |
| 179 |
/* Release/sync DMA mapping for additional fragments. |
| 180 |
* Fix length for last fragment. |
| 181 |
*/ |
| 182 |
unsigned int tail_frags = n_frags - 1; |
| 183 |
|
| 184 |
for (;;) { |
| 185 |
rx_buf = efx_rx_buf_next(rx_queue, rx_buf); |
| 186 |
if (--tail_frags == 0) |
| 187 |
break; |
| 188 |
efx_sync_rx_buffer(efx, rx_buf, efx->rx_dma_len); |
| 189 |
} |
| 190 |
rx_buf->len = len - (n_frags - 1) * efx->rx_dma_len; |
| 191 |
efx_sync_rx_buffer(efx, rx_buf, rx_buf->len); |
| 192 |
} |
| 193 |
|
| 194 |
/* All fragments have been DMA-synced, so recycle pages. */ |
| 195 |
rx_buf = efx_rx_buffer(rx_queue, index); |
| 196 |
efx_recycle_rx_pages(channel, rx_buf, n_frags); |
| 197 |
|
| 198 |
/* Pipeline receives so that we give time for packet headers to be |
| 199 |
* prefetched into cache. |
| 200 |
*/ |
| 201 |
efx_rx_flush_packet(channel); |
| 202 |
channel->rx_pkt_n_frags = n_frags; |
| 203 |
channel->rx_pkt_index = index; |
| 204 |
} |
| 205 |
|
| 206 |
static void efx_rx_deliver(struct efx_channel *channel, u8 *eh, |
| 207 |
struct efx_rx_buffer *rx_buf, |
| 208 |
unsigned int n_frags) |
| 209 |
{ |
| 210 |
struct sk_buff *skb; |
| 211 |
u16 hdr_len = min_t(u16, rx_buf->len, EFX_SKB_HEADERS); |
| 212 |
|
| 213 |
skb = efx_rx_mk_skb(channel, rx_buf, n_frags, eh, hdr_len); |
| 214 |
if (unlikely(skb == NULL)) { |
| 215 |
struct efx_rx_queue *rx_queue; |
| 216 |
|
| 217 |
rx_queue = efx_channel_get_rx_queue(channel); |
| 218 |
efx_free_rx_buffers(rx_queue, rx_buf, n_frags); |
| 219 |
return; |
| 220 |
} |
| 221 |
skb_record_rx_queue(skb, channel->rx_queue.core_index); |
| 222 |
|
| 223 |
/* Set the SKB flags */ |
| 224 |
skb_checksum_none_assert(skb); |
| 225 |
if (likely(rx_buf->flags & EFX_RX_PKT_CSUMMED)) { |
| 226 |
skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 227 |
skb->csum_level = !!(rx_buf->flags & EFX_RX_PKT_CSUM_LEVEL); |
| 228 |
} |
| 229 |
|
| 230 |
efx_rx_skb_attach_timestamp(channel, skb); |
| 231 |
|
| 232 |
if (channel->type->receive_skb) |
| 233 |
if (channel->type->receive_skb(channel, skb)) |
| 234 |
return; |
| 235 |
|
| 236 |
/* Pass the packet up */ |
| 237 |
if (channel->rx_list != NULL) |
| 238 |
/* Add to list, will pass up later */ |
| 239 |
list_add_tail(&skb->list, channel->rx_list); |
| 240 |
else |
| 241 |
/* No list, so pass it up now */ |
| 242 |
netif_receive_skb(skb); |
| 243 |
} |
| 244 |
|
| 245 |
/** efx_do_xdp: perform XDP processing on a received packet |
| 246 |
* |
| 247 |
* Returns true if packet should still be delivered. |
| 248 |
*/ |
| 249 |
static bool efx_do_xdp(struct efx_nic *efx, struct efx_channel *channel, |
| 250 |
struct efx_rx_buffer *rx_buf, u8 **ehp) |
| 251 |
{ |
| 252 |
u8 rx_prefix[EFX_MAX_RX_PREFIX_SIZE]; |
| 253 |
struct efx_rx_queue *rx_queue; |
| 254 |
struct bpf_prog *xdp_prog; |
| 255 |
struct xdp_frame *xdpf; |
| 256 |
struct xdp_buff xdp; |
| 257 |
u32 xdp_act; |
| 258 |
s16 offset; |
| 259 |
int err; |
| 260 |
|
| 261 |
xdp_prog = rcu_dereference_bh(efx->xdp_prog); |
| 262 |
if (!xdp_prog) |
| 263 |
return true; |
| 264 |
|
| 265 |
rx_queue = efx_channel_get_rx_queue(channel); |
| 266 |
|
| 267 |
if (unlikely(channel->rx_pkt_n_frags > 1)) { |
| 268 |
/* We can't do XDP on fragmented packets - drop. */ |
| 269 |
efx_free_rx_buffers(rx_queue, rx_buf, |
| 270 |
channel->rx_pkt_n_frags); |
| 271 |
if (net_ratelimit()) |
| 272 |
netif_err(efx, rx_err, efx->net_dev, |
| 273 |
"XDP is not possible with multiple receive fragments (%d)\n", |
| 274 |
channel->rx_pkt_n_frags); |
| 275 |
channel->n_rx_xdp_bad_drops++; |
| 276 |
return false; |
| 277 |
} |
| 278 |
|
| 279 |
dma_sync_single_for_cpu(&efx->pci_dev->dev, rx_buf->dma_addr, |
| 280 |
rx_buf->len, DMA_FROM_DEVICE); |
| 281 |
|
| 282 |
/* Save the rx prefix. */ |
| 283 |
EFX_WARN_ON_PARANOID(efx->rx_prefix_size > EFX_MAX_RX_PREFIX_SIZE); |
| 284 |
memcpy(rx_prefix, *ehp - efx->rx_prefix_size, |
| 285 |
efx->rx_prefix_size); |
| 286 |
|
| 287 |
xdp_init_buff(&xdp, efx->rx_page_buf_step, &rx_queue->xdp_rxq_info); |
| 288 |
/* No support yet for XDP metadata */ |
| 289 |
xdp_prepare_buff(&xdp, *ehp - EFX_XDP_HEADROOM, EFX_XDP_HEADROOM, |
| 290 |
rx_buf->len, false); |
| 291 |
|
| 292 |
xdp_act = bpf_prog_run_xdp(xdp_prog, &xdp); |
| 293 |
|
| 294 |
offset = (u8 *)xdp.data - *ehp; |
| 295 |
|
| 296 |
switch (xdp_act) { |
| 297 |
case XDP_PASS: |
| 298 |
/* Fix up rx prefix. */ |
| 299 |
if (offset) { |
| 300 |
*ehp += offset; |
| 301 |
rx_buf->page_offset += offset; |
| 302 |
rx_buf->len -= offset; |
| 303 |
memcpy(*ehp - efx->rx_prefix_size, rx_prefix, |
| 304 |
efx->rx_prefix_size); |
| 305 |
} |
| 306 |
break; |
| 307 |
|
| 308 |
case XDP_TX: |
| 309 |
/* Buffer ownership passes to tx on success. */ |
| 310 |
xdpf = xdp_convert_buff_to_frame(&xdp); |
| 311 |
err = efx_xdp_tx_buffers(efx, 1, &xdpf, true); |
| 312 |
if (unlikely(err != 1)) { |
| 313 |
efx_free_rx_buffers(rx_queue, rx_buf, 1); |
| 314 |
if (net_ratelimit()) |
| 315 |
netif_err(efx, rx_err, efx->net_dev, |
| 316 |
"XDP TX failed (%d)\n", err); |
| 317 |
channel->n_rx_xdp_bad_drops++; |
| 318 |
trace_xdp_exception(efx->net_dev, xdp_prog, xdp_act); |
| 319 |
} else { |
| 320 |
channel->n_rx_xdp_tx++; |
| 321 |
} |
| 322 |
break; |
| 323 |
|
| 324 |
case XDP_REDIRECT: |
| 325 |
err = xdp_do_redirect(efx->net_dev, &xdp, xdp_prog); |
| 326 |
if (unlikely(err)) { |
| 327 |
efx_free_rx_buffers(rx_queue, rx_buf, 1); |
| 328 |
if (net_ratelimit()) |
| 329 |
netif_err(efx, rx_err, efx->net_dev, |
| 330 |
"XDP redirect failed (%d)\n", err); |
| 331 |
channel->n_rx_xdp_bad_drops++; |
| 332 |
trace_xdp_exception(efx->net_dev, xdp_prog, xdp_act); |
| 333 |
} else { |
| 334 |
channel->n_rx_xdp_redirect++; |
| 335 |
} |
| 336 |
break; |
| 337 |
|
| 338 |
default: |
| 339 |
bpf_warn_invalid_xdp_action(efx->net_dev, xdp_prog, xdp_act); |
| 340 |
efx_free_rx_buffers(rx_queue, rx_buf, 1); |
| 341 |
channel->n_rx_xdp_bad_drops++; |
| 342 |
trace_xdp_exception(efx->net_dev, xdp_prog, xdp_act); |
| 343 |
break; |
| 344 |
|
| 345 |
case XDP_ABORTED: |
| 346 |
trace_xdp_exception(efx->net_dev, xdp_prog, xdp_act); |
| 347 |
fallthrough; |
| 348 |
case XDP_DROP: |
| 349 |
efx_free_rx_buffers(rx_queue, rx_buf, 1); |
| 350 |
channel->n_rx_xdp_drops++; |
| 351 |
break; |
| 352 |
} |
| 353 |
|
| 354 |
return xdp_act == XDP_PASS; |
| 355 |
} |
| 356 |
|
| 357 |
/* Handle a received packet. Second half: Touches packet payload. */ |
| 358 |
void __efx_rx_packet(struct efx_channel *channel) |
| 359 |
{ |
| 360 |
struct efx_rx_queue *rx_queue = efx_channel_get_rx_queue(channel); |
| 361 |
struct efx_nic *efx = channel->efx; |
| 362 |
struct efx_rx_buffer *rx_buf = |
| 363 |
efx_rx_buffer(rx_queue, channel->rx_pkt_index); |
| 364 |
u8 *eh = efx_rx_buf_va(rx_buf); |
| 365 |
|
| 366 |
/* Read length from the prefix if necessary. This already |
| 367 |
* excludes the length of the prefix itself. |
| 368 |
*/ |
| 369 |
if (rx_buf->flags & EFX_RX_PKT_PREFIX_LEN) { |
| 370 |
rx_buf->len = le16_to_cpup((__le16 *) |
| 371 |
(eh + efx->rx_packet_len_offset)); |
| 372 |
/* A known issue may prevent this being filled in; |
| 373 |
* if that happens, just drop the packet. |
| 374 |
* Must do that in the driver since passing a zero-length |
| 375 |
* packet up to the stack may cause a crash. |
| 376 |
*/ |
| 377 |
if (unlikely(!rx_buf->len)) { |
| 378 |
efx_free_rx_buffers(rx_queue, rx_buf, |
| 379 |
channel->rx_pkt_n_frags); |
| 380 |
channel->n_rx_frm_trunc++; |
| 381 |
goto out; |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
/* If we're in loopback test, then pass the packet directly to the |
| 386 |
* loopback layer, and free the rx_buf here |
| 387 |
*/ |
| 388 |
if (unlikely(efx->loopback_selftest)) { |
| 389 |
efx_loopback_rx_packet(efx, eh, rx_buf->len); |
| 390 |
efx_free_rx_buffers(rx_queue, rx_buf, |
| 391 |
channel->rx_pkt_n_frags); |
| 392 |
goto out; |
| 393 |
} |
| 394 |
|
| 395 |
rx_queue->rx_packets++; |
| 396 |
rx_queue->rx_bytes += rx_buf->len; |
| 397 |
|
| 398 |
if (!efx_do_xdp(efx, channel, rx_buf, &eh)) |
| 399 |
goto out; |
| 400 |
|
| 401 |
if (unlikely(!(efx->net_dev->features & NETIF_F_RXCSUM))) |
| 402 |
rx_buf->flags &= ~EFX_RX_PKT_CSUMMED; |
| 403 |
|
| 404 |
if ((rx_buf->flags & EFX_RX_PKT_TCP) && !channel->type->receive_skb) |
| 405 |
efx_rx_packet_gro(channel, rx_buf, channel->rx_pkt_n_frags, eh, 0); |
| 406 |
else |
| 407 |
efx_rx_deliver(channel, eh, rx_buf, channel->rx_pkt_n_frags); |
| 408 |
out: |
| 409 |
channel->rx_pkt_n_frags = 0; |
| 410 |
} |