rx.c 11.6 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2
/****************************************************************************
B
Ben Hutchings 已提交
3
 * Driver for Solarflare network controllers and boards
4
 * Copyright 2005-2006 Fen Systems Ltd.
B
Ben Hutchings 已提交
5
 * Copyright 2005-2013 Solarflare Communications Inc.
6 7 8 9
 */

#include <linux/socket.h>
#include <linux/in.h>
10
#include <linux/slab.h>
11
#include <linux/ip.h>
B
Ben Hutchings 已提交
12
#include <linux/ipv6.h>
13 14
#include <linux/tcp.h>
#include <linux/udp.h>
15
#include <linux/prefetch.h>
16
#include <linux/moduleparam.h>
17
#include <linux/iommu.h>
18 19
#include <net/ip.h>
#include <net/checksum.h>
20 21
#include <net/xdp.h>
#include <linux/bpf_trace.h>
22 23
#include "net_driver.h"
#include "efx.h"
24
#include "rx_common.h"
25
#include "filter.h"
B
Ben Hutchings 已提交
26
#include "nic.h"
27
#include "selftest.h"
28 29
#include "workarounds.h"

30 31
/* Preferred number of descriptors to fill at once */
#define EFX_RX_PREFERRED_BATCH 8U
32

33 34 35
/* Maximum rx prefix used by any architecture. */
#define EFX_MAX_RX_PREFIX_SIZE 16

36
/* Size of buffer allocated for skb header area. */
37
#define EFX_SKB_HEADERS  128u
38

39 40 41 42
/* Each packet can consume up to ceil(max_frame_len / buffer_size) buffers */
#define EFX_RX_MAX_FRAGS DIV_ROUND_UP(EFX_MAX_FRAME_LEN(EFX_MAX_MTU), \
				      EFX_RX_USR_BUF_SIZE)

43 44 45 46 47 48 49 50
static inline void efx_sync_rx_buffer(struct efx_nic *efx,
				      struct efx_rx_buffer *rx_buf,
				      unsigned int len)
{
	dma_sync_single_for_cpu(&efx->pci_dev->dev, rx_buf->dma_addr, len,
				DMA_FROM_DEVICE);
}

51 52
static void efx_rx_packet__check_len(struct efx_rx_queue *rx_queue,
				     struct efx_rx_buffer *rx_buf,
53
				     int len)
54 55 56 57 58 59 60 61 62 63
{
	struct efx_nic *efx = rx_queue->efx;
	unsigned max_len = rx_buf->len - efx->type->rx_buffer_padding;

	if (likely(len <= max_len))
		return;

	/* The packet must be discarded, but this is only a fatal error
	 * if the caller indicated it was
	 */
64
	rx_buf->flags |= EFX_RX_PKT_DISCARD;
65

66 67 68 69
	if (net_ratelimit())
		netif_err(efx, rx_err, efx->net_dev,
			  "RX queue %d overlength RX event (%#x > %#x)\n",
			  efx_rx_queue_index(rx_queue), len, max_len);
70

71
	efx_rx_queue_channel(rx_queue)->n_rx_overlength++;
72 73
}

74
/* Allocate and construct an SKB around page fragments */
75 76
static struct sk_buff *efx_rx_mk_skb(struct efx_channel *channel,
				     struct efx_rx_buffer *rx_buf,
77
				     unsigned int n_frags,
78 79 80 81
				     u8 *eh, int hdr_len)
{
	struct efx_nic *efx = channel->efx;
	struct sk_buff *skb;
82

83
	/* Allocate an SKB to store the headers */
84 85 86
	skb = netdev_alloc_skb(efx->net_dev,
			       efx->rx_ip_align + efx->rx_prefix_size +
			       hdr_len);
87 88
	if (unlikely(skb == NULL)) {
		atomic_inc(&efx->n_rx_noskb_drops);
89
		return NULL;
90
	}
91

92
	EFX_WARN_ON_ONCE_PARANOID(rx_buf->len < hdr_len);
93

94 95 96 97
	memcpy(skb->data + efx->rx_ip_align, eh - efx->rx_prefix_size,
	       efx->rx_prefix_size + hdr_len);
	skb_reserve(skb, efx->rx_ip_align + efx->rx_prefix_size);
	__skb_put(skb, hdr_len);
98

99
	/* Append the remaining page(s) onto the frag list */
100
	if (rx_buf->len > hdr_len) {
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
		rx_buf->page_offset += hdr_len;
		rx_buf->len -= hdr_len;

		for (;;) {
			skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
					   rx_buf->page, rx_buf->page_offset,
					   rx_buf->len);
			rx_buf->page = NULL;
			skb->len += rx_buf->len;
			skb->data_len += rx_buf->len;
			if (skb_shinfo(skb)->nr_frags == n_frags)
				break;

			rx_buf = efx_rx_buf_next(&channel->rx_queue, rx_buf);
		}
116 117
	} else {
		__free_pages(rx_buf->page, efx->rx_buffer_order);
118 119
		rx_buf->page = NULL;
		n_frags = 0;
120
	}
121

122
	skb->truesize += n_frags * efx->rx_buffer_truesize;
123 124 125 126

	/* Move past the ethernet header */
	skb->protocol = eth_type_trans(skb, efx->net_dev);

127 128
	skb_mark_napi_id(skb, &channel->napi_str);

129
	return skb;
130 131 132
}

void efx_rx_packet(struct efx_rx_queue *rx_queue, unsigned int index,
133
		   unsigned int n_frags, unsigned int len, u16 flags)
134 135
{
	struct efx_nic *efx = rx_queue->efx;
136
	struct efx_channel *channel = efx_rx_queue_channel(rx_queue);
137 138
	struct efx_rx_buffer *rx_buf;

139 140
	rx_queue->rx_packets++;

141
	rx_buf = efx_rx_buffer(rx_queue, index);
142
	rx_buf->flags |= flags;
143

144 145
	/* Validate the number of fragments and completed length */
	if (n_frags == 1) {
146 147
		if (!(flags & EFX_RX_PKT_PREFIX_LEN))
			efx_rx_packet__check_len(rx_queue, rx_buf, len);
148
	} else if (unlikely(n_frags > EFX_RX_MAX_FRAGS) ||
J
Jon Cooper 已提交
149 150
		   unlikely(len <= (n_frags - 1) * efx->rx_dma_len) ||
		   unlikely(len > n_frags * efx->rx_dma_len) ||
151 152 153 154 155 156 157
		   unlikely(!efx->rx_scatter)) {
		/* If this isn't an explicit discard request, either
		 * the hardware or the driver is broken.
		 */
		WARN_ON(!(len == 0 && rx_buf->flags & EFX_RX_PKT_DISCARD));
		rx_buf->flags |= EFX_RX_PKT_DISCARD;
	}
158

159
	netif_vdbg(efx, rx_status, efx->net_dev,
160
		   "RX queue %d received ids %x-%x len %d %s%s\n",
161
		   efx_rx_queue_index(rx_queue), index,
162
		   (index + n_frags - 1) & rx_queue->ptr_mask, len,
163 164
		   (rx_buf->flags & EFX_RX_PKT_CSUMMED) ? " [SUMMED]" : "",
		   (rx_buf->flags & EFX_RX_PKT_DISCARD) ? " [DISCARD]" : "");
165

166 167 168
	/* Discard packet, if instructed to do so.  Process the
	 * previous receive first.
	 */
169
	if (unlikely(rx_buf->flags & EFX_RX_PKT_DISCARD)) {
170
		efx_rx_flush_packet(channel);
171
		efx_discard_rx_packet(channel, rx_buf, n_frags);
172
		return;
173 174
	}

175
	if (n_frags == 1 && !(flags & EFX_RX_PKT_PREFIX_LEN))
176 177
		rx_buf->len = len;

178 179
	/* Release and/or sync the DMA mapping - assumes all RX buffers
	 * consumed in-order per RX queue.
180
	 */
181
	efx_sync_rx_buffer(efx, rx_buf, rx_buf->len);
182 183 184 185

	/* Prefetch nice and early so data will (hopefully) be in cache by
	 * the time we look at it.
	 */
186
	prefetch(efx_rx_buf_va(rx_buf));
187

188 189
	rx_buf->page_offset += efx->rx_prefix_size;
	rx_buf->len -= efx->rx_prefix_size;
190 191 192 193 194 195 196 197 198 199 200

	if (n_frags > 1) {
		/* Release/sync DMA mapping for additional fragments.
		 * Fix length for last fragment.
		 */
		unsigned int tail_frags = n_frags - 1;

		for (;;) {
			rx_buf = efx_rx_buf_next(rx_queue, rx_buf);
			if (--tail_frags == 0)
				break;
J
Jon Cooper 已提交
201
			efx_sync_rx_buffer(efx, rx_buf, efx->rx_dma_len);
202
		}
J
Jon Cooper 已提交
203
		rx_buf->len = len - (n_frags - 1) * efx->rx_dma_len;
204
		efx_sync_rx_buffer(efx, rx_buf, rx_buf->len);
205
	}
206

207
	/* All fragments have been DMA-synced, so recycle pages. */
208
	rx_buf = efx_rx_buffer(rx_queue, index);
209
	efx_recycle_rx_pages(channel, rx_buf, n_frags);
210

211 212 213
	/* Pipeline receives so that we give time for packet headers to be
	 * prefetched into cache.
	 */
214
	efx_rx_flush_packet(channel);
215 216
	channel->rx_pkt_n_frags = n_frags;
	channel->rx_pkt_index = index;
217 218
}

219
static void efx_rx_deliver(struct efx_channel *channel, u8 *eh,
220 221
			   struct efx_rx_buffer *rx_buf,
			   unsigned int n_frags)
222 223
{
	struct sk_buff *skb;
224
	u16 hdr_len = min_t(u16, rx_buf->len, EFX_SKB_HEADERS);
225

226
	skb = efx_rx_mk_skb(channel, rx_buf, n_frags, eh, hdr_len);
227
	if (unlikely(skb == NULL)) {
228 229 230 231
		struct efx_rx_queue *rx_queue;

		rx_queue = efx_channel_get_rx_queue(channel);
		efx_free_rx_buffers(rx_queue, rx_buf, n_frags);
232 233 234
		return;
	}
	skb_record_rx_queue(skb, channel->rx_queue.core_index);
235 236 237

	/* Set the SKB flags */
	skb_checksum_none_assert(skb);
238
	if (likely(rx_buf->flags & EFX_RX_PKT_CSUMMED)) {
239
		skb->ip_summed = CHECKSUM_UNNECESSARY;
240 241
		skb->csum_level = !!(rx_buf->flags & EFX_RX_PKT_CSUM_LEVEL);
	}
242

243 244
	efx_rx_skb_attach_timestamp(channel, skb);

245
	if (channel->type->receive_skb)
246
		if (channel->type->receive_skb(channel, skb))
247
			return;
248 249

	/* Pass the packet up */
E
Edward Cree 已提交
250 251 252 253 254 255
	if (channel->rx_list != NULL)
		/* Add to list, will pass up later */
		list_add_tail(&skb->list, channel->rx_list);
	else
		/* No list, so pass it up now */
		netif_receive_skb(skb);
256 257
}

258 259 260 261 262 263 264 265 266 267
/** efx_do_xdp: perform XDP processing on a received packet
 *
 * Returns true if packet should still be delivered.
 */
static bool efx_do_xdp(struct efx_nic *efx, struct efx_channel *channel,
		       struct efx_rx_buffer *rx_buf, u8 **ehp)
{
	u8 rx_prefix[EFX_MAX_RX_PREFIX_SIZE];
	struct efx_rx_queue *rx_queue;
	struct bpf_prog *xdp_prog;
268
	struct xdp_frame *xdpf;
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
	struct xdp_buff xdp;
	u32 xdp_act;
	s16 offset;
	int err;

	rcu_read_lock();
	xdp_prog = rcu_dereference(efx->xdp_prog);
	if (!xdp_prog) {
		rcu_read_unlock();
		return true;
	}

	rx_queue = efx_channel_get_rx_queue(channel);

	if (unlikely(channel->rx_pkt_n_frags > 1)) {
		/* We can't do XDP on fragmented packets - drop. */
		rcu_read_unlock();
		efx_free_rx_buffers(rx_queue, rx_buf,
				    channel->rx_pkt_n_frags);
		if (net_ratelimit())
			netif_err(efx, rx_err, efx->net_dev,
				  "XDP is not possible with multiple receive fragments (%d)\n",
				  channel->rx_pkt_n_frags);
292
		channel->n_rx_xdp_bad_drops++;
293 294 295 296 297 298 299 300 301 302 303 304
		return false;
	}

	dma_sync_single_for_cpu(&efx->pci_dev->dev, rx_buf->dma_addr,
				rx_buf->len, DMA_FROM_DEVICE);

	/* Save the rx prefix. */
	EFX_WARN_ON_PARANOID(efx->rx_prefix_size > EFX_MAX_RX_PREFIX_SIZE);
	memcpy(rx_prefix, *ehp - efx->rx_prefix_size,
	       efx->rx_prefix_size);

	xdp.data = *ehp;
305
	xdp.data_hard_start = xdp.data - EFX_XDP_HEADROOM;
306 307 308 309 310

	/* No support yet for XDP metadata */
	xdp_set_data_meta_invalid(&xdp);
	xdp.data_end = xdp.data + rx_buf->len;
	xdp.rxq = &rx_queue->xdp_rxq_info;
J
Jesper Dangaard Brouer 已提交
311
	xdp.frame_sz = efx->rx_page_buf_step;
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330

	xdp_act = bpf_prog_run_xdp(xdp_prog, &xdp);
	rcu_read_unlock();

	offset = (u8 *)xdp.data - *ehp;

	switch (xdp_act) {
	case XDP_PASS:
		/* Fix up rx prefix. */
		if (offset) {
			*ehp += offset;
			rx_buf->page_offset += offset;
			rx_buf->len -= offset;
			memcpy(*ehp - efx->rx_prefix_size, rx_prefix,
			       efx->rx_prefix_size);
		}
		break;

	case XDP_TX:
331
		/* Buffer ownership passes to tx on success. */
332
		xdpf = xdp_convert_buff_to_frame(&xdp);
333 334 335 336 337 338
		err = efx_xdp_tx_buffers(efx, 1, &xdpf, true);
		if (unlikely(err != 1)) {
			efx_free_rx_buffers(rx_queue, rx_buf, 1);
			if (net_ratelimit())
				netif_err(efx, rx_err, efx->net_dev,
					  "XDP TX failed (%d)\n", err);
339
			channel->n_rx_xdp_bad_drops++;
340
			trace_xdp_exception(efx->net_dev, xdp_prog, xdp_act);
341 342
		} else {
			channel->n_rx_xdp_tx++;
343 344
		}
		break;
345 346 347 348 349 350 351 352

	case XDP_REDIRECT:
		err = xdp_do_redirect(efx->net_dev, &xdp, xdp_prog);
		if (unlikely(err)) {
			efx_free_rx_buffers(rx_queue, rx_buf, 1);
			if (net_ratelimit())
				netif_err(efx, rx_err, efx->net_dev,
					  "XDP redirect failed (%d)\n", err);
353
			channel->n_rx_xdp_bad_drops++;
354
			trace_xdp_exception(efx->net_dev, xdp_prog, xdp_act);
355 356
		} else {
			channel->n_rx_xdp_redirect++;
357 358 359 360 361 362
		}
		break;

	default:
		bpf_warn_invalid_xdp_action(xdp_act);
		efx_free_rx_buffers(rx_queue, rx_buf, 1);
363
		channel->n_rx_xdp_bad_drops++;
364
		trace_xdp_exception(efx->net_dev, xdp_prog, xdp_act);
365 366 367 368 369 370 371
		break;

	case XDP_ABORTED:
		trace_xdp_exception(efx->net_dev, xdp_prog, xdp_act);
		/* Fall through */
	case XDP_DROP:
		efx_free_rx_buffers(rx_queue, rx_buf, 1);
372
		channel->n_rx_xdp_drops++;
373 374 375 376 377 378
		break;
	}

	return xdp_act == XDP_PASS;
}

379
/* Handle a received packet.  Second half: Touches packet payload. */
380
void __efx_rx_packet(struct efx_channel *channel)
381 382
{
	struct efx_nic *efx = channel->efx;
383 384
	struct efx_rx_buffer *rx_buf =
		efx_rx_buffer(&channel->rx_queue, channel->rx_pkt_index);
385
	u8 *eh = efx_rx_buf_va(rx_buf);
386

387 388 389 390 391 392 393
	/* Read length from the prefix if necessary.  This already
	 * excludes the length of the prefix itself.
	 */
	if (rx_buf->flags & EFX_RX_PKT_PREFIX_LEN)
		rx_buf->len = le16_to_cpup((__le16 *)
					   (eh + efx->rx_packet_len_offset));

394 395 396 397
	/* If we're in loopback test, then pass the packet directly to the
	 * loopback layer, and free the rx_buf here
	 */
	if (unlikely(efx->loopback_selftest)) {
398 399
		struct efx_rx_queue *rx_queue;

400
		efx_loopback_rx_packet(efx, eh, rx_buf->len);
401 402 403
		rx_queue = efx_channel_get_rx_queue(channel);
		efx_free_rx_buffers(rx_queue, rx_buf,
				    channel->rx_pkt_n_frags);
404
		goto out;
405 406
	}

407 408 409
	if (!efx_do_xdp(efx, channel, rx_buf, &eh))
		goto out;

410
	if (unlikely(!(efx->net_dev->features & NETIF_F_RXCSUM)))
411
		rx_buf->flags &= ~EFX_RX_PKT_CSUMMED;
412

413
	if ((rx_buf->flags & EFX_RX_PKT_TCP) && !channel->type->receive_skb)
414
		efx_rx_packet_gro(channel, rx_buf, channel->rx_pkt_n_frags, eh);
415
	else
416 417 418
		efx_rx_deliver(channel, eh, rx_buf, channel->rx_pkt_n_frags);
out:
	channel->rx_pkt_n_frags = 0;
419
}