af_xdp: add option to claim all available rx queues
[vpp.git] / src / plugins / af_xdp / af_xdp.h
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2018 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17
18 #ifndef _AF_XDP_H_
19 #define _AF_XDP_H_
20
21 #include <vlib/log.h>
22 #include <vnet/interface.h>
23 #include <bpf/xsk.h>
24
25 #define AF_XDP_NUM_RX_QUEUES_ALL        ((u16)-1)
26
27 #define af_xdp_log(lvl, dev, f, ...) \
28   vlib_log(lvl, af_xdp_main.log_class, "%v: " f, (dev)->name, ##__VA_ARGS__)
29
30 #define foreach_af_xdp_device_flags \
31   _(0, INITIALIZED, "initialized") \
32   _(1, ERROR, "error") \
33   _(2, ADMIN_UP, "admin-up") \
34   _(4, LINK_UP, "link-up") \
35   _(8, ZEROCOPY, "zero-copy") \
36
37 enum
38 {
39 #define _(a, b, c) AF_XDP_DEVICE_F_##b = (1 << a),
40   foreach_af_xdp_device_flags
41 #undef _
42 };
43
44 #define af_xdp_device_error(dev, fmt, ...) \
45   if (!(dev)->error) \
46     { \
47       clib_error_t *err_ = clib_error_return_unix (0, fmt, ## __VA_ARGS__); \
48       if (!clib_atomic_bool_cmp_and_swap (&(dev)->error, 0, err_)) \
49         clib_error_free(err_); \
50     }
51
52 typedef struct
53 {
54   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
55
56   /* fields below are accessed in data-plane (hot) */
57
58   struct xsk_ring_cons rx;
59   struct xsk_ring_prod fq;
60   int xsk_fd;
61
62   /* fields below are accessed in control-plane only (cold) */
63
64   uword file_index;
65 } af_xdp_rxq_t;
66
67 typedef struct
68 {
69   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
70
71   /* fields below are accessed in data-plane (hot) */
72
73   clib_spinlock_t lock;
74   struct xsk_ring_prod tx;
75   struct xsk_ring_cons cq;
76   int xsk_fd;
77 } af_xdp_txq_t;
78
79 typedef struct
80 {
81   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
82
83   /* fields below are accessed in data-plane (hot) */
84
85   af_xdp_rxq_t *rxqs;
86   af_xdp_txq_t *txqs;
87   vlib_buffer_t *buffer_template;
88   u32 per_interface_next_index;
89   u32 sw_if_index;
90   u32 hw_if_index;
91   u32 flags;
92   u8 pool;                      /* buffer pool index */
93   u8 txq_num;
94
95   /* fields below are accessed in control-plane only (cold) */
96
97   char *name;
98   char *linux_ifname;
99   u32 dev_instance;
100   u8 hwaddr[6];
101
102   struct xsk_umem **umem;
103   struct xsk_socket **xsk;
104
105   struct bpf_object *bpf_obj;
106   unsigned linux_ifindex;
107
108   /* error */
109   clib_error_t *error;
110 } af_xdp_device_t;
111
112 typedef struct
113 {
114   af_xdp_device_t *devices;
115   vlib_log_class_t log_class;
116   u16 msg_id_base;
117 } af_xdp_main_t;
118
119 extern af_xdp_main_t af_xdp_main;
120
121 typedef enum
122 {
123   AF_XDP_MODE_AUTO = 0,
124   AF_XDP_MODE_COPY = 1,
125   AF_XDP_MODE_ZERO_COPY = 2,
126 } af_xdp_mode_t;
127
128 typedef struct
129 {
130   char *linux_ifname;
131   char *name;
132   char *prog;
133   af_xdp_mode_t mode;
134   u32 rxq_size;
135   u32 txq_size;
136   u32 rxq_num;
137
138   /* return */
139   int rv;
140   u32 sw_if_index;
141   clib_error_t *error;
142 } af_xdp_create_if_args_t;
143
144 void af_xdp_create_if (vlib_main_t * vm, af_xdp_create_if_args_t * args);
145 void af_xdp_delete_if (vlib_main_t * vm, af_xdp_device_t * ad);
146
147 extern vlib_node_registration_t af_xdp_input_node;
148 extern vnet_device_class_t af_xdp_device_class;
149
150 /* format.c */
151 format_function_t format_af_xdp_device;
152 format_function_t format_af_xdp_device_name;
153 format_function_t format_af_xdp_input_trace;
154
155 /* unformat.c */
156 unformat_function_t unformat_af_xdp_create_if_args;
157
158 typedef struct
159 {
160   u32 next_index;
161   u32 hw_if_index;
162 } af_xdp_input_trace_t;
163
164 #define foreach_af_xdp_tx_func_error \
165 _(NO_FREE_SLOTS, "no free tx slots") \
166 _(SENDTO_REQUIRED, "sendto required") \
167 _(SENDTO_FAILURES, "sendto failures")
168
169 typedef enum
170 {
171 #define _(f,s) AF_XDP_TX_ERROR_##f,
172   foreach_af_xdp_tx_func_error
173 #undef _
174     AF_XDP_TX_N_ERROR,
175 } af_xdp_tx_func_error_t;
176
177 #endif /* _AF_XDP_H_ */
178
179 /*
180  * fd.io coding-style-patch-verification: ON
181  *
182  * Local Variables:
183  * eval: (c-set-style "gnu")
184  * End:
185  */