blob: 6e7265f63c04097219d5c9b50e510778ff41b352 [file] [log] [blame]
Björn Töpeldac091492018-05-18 14:00:21 +02001/* SPDX-License-Identifier: GPL-2.0 */
2/* AF_XDP internal functions
Björn Töpelc0c77d82018-05-02 13:01:23 +02003 * Copyright(c) 2018 Intel Corporation.
Björn Töpelc0c77d82018-05-02 13:01:23 +02004 */
5
6#ifndef _LINUX_XDP_SOCK_H
7#define _LINUX_XDP_SOCK_H
8
Björn Töpele61e62b92018-06-04 14:05:51 +02009#include <linux/workqueue.h>
10#include <linux/if_xdp.h>
Björn Töpelc0c77d82018-05-02 13:01:23 +020011#include <linux/mutex.h>
Magnus Karlssonac98d8a2018-06-04 14:05:57 +020012#include <linux/spinlock.h>
Björn Töpele61e62b92018-06-04 14:05:51 +020013#include <linux/mm.h>
Björn Töpelc0c77d82018-05-02 13:01:23 +020014#include <net/sock.h>
15
Björn Töpelb9b6b682018-05-02 13:01:25 +020016struct net_device;
17struct xsk_queue;
Magnus Karlssona71506a2020-05-20 21:20:51 +020018struct xdp_buff;
Björn Töpele61e62b92018-06-04 14:05:51 +020019
Björn Töpel8aef7342018-06-04 14:05:52 +020020struct xdp_umem_page {
21 void *addr;
Björn Töpel173d3ad2018-06-04 14:05:55 +020022 dma_addr_t dma;
Björn Töpel8aef7342018-06-04 14:05:52 +020023};
24
Jakub Kicinskif5bd9132018-09-07 10:18:46 +020025struct xdp_umem_fq_reuse {
26 u32 nentries;
27 u32 length;
28 u64 handles[];
29};
30
Björn Töpele61e62b92018-06-04 14:05:51 +020031struct xdp_umem {
32 struct xsk_queue *fq;
33 struct xsk_queue *cq;
Björn Töpel2b434702020-05-20 21:20:53 +020034 struct xsk_buff_pool *pool;
Björn Töpel8aef7342018-06-04 14:05:52 +020035 struct xdp_umem_page *pages;
Magnus Karlsson93ee30f2018-08-31 13:40:02 +020036 u64 chunk_mask;
37 u64 size;
Björn Töpele61e62b92018-06-04 14:05:51 +020038 u32 headroom;
39 u32 chunk_size_nohr;
Björn Töpel2b434702020-05-20 21:20:53 +020040 u32 chunk_size;
Björn Töpele61e62b92018-06-04 14:05:51 +020041 struct user_struct *user;
Björn Töpele61e62b92018-06-04 14:05:51 +020042 refcount_t users;
43 struct work_struct work;
Björn Töpel8aef7342018-06-04 14:05:52 +020044 struct page **pgs;
Björn Töpele61e62b92018-06-04 14:05:51 +020045 u32 npgs;
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020046 u16 queue_id;
47 u8 need_wakeup;
48 u8 flags;
Björn Töpel50e74c02019-01-24 19:59:38 +010049 int id;
Björn Töpel173d3ad2018-06-04 14:05:55 +020050 struct net_device *dev;
Jakub Kicinskif5bd9132018-09-07 10:18:46 +020051 struct xdp_umem_fq_reuse *fq_reuse;
Björn Töpel173d3ad2018-06-04 14:05:55 +020052 bool zc;
Magnus Karlssone4e5aef2020-05-04 15:33:51 +020053 spinlock_t xsk_tx_list_lock;
54 struct list_head xsk_tx_list;
Björn Töpele61e62b92018-06-04 14:05:51 +020055};
Björn Töpelc0c77d82018-05-02 13:01:23 +020056
Björn Töpeld8179912019-11-01 12:03:46 +010057struct xsk_map {
58 struct bpf_map map;
Björn Töpeld8179912019-11-01 12:03:46 +010059 spinlock_t lock; /* Synchronize map updates */
60 struct xdp_sock *xsk_map[];
61};
62
Björn Töpelc0c77d82018-05-02 13:01:23 +020063struct xdp_sock {
64 /* struct sock must be the first member of struct xdp_sock */
65 struct sock sk;
Björn Töpelb9b6b682018-05-02 13:01:25 +020066 struct xsk_queue *rx;
67 struct net_device *dev;
Björn Töpelc0c77d82018-05-02 13:01:23 +020068 struct xdp_umem *umem;
Björn Töpelfbfc504a2018-05-02 13:01:28 +020069 struct list_head flush_node;
Magnus Karlsson965a9902018-05-02 13:01:26 +020070 u16 queue_id;
Magnus Karlssonac98d8a2018-06-04 14:05:57 +020071 bool zc;
Ilya Maximets455302d2019-06-28 11:04:07 +030072 enum {
73 XSK_READY = 0,
74 XSK_BOUND,
75 XSK_UNBOUND,
76 } state;
Björn Töpelc0c77d82018-05-02 13:01:23 +020077 /* Protects multiple processes in the control path */
78 struct mutex mutex;
Jonathan Lemonfada7fd2019-06-06 13:59:40 -070079 struct xsk_queue *tx ____cacheline_aligned_in_smp;
80 struct list_head list;
Magnus Karlssona9744f72018-06-29 09:48:20 +020081 /* Mutual exclusion of NAPI TX thread and sendmsg error paths
82 * in the SKB destructor callback.
83 */
84 spinlock_t tx_completion_lock;
Ilya Maximetsbf0bdd12019-07-03 15:09:16 +030085 /* Protects generic receive. */
86 spinlock_t rx_lock;
Björn Töpelc4971762018-05-02 13:01:27 +020087 u64 rx_dropped;
Björn Töpel0402acd2019-08-15 11:30:13 +020088 struct list_head map_list;
89 /* Protects map_list */
90 spinlock_t map_list_lock;
Björn Töpelc0c77d82018-05-02 13:01:23 +020091};
92
Björn Töpelc4971762018-05-02 13:01:27 +020093#ifdef CONFIG_XDP_SOCKETS
Björn Töpel90254032018-08-28 14:44:27 +020094
Magnus Karlssona71506a2020-05-20 21:20:51 +020095int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp);
Björn Töpele312b9e2019-12-19 07:10:02 +010096int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp);
97void __xsk_map_flush(void);
Björn Töpeld8179912019-11-01 12:03:46 +010098
99static inline struct xdp_sock *__xsk_map_lookup_elem(struct bpf_map *map,
100 u32 key)
101{
102 struct xsk_map *m = container_of(map, struct xsk_map, map);
103 struct xdp_sock *xs;
104
105 if (key >= map->max_entries)
106 return NULL;
107
108 xs = READ_ONCE(m->xsk_map[key]);
109 return xs;
110}
Björn Töpel0402acd2019-08-15 11:30:13 +0200111
Kevin Laatzc05cd362019-08-27 02:25:22 +0000112static inline u64 xsk_umem_extract_addr(u64 addr)
113{
114 return addr & XSK_UNALIGNED_BUF_ADDR_MASK;
115}
116
117static inline u64 xsk_umem_extract_offset(u64 addr)
118{
119 return addr >> XSK_UNALIGNED_BUF_OFFSET_SHIFT;
120}
121
122static inline u64 xsk_umem_add_offset_to_addr(u64 addr)
123{
124 return xsk_umem_extract_addr(addr) + xsk_umem_extract_offset(addr);
125}
126
Björn Töpelc4971762018-05-02 13:01:27 +0200127#else
Magnus Karlssona71506a2020-05-20 21:20:51 +0200128
Björn Töpelc4971762018-05-02 13:01:27 +0200129static inline int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
130{
131 return -ENOTSUPP;
132}
133
Magnus Karlssona71506a2020-05-20 21:20:51 +0200134static inline int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp)
Maxim Mikityanskiyd57d7642019-06-26 17:35:24 +0300135{
Magnus Karlssona71506a2020-05-20 21:20:51 +0200136 return -EOPNOTSUPP;
Maxim Mikityanskiyd57d7642019-06-26 17:35:24 +0300137}
138
Magnus Karlssona71506a2020-05-20 21:20:51 +0200139static inline void __xsk_map_flush(void)
Björn Töpel90254032018-08-28 14:44:27 +0200140{
141}
142
Magnus Karlssona71506a2020-05-20 21:20:51 +0200143static inline struct xdp_sock *__xsk_map_lookup_elem(struct bpf_map *map,
144 u32 key)
Jakub Kicinski1661d342018-10-01 14:51:36 +0200145{
146 return NULL;
147}
148
Kevin Laatzc05cd362019-08-27 02:25:22 +0000149static inline u64 xsk_umem_extract_addr(u64 addr)
150{
151 return 0;
152}
153
154static inline u64 xsk_umem_extract_offset(u64 addr)
155{
156 return 0;
157}
158
159static inline u64 xsk_umem_add_offset_to_addr(u64 addr)
160{
161 return 0;
162}
163
Björn Töpelc4971762018-05-02 13:01:27 +0200164#endif /* CONFIG_XDP_SOCKETS */
165
Björn Töpelc0c77d82018-05-02 13:01:23 +0200166#endif /* _LINUX_XDP_SOCK_H */