Skip to content

Commit 2d1b138

Browse files
committed
Merge branch 'Handle-multiple-received-packets-at-each-stage'
Edward Cree says: ==================== Handle multiple received packets at each stage This patch series adds the capability for the network stack to receive a list of packets and process them as a unit, rather than handling each packet singly in sequence. This is done by factoring out the existing datapath code at each layer and wrapping it in list handling code. The motivation for this change is twofold: * Instruction cache locality. Currently, running the entire network stack receive path on a packet involves more code than will fit in the lowest-level icache, meaning that when the next packet is handled, the code has to be reloaded from more distant caches. By handling packets in "row-major order", we ensure that the code at each layer is hot for most of the list. (There is a corresponding downside in _data_ cache locality, since we are now touching every packet at every layer, but in practice there is easily enough room in dcache to hold one cacheline of each of the 64 packets in a NAPI poll.) * Reduction of indirect calls. Owing to Spectre mitigations, indirect function calls are now more expensive than ever; they are also heavily used in the network stack's architecture (see [1]). By replacing 64 indirect calls to the next-layer per-packet function with a single indirect call to the next-layer list function, we can save CPU cycles. Drivers pass an SKB list to the stack at the end of the NAPI poll; this gives a natural batch size (the NAPI poll weight) and avoids waiting at the software level for further packets to make a larger batch (which would add latency). It also means that the batch size is automatically tuned by the existing interrupt moderation mechanism. The stack then runs each layer of processing over all the packets in the list before proceeding to the next layer. Where the 'next layer' (or the context in which it must run) differs among the packets, the stack splits the list; this 'late demux' means that packets which differ only in later headers (e.g. same L2/L3 but different L4) can traverse the early part of the stack together. Also, where the next layer is not (yet) list-aware, the stack can revert to calling the rest of the stack in a loop; this allows gradual/creeping listification, with no 'flag day' patch needed to listify everything. Patches 1-2 simply place received packets on a list during the event processing loop on the sfc EF10 architecture, then call the normal stack for each packet singly at the end of the NAPI poll. (Analogues of patch #2 for other NIC drivers should be fairly straightforward.) Patches 3-9 extend the list processing as far as the IP receive handler. Patches 1-2 alone give about a 10% improvement in packet rate in the baseline test; adding patches 3-9 raises this to around 25%. Performance measurements were made with NetPerf UDP_STREAM, using 1-byte packets and a single core to handle interrupts on the RX side; this was in order to measure as simply as possible the packet rate handled by a single core. Figures are in Mbit/s; divide by 8 to obtain Mpps. The setup was tuned for maximum reproducibility, rather than raw performance. Full details and more results (both with and without retpolines) from a previous version of the patch series are presented in [2]. The baseline test uses four streams, and multiple RXQs all bound to a single CPU (the netperf binary is bound to a neighbouring CPU). These tests were run with retpolines. net-next: 6.91 Mb/s (datum) after 9: 8.46 Mb/s (+22.5%) Note however that these results are not robust; changes in the parameters of the test sometimes shrink the gain to single-digit percentages. For instance, when using only a single RXQ, only a 4% gain was seen. One test variation was the use of software filtering/firewall rules. Adding a single iptables rule (UDP port drop on a port range not matching the test traffic), thus making the netfilter hook have work to do, reduced baseline performance but showed a similar gain from the patches: net-next: 5.02 Mb/s (datum) after 9: 6.78 Mb/s (+35.1%) Similarly, testing with a set of TC flower filters (kindly supplied by Cong Wang) gave the following: net-next: 6.83 Mb/s (datum) after 9: 8.86 Mb/s (+29.7%) These data suggest that the batching approach remains effective in the presence of software switching rules, and perhaps even improves the performance of those rules by allowing them and their codepaths to stay in cache between packets. Changes from v3: * Fixed build error when CONFIG_NETFILTER=n (thanks kbuild). Changes from v2: * Used standard list handling (and skb->list) instead of the skb-queue functions (that use skb->next, skb->prev). - As part of this, changed from a "dequeue, process, enqueue" model to using list_for_each_safe, list_del, and (new) list_cut_before. * Altered __netif_receive_skb_core() changes in patch 6 as per Willem de Bruijn's suggestions (separate **ppt_prev from *pt_prev; renaming). * Removed patches to Generic XDP, since they were producing no benefit. I may revisit them later. * Removed RFC tags. Changes from v1: * Rebased across 2 years' net-next movement (surprisingly straightforward). - Added Generic XDP handling to netif_receive_skb_list_internal() - Dealt with changes to PFMEMALLOC setting APIs * General cleanup of code and comments. * Skipped function calls for empty lists at various points in the stack (patch #9). * Added listified Generic XDP handling (patches 10-12), though it doesn't seem to help (see above). * Extended testing to cover software firewalls / netfilter etc. [1] http://vger.kernel.org/netconf2018_files/DavidMiller_netconf2018.pdf [2] http://vger.kernel.org/netconf2018_files/EdwardCree_netconf2018.pdf ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents 2bdea15 + b9f463d commit 2d1b138

File tree

11 files changed

+360
-16
lines changed

11 files changed

+360
-16
lines changed

drivers/net/ethernet/sfc/efx.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,17 @@ static int efx_check_disabled(struct efx_nic *efx)
264264
static int efx_process_channel(struct efx_channel *channel, int budget)
265265
{
266266
struct efx_tx_queue *tx_queue;
267+
struct list_head rx_list;
267268
int spent;
268269

269270
if (unlikely(!channel->enabled))
270271
return 0;
271272

273+
/* Prepare the batch receive list */
274+
EFX_WARN_ON_PARANOID(channel->rx_list != NULL);
275+
INIT_LIST_HEAD(&rx_list);
276+
channel->rx_list = &rx_list;
277+
272278
efx_for_each_channel_tx_queue(tx_queue, channel) {
273279
tx_queue->pkts_compl = 0;
274280
tx_queue->bytes_compl = 0;
@@ -291,6 +297,10 @@ static int efx_process_channel(struct efx_channel *channel, int budget)
291297
}
292298
}
293299

300+
/* Receive any packets we queued up */
301+
netif_receive_skb_list(channel->rx_list);
302+
channel->rx_list = NULL;
303+
294304
return spent;
295305
}
296306

@@ -555,6 +565,8 @@ static int efx_probe_channel(struct efx_channel *channel)
555565
goto fail;
556566
}
557567

568+
channel->rx_list = NULL;
569+
558570
return 0;
559571

560572
fail:

drivers/net/ethernet/sfc/net_driver.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ enum efx_sync_events_state {
448448
* __efx_rx_packet(), or zero if there is none
449449
* @rx_pkt_index: Ring index of first buffer for next packet to be delivered
450450
* by __efx_rx_packet(), if @rx_pkt_n_frags != 0
451+
* @rx_list: list of SKBs from current RX, awaiting processing
451452
* @rx_queue: RX queue for this channel
452453
* @tx_queue: TX queues for this channel
453454
* @sync_events_state: Current state of sync events on this channel
@@ -500,6 +501,8 @@ struct efx_channel {
500501
unsigned int rx_pkt_n_frags;
501502
unsigned int rx_pkt_index;
502503

504+
struct list_head *rx_list;
505+
503506
struct efx_rx_queue rx_queue;
504507
struct efx_tx_queue tx_queue[EFX_TXQ_TYPES];
505508

drivers/net/ethernet/sfc/rx.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,12 @@ static void efx_rx_deliver(struct efx_channel *channel, u8 *eh,
634634
return;
635635

636636
/* Pass the packet up */
637-
netif_receive_skb(skb);
637+
if (channel->rx_list != NULL)
638+
/* Add to list, will pass up later */
639+
list_add_tail(&skb->list, channel->rx_list);
640+
else
641+
/* No list, so pass it up now */
642+
netif_receive_skb(skb);
638643
}
639644

640645
/* Handle a received packet. Second half: Touches packet payload. */

include/linux/list.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,36 @@ static inline void list_cut_position(struct list_head *list,
285285
__list_cut_position(list, head, entry);
286286
}
287287

288+
/**
289+
* list_cut_before - cut a list into two, before given entry
290+
* @list: a new list to add all removed entries
291+
* @head: a list with entries
292+
* @entry: an entry within head, could be the head itself
293+
*
294+
* This helper moves the initial part of @head, up to but
295+
* excluding @entry, from @head to @list. You should pass
296+
* in @entry an element you know is on @head. @list should
297+
* be an empty list or a list you do not care about losing
298+
* its data.
299+
* If @entry == @head, all entries on @head are moved to
300+
* @list.
301+
*/
302+
static inline void list_cut_before(struct list_head *list,
303+
struct list_head *head,
304+
struct list_head *entry)
305+
{
306+
if (head->next == entry) {
307+
INIT_LIST_HEAD(list);
308+
return;
309+
}
310+
list->next = head->next;
311+
list->next->prev = list;
312+
list->prev = entry->prev;
313+
list->prev->next = list;
314+
head->next = entry;
315+
entry->prev = head;
316+
}
317+
288318
static inline void __list_splice(const struct list_head *list,
289319
struct list_head *prev,
290320
struct list_head *next)

include/linux/netdevice.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2297,6 +2297,9 @@ struct packet_type {
22972297
struct net_device *,
22982298
struct packet_type *,
22992299
struct net_device *);
2300+
void (*list_func) (struct list_head *,
2301+
struct packet_type *,
2302+
struct net_device *);
23002303
bool (*id_match)(struct packet_type *ptype,
23012304
struct sock *sk);
23022305
void *af_packet_priv;
@@ -3477,6 +3480,7 @@ int netif_rx(struct sk_buff *skb);
34773480
int netif_rx_ni(struct sk_buff *skb);
34783481
int netif_receive_skb(struct sk_buff *skb);
34793482
int netif_receive_skb_core(struct sk_buff *skb);
3483+
void netif_receive_skb_list(struct list_head *head);
34803484
gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb);
34813485
void napi_gro_flush(struct napi_struct *napi, bool flush_old);
34823486
struct sk_buff *napi_get_frags(struct napi_struct *napi);

include/linux/netfilter.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,20 @@ NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, struct
288288
return ret;
289289
}
290290

291+
static inline void
292+
NF_HOOK_LIST(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
293+
struct list_head *head, struct net_device *in, struct net_device *out,
294+
int (*okfn)(struct net *, struct sock *, struct sk_buff *))
295+
{
296+
struct sk_buff *skb, *next;
297+
298+
list_for_each_entry_safe(skb, next, head, list) {
299+
int ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn);
300+
if (ret != 1)
301+
list_del(&skb->list);
302+
}
303+
}
304+
291305
/* Call setsockopt() */
292306
int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
293307
unsigned int len);
@@ -369,6 +383,14 @@ NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
369383
return okfn(net, sk, skb);
370384
}
371385

386+
static inline void
387+
NF_HOOK_LIST(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
388+
struct list_head *head, struct net_device *in, struct net_device *out,
389+
int (*okfn)(struct net *, struct sock *, struct sk_buff *))
390+
{
391+
/* nothing to do */
392+
}
393+
372394
static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,
373395
struct sock *sk, struct sk_buff *skb,
374396
struct net_device *indev, struct net_device *outdev,

include/net/ip.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ int ip_build_and_send_pkt(struct sk_buff *skb, const struct sock *sk,
138138
struct ip_options_rcu *opt);
139139
int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt,
140140
struct net_device *orig_dev);
141+
void ip_list_rcv(struct list_head *head, struct packet_type *pt,
142+
struct net_device *orig_dev);
141143
int ip_local_deliver(struct sk_buff *skb);
142144
int ip_mr_input(struct sk_buff *skb);
143145
int ip_output(struct net *net, struct sock *sk, struct sk_buff *skb);

include/trace/events/net.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,13 @@ DEFINE_EVENT(net_dev_rx_verbose_template, netif_receive_skb_entry,
223223
TP_ARGS(skb)
224224
);
225225

226+
DEFINE_EVENT(net_dev_rx_verbose_template, netif_receive_skb_list_entry,
227+
228+
TP_PROTO(const struct sk_buff *skb),
229+
230+
TP_ARGS(skb)
231+
);
232+
226233
DEFINE_EVENT(net_dev_rx_verbose_template, netif_rx_entry,
227234

228235
TP_PROTO(const struct sk_buff *skb),

0 commit comments

Comments
 (0)