af_xdp: update interrupt mode to new infra
[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   _ (3, LINK_UP, "link-up")                                                   \
35   _ (4, 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   u32 queue_index;
66   u8 is_polling;
67 } af_xdp_rxq_t;
68
69 typedef struct
70 {
71   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
72
73   /* fields below are accessed in data-plane (hot) */
74
75   clib_spinlock_t lock;
76   struct xsk_ring_prod tx;
77   struct xsk_ring_cons cq;
78   int xsk_fd;
79 } af_xdp_txq_t;
80
81 typedef struct
82 {
83   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
84
85   /* fields below are accessed in data-plane (hot) */
86
87   af_xdp_rxq_t *rxqs;
88   af_xdp_txq_t *txqs;
89   vlib_buffer_t *buffer_template;
90   u32 per_interface_next_index;
91   u32 sw_if_index;
92   u32 hw_if_index;
93   u32 flags;
94   u8 pool;                      /* buffer pool index */
95   u8 txq_num;
96
97   /* fields below are accessed in control-plane only (cold) */
98
99   char *name;
100   char *linux_ifname;
101   u32 dev_instance;
102   u8 hwaddr[6];
103
104   struct xsk_umem **umem;
105   struct xsk_socket **xsk;
106
107   struct bpf_object *bpf_obj;
108   unsigned linux_ifindex;
109
110   /* error */
111   clib_error_t *error;
112 } af_xdp_device_t;
113
114 typedef struct
115 {
116   af_xdp_device_t *devices;
117   vlib_log_class_t log_class;
118   u16 msg_id_base;
119 } af_xdp_main_t;
120
121 extern af_xdp_main_t af_xdp_main;
122
123 typedef enum
124 {
125   AF_XDP_MODE_AUTO = 0,
126   AF_XDP_MODE_COPY = 1,
127   AF_XDP_MODE_ZERO_COPY = 2,
128 } af_xdp_mode_t;
129
130 typedef struct
131 {
132   char *linux_ifname;
133   char *name;
134   char *prog;
135   af_xdp_mode_t mode;
136   u32 rxq_size;
137   u32 txq_size;
138   u32 rxq_num;
139
140   /* return */
141   int rv;
142   u32 sw_if_index;
143   clib_error_t *error;
144 } af_xdp_create_if_args_t;
145
146 void af_xdp_create_if (vlib_main_t * vm, af_xdp_create_if_args_t * args);
147 void af_xdp_delete_if (vlib_main_t * vm, af_xdp_device_t * ad);
148
149 extern vlib_node_registration_t af_xdp_input_node;
150 extern vnet_device_class_t af_xdp_device_class;
151
152 /* format.c */
153 format_function_t format_af_xdp_device;
154 format_function_t format_af_xdp_device_name;
155 format_function_t format_af_xdp_input_trace;
156
157 /* unformat.c */
158 unformat_function_t unformat_af_xdp_create_if_args;
159
160 typedef struct
161 {
162   u32 next_index;
163   u32 hw_if_index;
164 } af_xdp_input_trace_t;
165
166 #define foreach_af_xdp_tx_func_error \
167 _(NO_FREE_SLOTS, "no free tx slots") \
168 _(SENDTO_REQUIRED, "sendto required") \
169 _(SENDTO_FAILURES, "sendto failures")
170
171 typedef enum
172 {
173 #define _(f,s) AF_XDP_TX_ERROR_##f,
174   foreach_af_xdp_tx_func_error
175 #undef _
176     AF_XDP_TX_N_ERROR,
177 } af_xdp_tx_func_error_t;
178
179 #endif /* _AF_XDP_H_ */
180
181 /*
182  * fd.io coding-style-patch-verification: ON
183  *
184  * Local Variables:
185  * eval: (c-set-style "gnu")
186  * End:
187  */