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
efx_rx_packet__check_len(struct efx_rx_queue * rx_queue,struct efx_rx_buffer * rx_buf,int len)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 */
efx_rx_mk_skb(struct efx_channel * channel,struct efx_rx_buffer * rx_buf,unsigned int n_frags,u8 * eh,int hdr_len)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_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
98 rx_buf->page, rx_buf->page_offset,
99 rx_buf->len);
100 rx_buf->page = NULL;
101 skb->len += rx_buf->len;
102 skb->data_len += rx_buf->len;
103 if (skb_shinfo(skb)->nr_frags == n_frags)
104 break;
105
106 rx_buf = efx_rx_buf_next(&channel->rx_queue, rx_buf);
107 }
108 } else {
109 __free_pages(rx_buf->page, efx->rx_buffer_order);
110 rx_buf->page = NULL;
111 n_frags = 0;
112 }
113
114 skb->truesize += n_frags * efx->rx_buffer_truesize;
115
116 /* Move past the ethernet header */
117 skb->protocol = eth_type_trans(skb, efx->net_dev);
118
119 skb_mark_napi_id(skb, &channel->napi_str);
120
121 return skb;
122 }
123
efx_rx_packet(struct efx_rx_queue * rx_queue,unsigned int index,unsigned int n_frags,unsigned int len,u16 flags)124 void efx_rx_packet(struct efx_rx_queue *rx_queue, unsigned int index,
125 unsigned int n_frags, unsigned int len, u16 flags)
126 {
127 struct efx_nic *efx = rx_queue->efx;
128 struct efx_channel *channel = efx_rx_queue_channel(rx_queue);
129 struct efx_rx_buffer *rx_buf;
130
131 rx_queue->rx_packets++;
132
133 rx_buf = efx_rx_buffer(rx_queue, index);
134 rx_buf->flags |= flags;
135
136 /* Validate the number of fragments and completed length */
137 if (n_frags == 1) {
138 if (!(flags & EFX_RX_PKT_PREFIX_LEN))
139 efx_rx_packet__check_len(rx_queue, rx_buf, len);
140 } else if (unlikely(n_frags > EFX_RX_MAX_FRAGS) ||
141 unlikely(len <= (n_frags - 1) * efx->rx_dma_len) ||
142 unlikely(len > n_frags * efx->rx_dma_len) ||
143 unlikely(!efx->rx_scatter)) {
144 /* If this isn't an explicit discard request, either
145 * the hardware or the driver is broken.
146 */
147 WARN_ON(!(len == 0 && rx_buf->flags & EFX_RX_PKT_DISCARD));
148 rx_buf->flags |= EFX_RX_PKT_DISCARD;
149 }
150
151 netif_vdbg(efx, rx_status, efx->net_dev,
152 "RX queue %d received ids %x-%x len %d %s%s\n",
153 efx_rx_queue_index(rx_queue), index,
154 (index + n_frags - 1) & rx_queue->ptr_mask, len,
155 (rx_buf->flags & EFX_RX_PKT_CSUMMED) ? " [SUMMED]" : "",
156 (rx_buf->flags & EFX_RX_PKT_DISCARD) ? " [DISCARD]" : "");
157
158 /* Discard packet, if instructed to do so. Process the
159 * previous receive first.
160 */
161 if (unlikely(rx_buf->flags & EFX_RX_PKT_DISCARD)) {
162 efx_rx_flush_packet(channel);
163 efx_discard_rx_packet(channel, rx_buf, n_frags);
164 return;
165 }
166
167 if (n_frags == 1 && !(flags & EFX_RX_PKT_PREFIX_LEN))
168 rx_buf->len = len;
169
170 /* Release and/or sync the DMA mapping - assumes all RX buffers
171 * consumed in-order per RX queue.
172 */
173 efx_sync_rx_buffer(efx, rx_buf, rx_buf->len);
174
175 /* Prefetch nice and early so data will (hopefully) be in cache by
176 * the time we look at it.
177 */
178 prefetch(efx_rx_buf_va(rx_buf));
179
180 rx_buf->page_offset += efx->rx_prefix_size;
181 rx_buf->len -= efx->rx_prefix_size;
182
183 if (n_frags > 1) {
184 /* Release/sync DMA mapping for additional fragments.
185 * Fix length for last fragment.
186 */
187 unsigned int tail_frags = n_frags - 1;
188
189 for (;;) {
190 rx_buf = efx_rx_buf_next(rx_queue, rx_buf);
191 if (--tail_frags == 0)
192 break;
193 efx_sync_rx_buffer(efx, rx_buf, efx->rx_dma_len);
194 }
195 rx_buf->len = len - (n_frags - 1) * efx->rx_dma_len;
196 efx_sync_rx_buffer(efx, rx_buf, rx_buf->len);
197 }
198
199 /* All fragments have been DMA-synced, so recycle pages. */
200 rx_buf = efx_rx_buffer(rx_queue, index);
201 efx_recycle_rx_pages(channel, rx_buf, n_frags);
202
203 /* Pipeline receives so that we give time for packet headers to be
204 * prefetched into cache.
205 */
206 efx_rx_flush_packet(channel);
207 channel->rx_pkt_n_frags = n_frags;
208 channel->rx_pkt_index = index;
209 }
210
efx_rx_deliver(struct efx_channel * channel,u8 * eh,struct efx_rx_buffer * rx_buf,unsigned int n_frags)211 static void efx_rx_deliver(struct efx_channel *channel, u8 *eh,
212 struct efx_rx_buffer *rx_buf,
213 unsigned int n_frags)
214 {
215 struct sk_buff *skb;
216 u16 hdr_len = min_t(u16, rx_buf->len, EFX_SKB_HEADERS);
217
218 skb = efx_rx_mk_skb(channel, rx_buf, n_frags, eh, hdr_len);
219 if (unlikely(skb == NULL)) {
220 struct efx_rx_queue *rx_queue;
221
222 rx_queue = efx_channel_get_rx_queue(channel);
223 efx_free_rx_buffers(rx_queue, rx_buf, n_frags);
224 return;
225 }
226 skb_record_rx_queue(skb, channel->rx_queue.core_index);
227
228 /* Set the SKB flags */
229 skb_checksum_none_assert(skb);
230 if (likely(rx_buf->flags & EFX_RX_PKT_CSUMMED)) {
231 skb->ip_summed = CHECKSUM_UNNECESSARY;
232 skb->csum_level = !!(rx_buf->flags & EFX_RX_PKT_CSUM_LEVEL);
233 }
234
235 efx_rx_skb_attach_timestamp(channel, skb);
236
237 if (channel->type->receive_skb)
238 if (channel->type->receive_skb(channel, skb))
239 return;
240
241 /* Pass the packet up */
242 if (channel->rx_list != NULL)
243 /* Add to list, will pass up later */
244 list_add_tail(&skb->list, channel->rx_list);
245 else
246 /* No list, so pass it up now */
247 netif_receive_skb(skb);
248 }
249
250 /** efx_do_xdp: perform XDP processing on a received packet
251 *
252 * Returns true if packet should still be delivered.
253 */
efx_do_xdp(struct efx_nic * efx,struct efx_channel * channel,struct efx_rx_buffer * rx_buf,u8 ** ehp)254 static bool efx_do_xdp(struct efx_nic *efx, struct efx_channel *channel,
255 struct efx_rx_buffer *rx_buf, u8 **ehp)
256 {
257 u8 rx_prefix[EFX_MAX_RX_PREFIX_SIZE];
258 struct efx_rx_queue *rx_queue;
259 struct bpf_prog *xdp_prog;
260 struct xdp_frame *xdpf;
261 struct xdp_buff xdp;
262 u32 xdp_act;
263 s16 offset;
264 int err;
265
266 rcu_read_lock();
267 xdp_prog = rcu_dereference(efx->xdp_prog);
268 if (!xdp_prog) {
269 rcu_read_unlock();
270 return true;
271 }
272
273 rx_queue = efx_channel_get_rx_queue(channel);
274
275 if (unlikely(channel->rx_pkt_n_frags > 1)) {
276 /* We can't do XDP on fragmented packets - drop. */
277 rcu_read_unlock();
278 efx_free_rx_buffers(rx_queue, rx_buf,
279 channel->rx_pkt_n_frags);
280 if (net_ratelimit())
281 netif_err(efx, rx_err, efx->net_dev,
282 "XDP is not possible with multiple receive fragments (%d)\n",
283 channel->rx_pkt_n_frags);
284 channel->n_rx_xdp_bad_drops++;
285 return false;
286 }
287
288 dma_sync_single_for_cpu(&efx->pci_dev->dev, rx_buf->dma_addr,
289 rx_buf->len, DMA_FROM_DEVICE);
290
291 /* Save the rx prefix. */
292 EFX_WARN_ON_PARANOID(efx->rx_prefix_size > EFX_MAX_RX_PREFIX_SIZE);
293 memcpy(rx_prefix, *ehp - efx->rx_prefix_size,
294 efx->rx_prefix_size);
295
296 xdp.data = *ehp;
297 xdp.data_hard_start = xdp.data - EFX_XDP_HEADROOM;
298
299 /* No support yet for XDP metadata */
300 xdp_set_data_meta_invalid(&xdp);
301 xdp.data_end = xdp.data + rx_buf->len;
302 xdp.rxq = &rx_queue->xdp_rxq_info;
303 xdp.frame_sz = efx->rx_page_buf_step;
304
305 xdp_act = bpf_prog_run_xdp(xdp_prog, &xdp);
306 rcu_read_unlock();
307
308 offset = (u8 *)xdp.data - *ehp;
309
310 switch (xdp_act) {
311 case XDP_PASS:
312 /* Fix up rx prefix. */
313 if (offset) {
314 *ehp += offset;
315 rx_buf->page_offset += offset;
316 rx_buf->len -= offset;
317 memcpy(*ehp - efx->rx_prefix_size, rx_prefix,
318 efx->rx_prefix_size);
319 }
320 break;
321
322 case XDP_TX:
323 /* Buffer ownership passes to tx on success. */
324 xdpf = xdp_convert_buff_to_frame(&xdp);
325 err = efx_xdp_tx_buffers(efx, 1, &xdpf, true);
326 if (unlikely(err != 1)) {
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 TX 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_tx++;
335 }
336 break;
337
338 case XDP_REDIRECT:
339 err = xdp_do_redirect(efx->net_dev, &xdp, xdp_prog);
340 if (unlikely(err)) {
341 efx_free_rx_buffers(rx_queue, rx_buf, 1);
342 if (net_ratelimit())
343 netif_err(efx, rx_err, efx->net_dev,
344 "XDP redirect failed (%d)\n", err);
345 channel->n_rx_xdp_bad_drops++;
346 trace_xdp_exception(efx->net_dev, xdp_prog, xdp_act);
347 } else {
348 channel->n_rx_xdp_redirect++;
349 }
350 break;
351
352 default:
353 bpf_warn_invalid_xdp_action(xdp_act);
354 efx_free_rx_buffers(rx_queue, rx_buf, 1);
355 channel->n_rx_xdp_bad_drops++;
356 trace_xdp_exception(efx->net_dev, xdp_prog, xdp_act);
357 break;
358
359 case XDP_ABORTED:
360 trace_xdp_exception(efx->net_dev, xdp_prog, xdp_act);
361 fallthrough;
362 case XDP_DROP:
363 efx_free_rx_buffers(rx_queue, rx_buf, 1);
364 channel->n_rx_xdp_drops++;
365 break;
366 }
367
368 return xdp_act == XDP_PASS;
369 }
370
371 /* Handle a received packet. Second half: Touches packet payload. */
__efx_rx_packet(struct efx_channel * channel)372 void __efx_rx_packet(struct efx_channel *channel)
373 {
374 struct efx_nic *efx = channel->efx;
375 struct efx_rx_buffer *rx_buf =
376 efx_rx_buffer(&channel->rx_queue, channel->rx_pkt_index);
377 u8 *eh = efx_rx_buf_va(rx_buf);
378
379 /* Read length from the prefix if necessary. This already
380 * excludes the length of the prefix itself.
381 */
382 if (rx_buf->flags & EFX_RX_PKT_PREFIX_LEN)
383 rx_buf->len = le16_to_cpup((__le16 *)
384 (eh + efx->rx_packet_len_offset));
385
386 /* If we're in loopback test, then pass the packet directly to the
387 * loopback layer, and free the rx_buf here
388 */
389 if (unlikely(efx->loopback_selftest)) {
390 struct efx_rx_queue *rx_queue;
391
392 efx_loopback_rx_packet(efx, eh, rx_buf->len);
393 rx_queue = efx_channel_get_rx_queue(channel);
394 efx_free_rx_buffers(rx_queue, rx_buf,
395 channel->rx_pkt_n_frags);
396 goto out;
397 }
398
399 if (!efx_do_xdp(efx, channel, rx_buf, &eh))
400 goto out;
401
402 if (unlikely(!(efx->net_dev->features & NETIF_F_RXCSUM)))
403 rx_buf->flags &= ~EFX_RX_PKT_CSUMMED;
404
405 if ((rx_buf->flags & EFX_RX_PKT_TCP) && !channel->type->receive_skb)
406 efx_rx_packet_gro(channel, rx_buf, channel->rx_pkt_n_frags, eh, 0);
407 else
408 efx_rx_deliver(channel, eh, rx_buf, channel->rx_pkt_n_frags);
409 out:
410 channel->rx_pkt_n_frags = 0;
411 }
412