af_xdp: AF_XDP input plugin
[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_log(lvl, dev, f, ...) \
26   vlib_log(lvl, af_xdp_main.log_class, "%v: " f, (dev)->name, ##__VA_ARGS__)
27
28 #define foreach_af_xdp_device_flags \
29   _(0, INITIALIZED, "initialized") \
30   _(1, ERROR, "error") \
31   _(2, ADMIN_UP, "admin-up") \
32   _(4, LINK_UP, "link-up") \
33   _(8, ZEROCOPY, "zero-copy") \
34
35 enum
36 {
37 #define _(a, b, c) AF_XDP_DEVICE_F_##b = (1 << a),
38   foreach_af_xdp_device_flags
39 #undef _
40 };
41
42 #define af_xdp_device_error(dev, fmt, ...) \
43   if (!(dev)->error) \
44     { \
45       clib_error_t *err_ = clib_error_return_unix (0, fmt, ## __VA_ARGS__); \
46       if (!clib_atomic_bool_cmp_and_swap (&(dev)->error, 0, err_)) \
47         clib_error_free(err_); \
48     }
49
50 typedef struct
51 {
52   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
53
54   /* fields below are accessed in data-plane (hot) */
55
56   struct xsk_ring_cons rx;
57   struct xsk_ring_prod fq;
58   int xsk_fd;
59
60   /* fields below are accessed in control-plane only (cold) */
61
62   uword file_index;
63 } af_xdp_rxq_t;
64
65 typedef struct
66 {
67   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
68
69   /* fields below are accessed in data-plane (hot) */
70
71   clib_spinlock_t lock;
72   struct xsk_ring_prod tx;
73   struct xsk_ring_cons cq;
74   int xsk_fd;
75 } af_xdp_txq_t;
76
77 typedef struct
78 {
79   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
80
81   /* fields below are accessed in data-plane (hot) */
82
83   af_xdp_rxq_t *rxqs;
84   af_xdp_txq_t *txqs;
85   vlib_buffer_t *buffer_template;
86   u32 per_interface_next_index;
87   u32 sw_if_index;
88   u32 hw_if_index;
89   u32 flags;
90   u8 pool;                      /* buffer pool index */
91   u8 txq_num;
92
93   /* fields below are accessed in control-plane only (cold) */
94
95   char *name;
96   char *linux_ifname;
97   u32 dev_instance;
98   u8 hwaddr[6];
99
100   struct xsk_umem **umem;
101   struct xsk_socket **xsk;
102
103   struct bpf_object *bpf_obj;
104   unsigned linux_ifindex;
105
106   /* error */
107   clib_error_t *error;
108 } af_xdp_device_t;
109
110 typedef struct
111 {
112   af_xdp_device_t *devices;
113   vlib_log_class_t log_class;
114   u16 msg_id_base;
115 } af_xdp_main_t;
116
117 extern af_xdp_main_t af_xdp_main;
118
119 typedef enum
120 {
121   AF_XDP_MODE_AUTO = 0,
122   AF_XDP_MODE_COPY = 1,
123   AF_XDP_MODE_ZERO_COPY = 2,
124 } af_xdp_mode_t;
125
126 typedef struct
127 {
128   char *linux_ifname;
129   char *name;
130   char *prog;
131   af_xdp_mode_t mode;
132   u32 rxq_size;
133   u32 txq_size;
134   u32 rxq_num;
135
136   /* return */
137   int rv;
138   u32 sw_if_index;
139   clib_error_t *error;
140 } af_xdp_create_if_args_t;
141
142 void af_xdp_create_if (vlib_main_t * vm, af_xdp_create_if_args_t * args);
143 void af_xdp_delete_if (vlib_main_t * vm, af_xdp_device_t * ad);
144
145 extern vlib_node_registration_t af_xdp_input_node;
146 extern vnet_device_class_t af_xdp_device_class;
147
148 /* format.c */
149 format_function_t format_af_xdp_device;
150 format_function_t format_af_xdp_device_name;
151 format_function_t format_af_xdp_input_trace;
152
153 /* unformat.c */
154 unformat_function_t unformat_af_xdp_create_if_args;
155
156 typedef struct
157 {
158   u32 next_index;
159   u32 hw_if_index;
160 } af_xdp_input_trace_t;
161
162 #define foreach_af_xdp_tx_func_error \
163 _(NO_FREE_SLOTS, "no free tx slots") \
164 _(SENDTO_REQUIRED, "sendto required") \
165 _(SENDTO_FAILURES, "sendto failures")
166
167 typedef enum
168 {
169 #define _(f,s) AF_XDP_TX_ERROR_##f,
170   foreach_af_xdp_tx_func_error
171 #undef _
172     AF_XDP_TX_N_ERROR,
173 } af_xdp_tx_func_error_t;
174
175 #endif /* _AF_XDP_H_ */
176
177 /*
178  * fd.io coding-style-patch-verification: ON
179  *
180  * Local Variables:
181  * eval: (c-set-style "gnu")
182  * End:
183  */