Björn Töpel | dac09149 | 2018-05-18 14:00:21 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | /* AF_XDP internal functions |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 3 | * Copyright(c) 2018 Intel Corporation. |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #ifndef _LINUX_XDP_SOCK_H |
| 7 | #define _LINUX_XDP_SOCK_H |
| 8 | |
Björn Töpel | e61e62b9 | 2018-06-04 14:05:51 +0200 | [diff] [blame] | 9 | #include <linux/workqueue.h> |
| 10 | #include <linux/if_xdp.h> |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 11 | #include <linux/mutex.h> |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 12 | #include <linux/spinlock.h> |
Björn Töpel | e61e62b9 | 2018-06-04 14:05:51 +0200 | [diff] [blame] | 13 | #include <linux/mm.h> |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 14 | #include <net/sock.h> |
| 15 | |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 16 | struct net_device; |
| 17 | struct xsk_queue; |
Magnus Karlsson | a71506a | 2020-05-20 21:20:51 +0200 | [diff] [blame] | 18 | struct xdp_buff; |
Björn Töpel | e61e62b9 | 2018-06-04 14:05:51 +0200 | [diff] [blame] | 19 | |
Björn Töpel | e61e62b9 | 2018-06-04 14:05:51 +0200 | [diff] [blame] | 20 | struct xdp_umem { |
| 21 | struct xsk_queue *fq; |
| 22 | struct xsk_queue *cq; |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 23 | struct xsk_buff_pool *pool; |
Magnus Karlsson | 93ee30f | 2018-08-31 13:40:02 +0200 | [diff] [blame] | 24 | u64 size; |
Björn Töpel | e61e62b9 | 2018-06-04 14:05:51 +0200 | [diff] [blame] | 25 | u32 headroom; |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 26 | u32 chunk_size; |
Björn Töpel | e61e62b9 | 2018-06-04 14:05:51 +0200 | [diff] [blame] | 27 | struct user_struct *user; |
Björn Töpel | e61e62b9 | 2018-06-04 14:05:51 +0200 | [diff] [blame] | 28 | refcount_t users; |
| 29 | struct work_struct work; |
Björn Töpel | 8aef734 | 2018-06-04 14:05:52 +0200 | [diff] [blame] | 30 | struct page **pgs; |
Björn Töpel | e61e62b9 | 2018-06-04 14:05:51 +0200 | [diff] [blame] | 31 | u32 npgs; |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 32 | u16 queue_id; |
| 33 | u8 need_wakeup; |
| 34 | u8 flags; |
Björn Töpel | 50e74c0 | 2019-01-24 19:59:38 +0100 | [diff] [blame] | 35 | int id; |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 36 | struct net_device *dev; |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 37 | bool zc; |
Magnus Karlsson | e4e5aef | 2020-05-04 15:33:51 +0200 | [diff] [blame] | 38 | spinlock_t xsk_tx_list_lock; |
| 39 | struct list_head xsk_tx_list; |
Björn Töpel | e61e62b9 | 2018-06-04 14:05:51 +0200 | [diff] [blame] | 40 | }; |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 41 | |
Björn Töpel | d817991 | 2019-11-01 12:03:46 +0100 | [diff] [blame] | 42 | struct xsk_map { |
| 43 | struct bpf_map map; |
Björn Töpel | d817991 | 2019-11-01 12:03:46 +0100 | [diff] [blame] | 44 | spinlock_t lock; /* Synchronize map updates */ |
| 45 | struct xdp_sock *xsk_map[]; |
| 46 | }; |
| 47 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 48 | struct xdp_sock { |
| 49 | /* struct sock must be the first member of struct xdp_sock */ |
| 50 | struct sock sk; |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 51 | struct xsk_queue *rx; |
| 52 | struct net_device *dev; |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 53 | struct xdp_umem *umem; |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 54 | struct list_head flush_node; |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 55 | u16 queue_id; |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 56 | bool zc; |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 57 | enum { |
| 58 | XSK_READY = 0, |
| 59 | XSK_BOUND, |
| 60 | XSK_UNBOUND, |
| 61 | } state; |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 62 | /* Protects multiple processes in the control path */ |
| 63 | struct mutex mutex; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 64 | struct xsk_queue *tx ____cacheline_aligned_in_smp; |
| 65 | struct list_head list; |
Magnus Karlsson | a9744f7 | 2018-06-29 09:48:20 +0200 | [diff] [blame] | 66 | /* Mutual exclusion of NAPI TX thread and sendmsg error paths |
| 67 | * in the SKB destructor callback. |
| 68 | */ |
| 69 | spinlock_t tx_completion_lock; |
Ilya Maximets | bf0bdd1 | 2019-07-03 15:09:16 +0300 | [diff] [blame] | 70 | /* Protects generic receive. */ |
| 71 | spinlock_t rx_lock; |
Ciara Loftus | 8aa5a33 | 2020-07-08 07:28:33 +0000 | [diff] [blame] | 72 | |
| 73 | /* Statistics */ |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 74 | u64 rx_dropped; |
Ciara Loftus | 8aa5a33 | 2020-07-08 07:28:33 +0000 | [diff] [blame] | 75 | u64 rx_queue_full; |
| 76 | |
Björn Töpel | 0402acd | 2019-08-15 11:30:13 +0200 | [diff] [blame] | 77 | struct list_head map_list; |
| 78 | /* Protects map_list */ |
| 79 | spinlock_t map_list_lock; |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 80 | }; |
| 81 | |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 82 | #ifdef CONFIG_XDP_SOCKETS |
Björn Töpel | 9025403 | 2018-08-28 14:44:27 +0200 | [diff] [blame] | 83 | |
Magnus Karlsson | a71506a | 2020-05-20 21:20:51 +0200 | [diff] [blame] | 84 | int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp); |
Björn Töpel | e312b9e | 2019-12-19 07:10:02 +0100 | [diff] [blame] | 85 | int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp); |
| 86 | void __xsk_map_flush(void); |
Björn Töpel | d817991 | 2019-11-01 12:03:46 +0100 | [diff] [blame] | 87 | |
| 88 | static inline struct xdp_sock *__xsk_map_lookup_elem(struct bpf_map *map, |
| 89 | u32 key) |
| 90 | { |
| 91 | struct xsk_map *m = container_of(map, struct xsk_map, map); |
| 92 | struct xdp_sock *xs; |
| 93 | |
| 94 | if (key >= map->max_entries) |
| 95 | return NULL; |
| 96 | |
| 97 | xs = READ_ONCE(m->xsk_map[key]); |
| 98 | return xs; |
| 99 | } |
Björn Töpel | 0402acd | 2019-08-15 11:30:13 +0200 | [diff] [blame] | 100 | |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 101 | #else |
Magnus Karlsson | a71506a | 2020-05-20 21:20:51 +0200 | [diff] [blame] | 102 | |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 103 | static inline int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp) |
| 104 | { |
| 105 | return -ENOTSUPP; |
| 106 | } |
| 107 | |
Magnus Karlsson | a71506a | 2020-05-20 21:20:51 +0200 | [diff] [blame] | 108 | static inline int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp) |
Maxim Mikityanskiy | d57d764 | 2019-06-26 17:35:24 +0300 | [diff] [blame] | 109 | { |
Magnus Karlsson | a71506a | 2020-05-20 21:20:51 +0200 | [diff] [blame] | 110 | return -EOPNOTSUPP; |
Maxim Mikityanskiy | d57d764 | 2019-06-26 17:35:24 +0300 | [diff] [blame] | 111 | } |
| 112 | |
Magnus Karlsson | a71506a | 2020-05-20 21:20:51 +0200 | [diff] [blame] | 113 | static inline void __xsk_map_flush(void) |
Björn Töpel | 9025403 | 2018-08-28 14:44:27 +0200 | [diff] [blame] | 114 | { |
| 115 | } |
| 116 | |
Magnus Karlsson | a71506a | 2020-05-20 21:20:51 +0200 | [diff] [blame] | 117 | static inline struct xdp_sock *__xsk_map_lookup_elem(struct bpf_map *map, |
| 118 | u32 key) |
Jakub Kicinski | 1661d34 | 2018-10-01 14:51:36 +0200 | [diff] [blame] | 119 | { |
| 120 | return NULL; |
| 121 | } |
| 122 | |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 123 | #endif /* CONFIG_XDP_SOCKETS */ |
| 124 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 125 | #endif /* _LINUX_XDP_SOCK_H */ |