forked from baidu/dperf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkni.c
338 lines (279 loc) · 7.35 KB
/
kni.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* Copyright (c) 2022 Baidu.com, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Author: Jianzhang Peng (pengjianzhang@baidu.com)
*/
#include "kni.h"
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <rte_ethdev.h>
#include <rte_kni.h>
#include <rte_bus_pci.h>
#include "config.h"
#include "port.h"
#include "mbuf.h"
#include "work_space.h"
#include "bond.h"
static void kni_set_name(struct config *cfg, struct netif_port *port, char *name);
#if RTE_VERSION >= RTE_VERSION_NUM(19,0,0,0)
static int kni_set_mtu(uint16_t port_id, struct rte_kni_conf *conf)
{
int ret = 0;
struct rte_eth_dev_info dev_info;
ret = rte_eth_dev_info_get(port_id, &dev_info);
if (ret != 0) {
return -1;
}
conf->min_mtu = dev_info.min_mtu;
conf->max_mtu = dev_info.max_mtu;
rte_eth_dev_get_mtu(port_id, &conf->mtu);
return 0;
}
#else
static int kni_set_mtu(__rte_unused uint16_t port_id, __rte_unused struct rte_kni_conf *conf)
{
return 0;
}
#endif
#if RTE_VERSION >= RTE_VERSION_NUM(18,0,0,0)
/* set mac before rte_kni_alloc */
static int kni_set_mac(struct netif_port *port, struct rte_kni_conf *conf)
{
memcpy(&(conf->mac_addr), &port->local_mac, ETH_ADDR_LEN);
return 0;
}
static int kni_set_pci(__rte_unused uint16_t port_id, __rte_unused struct rte_kni_conf *conf)
{
return 0;
}
static int kni_set_hwaddr(__rte_unused struct config *cfg, __rte_unused struct netif_port *port)
{
return 0;
}
#else
static int kni_set_mac(__rte_unused struct netif_port *port, __rte_unused struct rte_kni_conf *conf)
{
return 0;
}
static int kni_set_pci(uint16_t port_id, struct rte_kni_conf *conf)
{
struct rte_eth_dev_info dev_info;
memset(&dev_info, 0, sizeof(dev_info));
rte_eth_dev_info_get(port_id, &dev_info);
if (dev_info.pci_dev) {
conf->addr = dev_info.pci_dev->addr;
conf->id = dev_info.pci_dev->id;
}
return 0;
}
/* DPDK-17 need to set MAC by ioctl after rte_kni_alloc */
static int kni_set_hwaddr(struct config *cfg, struct netif_port *port)
{
int fd = -1;
int ret = -1;
struct ifreq ifr;
fd = socket(PF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
return -1;
}
memset(&ifr, 0, sizeof(struct ifreq));
kni_set_name(cfg, port, ifr.ifr_name);
memcpy(ifr.ifr_hwaddr.sa_data, &port->local_mac, ETH_ADDR_LEN);
ifr.ifr_hwaddr.sa_family = 1; /* ARPHRD_ETHER */
if (ioctl(fd, SIOCSIFHWADDR, &ifr) < 0) {
printf("Error: kni set hwaddr\n");
} else {
ret = 0;
}
close(fd);
return ret;
}
#endif
static void kni_set_name(struct config *cfg, struct netif_port *port, char *name)
{
int idx = 0;
/*
* do not use 'port->id'.
* we want ifname id starting from zero.
* */
idx = port - &(cfg->ports[0]);
snprintf(name, RTE_KNI_NAMESIZE, "%s%d", cfg->kni_ifname, idx);
}
static struct rte_kni *kni_alloc(struct config *cfg, struct netif_port *port)
{
uint16_t port_id = 0;
struct rte_kni *kni = NULL;
struct rte_mempool *mbuf_pool = NULL;
struct rte_kni_conf conf;
/* the first thread of a port process this kni */
mbuf_pool = port->mbuf_pool[0];
port_id = port->id;
memset(&conf, 0, sizeof(conf));
conf.group_id = port_id;
conf.mbuf_size = RTE_MBUF_DEFAULT_DATAROOM;
kni_set_name(cfg, port, conf.name);
if (kni_set_mtu(port_id, &conf) < 0) {
return NULL;
}
if (kni_set_mac(port, &conf) < 0) {
return NULL;
}
if (kni_set_pci(port_id, &conf) < 0) {
return NULL;
}
kni = rte_kni_alloc(mbuf_pool, &conf, NULL);
if (kni_set_hwaddr(cfg, port) < 0) {
rte_kni_release(kni);
return NULL;
}
return kni;
}
static int kni_set_link_up(struct config *cfg, struct netif_port *port)
{
int fd = -1;
int ret = -1;
struct ifreq ifr;
if (!port->kni) {
return 0;
}
fd = socket(PF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
return -1;
}
memset(&ifr, 0, sizeof(struct ifreq));
kni_set_name(cfg, port, ifr.ifr_name);
if (ioctl(fd, SIOCGIFFLAGS, (void *) &ifr) < 0) {
printf("kni get flags error\n");
goto out;
}
ifr.ifr_flags |= IFF_UP;
if (ioctl(fd, SIOCSIFFLAGS, (void *) &ifr) < 0) {
printf("kni set flags error\n");
goto out;
}
ret = 0;
out:
close(fd);
return ret;
}
/*
* kni_link_up() should be called at the ctl thread.
*
* When a kni interface is linked up, the kni will send some messages,
* which need to be processed by rte_kni_handle_request().
* These messages have a timeout and need to be processed immediately.
* Otherwise, the interface will fail to link up.
*/
int kni_link_up(struct config *cfg)
{
struct netif_port *port = NULL;
config_for_each_port(cfg, port) {
if (kni_set_link_up(cfg, port) < 0) {
return -1;
}
}
return 0;
}
static void kni_free(struct config *cfg)
{
struct netif_port *port = NULL;
config_for_each_port(cfg, port) {
if (port->kni == NULL) {
continue;
}
if (rte_kni_release(port->kni)) {
printf("failed to free kni\n");
}
port->kni = NULL;
}
}
/*
* kni address cannot in client or server range
*/
static int kni_create(struct config *cfg)
{
struct rte_kni *kni = NULL;
struct netif_port *port = NULL;
rte_kni_init(NETIF_PORT_MAX);
config_for_each_port(cfg, port) {
kni = kni_alloc(cfg, port);
if (kni == NULL) {
return -1;
}
port->kni = kni;
}
return 0;
}
int kni_start(struct config *cfg)
{
if (!cfg->kni) {
return 0;
}
return kni_create(cfg);
}
void kni_stop(struct config *cfg)
{
if (cfg->kni) {
kni_free(cfg);
}
}
void kni_recv(struct work_space *ws, struct rte_mbuf *m)
{
struct netif_port *port = NULL;
struct rte_kni *kni = NULL;
port = ws->port;
kni = port->kni;
if (kni) {
if (rte_kni_tx_burst(kni, &m, 1) == 1) {
net_stats_kni_rx();
return;
}
}
mbuf_free2(m);
}
void kni_broadcast(struct work_space *ws, struct rte_mbuf *m)
{
struct rte_mbuf *m2 = NULL;
m2 = mbuf_dup(m);
if (m2) {
kni_recv(ws, m2);
}
}
static void kni_send_mbuf(struct work_space *ws, struct rte_mbuf *m)
{
if (port_is_bond4(ws->port) && mbuf_is_neigh(m)) {
bond_broadcast(ws, m);
}
work_space_tx_send(ws, m);
net_stats_kni_tx();
}
void kni_send(struct work_space *ws)
{
int i = 0;
int num = 0;
struct rte_mbuf *mbufs[NB_RXD];
struct netif_port *port = NULL;
struct rte_kni *kni = NULL;
port = ws->port;
kni = port->kni;
rte_kni_handle_request(kni);
num = rte_kni_rx_burst(kni, mbufs, NB_RXD);
for (i = 0; i < num; i++) {
kni_send_mbuf(ws, mbufs[i]);
}
}