blob: cc17bc957548257602a5d9a6cdd59bb704f4e278 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/* SPDX-License-Identifier: GPL-2.0 */
2/* AF_XDP internal functions
3 * Copyright(c) 2018 Intel Corporation.
4 */
5
6#ifndef _LINUX_XDP_SOCK_H
7#define _LINUX_XDP_SOCK_H
8
9#include <linux/workqueue.h>
10#include <linux/if_xdp.h>
11#include <linux/mutex.h>
12#include <linux/spinlock.h>
13#include <linux/mm.h>
14#include <net/sock.h>
15
16struct net_device;
17struct xsk_queue;
Olivier Deprez157378f2022-04-04 15:47:50 +020018struct xdp_buff;
David Brazdil0f672f62019-12-10 10:32:29 +000019
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000020struct xdp_umem {
Olivier Deprez157378f2022-04-04 15:47:50 +020021 void *addrs;
David Brazdil0f672f62019-12-10 10:32:29 +000022 u64 size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000023 u32 headroom;
Olivier Deprez157378f2022-04-04 15:47:50 +020024 u32 chunk_size;
25 u32 chunks;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000026 u32 npgs;
Olivier Deprez157378f2022-04-04 15:47:50 +020027 struct user_struct *user;
28 refcount_t users;
David Brazdil0f672f62019-12-10 10:32:29 +000029 u8 flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000030 bool zc;
Olivier Deprez157378f2022-04-04 15:47:50 +020031 struct page **pgs;
32 int id;
33 struct list_head xsk_dma_list;
34 struct work_struct work;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000035};
36
Olivier Deprez157378f2022-04-04 15:47:50 +020037struct xsk_map {
38 struct bpf_map map;
39 spinlock_t lock; /* Synchronize map updates */
40 struct xdp_sock *xsk_map[];
David Brazdil0f672f62019-12-10 10:32:29 +000041};
42
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000043struct xdp_sock {
44 /* struct sock must be the first member of struct xdp_sock */
45 struct sock sk;
Olivier Deprez157378f2022-04-04 15:47:50 +020046 struct xsk_queue *rx ____cacheline_aligned_in_smp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000047 struct net_device *dev;
48 struct xdp_umem *umem;
49 struct list_head flush_node;
Olivier Deprez157378f2022-04-04 15:47:50 +020050 struct xsk_buff_pool *pool;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000051 u16 queue_id;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000052 bool zc;
David Brazdil0f672f62019-12-10 10:32:29 +000053 enum {
54 XSK_READY = 0,
55 XSK_BOUND,
56 XSK_UNBOUND,
57 } state;
Olivier Deprez157378f2022-04-04 15:47:50 +020058
David Brazdil0f672f62019-12-10 10:32:29 +000059 struct xsk_queue *tx ____cacheline_aligned_in_smp;
Olivier Deprez157378f2022-04-04 15:47:50 +020060 struct list_head tx_list;
David Brazdil0f672f62019-12-10 10:32:29 +000061 /* Protects generic receive. */
62 spinlock_t rx_lock;
Olivier Deprez157378f2022-04-04 15:47:50 +020063
64 /* Statistics */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000065 u64 rx_dropped;
Olivier Deprez157378f2022-04-04 15:47:50 +020066 u64 rx_queue_full;
67
David Brazdil0f672f62019-12-10 10:32:29 +000068 struct list_head map_list;
69 /* Protects map_list */
70 spinlock_t map_list_lock;
Olivier Deprez157378f2022-04-04 15:47:50 +020071 /* Protects multiple processes in the control path */
72 struct mutex mutex;
73 struct xsk_queue *fq_tmp; /* Only as tmp storage before bind */
74 struct xsk_queue *cq_tmp; /* Only as tmp storage before bind */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000075};
76
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000077#ifdef CONFIG_XDP_SOCKETS
Olivier Deprez157378f2022-04-04 15:47:50 +020078
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000079int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp);
Olivier Deprez157378f2022-04-04 15:47:50 +020080int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp);
81void __xsk_map_flush(void);
David Brazdil0f672f62019-12-10 10:32:29 +000082
Olivier Deprez157378f2022-04-04 15:47:50 +020083static inline struct xdp_sock *__xsk_map_lookup_elem(struct bpf_map *map,
84 u32 key)
David Brazdil0f672f62019-12-10 10:32:29 +000085{
Olivier Deprez157378f2022-04-04 15:47:50 +020086 struct xsk_map *m = container_of(map, struct xsk_map, map);
87 struct xdp_sock *xs;
88
89 if (key >= map->max_entries)
90 return NULL;
91
92 xs = READ_ONCE(m->xsk_map[key]);
93 return xs;
David Brazdil0f672f62019-12-10 10:32:29 +000094}
95
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000096#else
Olivier Deprez157378f2022-04-04 15:47:50 +020097
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000098static inline int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
99{
100 return -ENOTSUPP;
101}
102
Olivier Deprez157378f2022-04-04 15:47:50 +0200103static inline int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000104{
Olivier Deprez157378f2022-04-04 15:47:50 +0200105 return -EOPNOTSUPP;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000106}
107
Olivier Deprez157378f2022-04-04 15:47:50 +0200108static inline void __xsk_map_flush(void)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000109{
110}
111
Olivier Deprez157378f2022-04-04 15:47:50 +0200112static inline struct xdp_sock *__xsk_map_lookup_elem(struct bpf_map *map,
113 u32 key)
David Brazdil0f672f62019-12-10 10:32:29 +0000114{
115 return NULL;
116}
117
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000118#endif /* CONFIG_XDP_SOCKETS */
119
120#endif /* _LINUX_XDP_SOCK_H */