forked from iqiyi/dpvs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.c
265 lines (223 loc) · 6.62 KB
/
common.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
/*
* DPVS is a software load balancer (Virtual Server) based on DPDK.
*
* Copyright (C) 2017 iQIYI (www.iqiyi.com).
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdbool.h>
#include <numa.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <net/ethernet.h>
#include "common.h"
struct dpvs_err_tab {
int errcode;
const char *errmsg;
};
const char *dpvs_strerror(int err)
{
/* TODO: "per-lcorelize" it */
const static struct dpvs_err_tab err_tab[] = {
{ EDPVS_OK, "OK" },
{ EDPVS_INVAL, "invalid parameter" },
{ EDPVS_NOMEM, "no memory" },
{ EDPVS_EXIST, "already exist" },
{ EDPVS_NOTEXIST, "not exist" },
{ EDPVS_INVPKT, "invalid packet" },
{ EDPVS_DROP, "packet dropped" },
{ EDPVS_NOPROT, "no protocol" },
{ EDPVS_NOROUTE, "no route" },
{ EDPVS_DEFRAG, "defragment error" },
{ EDPVS_FRAG, "fragment error" },
{ EDPVS_DPDKAPIFAIL, "failed dpdk api" },
{ EDPVS_IDLE, "nothing to do" },
{ EDPVS_BUSY, "resource busy" },
{ EDPVS_NOTSUPP, "not support" },
{ EDPVS_RESOURCE, "no resource" },
{ EDPVS_OVERLOAD, "overloaded" },
{ EDPVS_NOSERV, "no service" },
{ EDPVS_DISABLED, "disabled" },
{ EDPVS_NOROOM, "no room" },
{ EDPVS_NONEALCORE, "non-EAL thread lcore" },
{ EDPVS_CALLBACKFAIL, "callback failed" },
{ EDPVS_IO, "I/O error" },
{ EDPVS_MSG_FAIL, "msg callback failed"},
{ EDPVS_MSG_DROP, "msg dropped"},
{ EDPVS_PKTSTOLEN, "stolen packet"},
{ EDPVS_SYSCALL, "system call failed"},
{ EDPVS_NODEV, "no such device"},
{ EDPVS_KNICONTINUE, "kni to continue"},
{ EDPVS_INPROGRESS, "in progress"},
};
int i;
for (i = 0; i < NELEMS(err_tab); i++) {
if (err == err_tab[i].errcode)
return err_tab[i].errmsg;
}
return "<unknow>";
}
static dpvs_state_t g_dpvs_tate = DPVS_STATE_STOP;
void dpvs_state_set(dpvs_state_t stat)
{
g_dpvs_tate = stat;
}
dpvs_state_t dpvs_state_get(void)
{
return g_dpvs_tate;
}
int get_numa_nodes(void)
{
int numa_nodes;
if (numa_available() < 0)
numa_nodes = 0;
else
numa_nodes = numa_max_node();
return (numa_nodes + 1);
}
/* if (num+offset) == 2^n, return true,
* otherwise return false and 'lower' is filled with
* the closest lower bound value to 'num' */
bool is_power2(int num, int offset, int *lower)
{
int i, onum;
bool ret = true;
onum = num + offset;
if (num < 2 || onum < 2) {
if (lower)
*lower = num;
return false;
}
for (i = 1; (onum >> i) > 1;i++) {
if ((onum >> i) % 2) {
ret = false;
}
}
if (lower)
*lower = (1u << i);
return ret;
}
int linux_set_if_mac(const char *ifname, const unsigned char mac[ETH_ALEN])
{
int sock_fd;
struct ifreq ifr = {};
if (!ifname || !mac || !strncmp(ifname, "lo", 2))
return EDPVS_INVAL;
sock_fd = socket(PF_INET, SOCK_DGRAM, 0);
if (sock_fd < 0)
return EDPVS_SYSCALL;
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", ifname);
ifr.ifr_hwaddr.sa_family = 1;
memcpy(ifr.ifr_hwaddr.sa_data, mac, ETH_ALEN);
if (ioctl(sock_fd, SIOCSIFHWADDR, &ifr)) {
close(sock_fd);
return EDPVS_SYSCALL;
}
close(sock_fd);
return EDPVS_OK;
}
static int linux_hw_mc_mod(const char *ifname,
const uint8_t hwma[ETH_ALEN], bool add)
{
int fd, cmd;
struct ifreq ifr = {};
snprintf(ifr.ifr_name, IFNAMSIZ, "%s", ifname);
memcpy(&ifr.ifr_hwaddr.sa_data, hwma, ETH_ALEN);
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0)
return EDPVS_SYSCALL;
cmd = add ? SIOCADDMULTI : SIOCDELMULTI;
if (ioctl(fd, cmd, (void *)&ifr) != 0) {
fprintf(stderr, "%s: fail to set link mcast to %s: %s\n",
__func__, ifname, strerror(errno));
close(fd);
return EDPVS_SYSCALL;
}
close(fd);
return 0;
}
int linux_hw_mc_add(const char *ifname, const uint8_t hwma[ETH_ALEN])
{
return linux_hw_mc_mod(ifname, hwma, true);
}
int linux_hw_mc_del(const char *ifname, const uint8_t hwma[ETH_ALEN])
{
return linux_hw_mc_mod(ifname, hwma, false);
}
ssize_t readn(int fd, void *vptr, size_t n)
{
size_t nleft;
ssize_t nread;
char *ptr;
ptr = vptr;
nleft = n;
while (nleft > 0) {
if ((nread = read(fd, ptr, nleft)) < 0) {
if (errno == EINTR)
nread = 0; /* and call read() again */
else
return (-1);
} else if (nread == 0)
break; /* EOF */
nleft -= nread;
ptr += nread;
}
return (n - nleft); /* return >= 0 */
}
/* write "n" bytes to a descriptor */
ssize_t writen(int fd, const void *vptr, size_t n)
{
size_t nleft;
ssize_t nwritten;
const char *ptr;
ptr = vptr;
nleft = n;
while (nleft > 0) {
if ((nwritten = write(fd, ptr, nleft)) <= 0) {
if (nwritten < 0 && errno == EINTR)
nwritten = 0; /* and call write() again */
else
return (-1); /* error */
}
nleft -= nwritten;
ptr += nwritten;
}
return (n);
}
/* send "n" bytes to a descriptor */
ssize_t sendn(int fd, const void *vptr, size_t n, int flags)
{
size_t nleft;
ssize_t nwritten;
const char *ptr;
ptr = vptr;
nleft = n;
while (nleft > 0) {
if ((nwritten = send(fd, ptr, nleft, flags)) <= 0) {
if (nwritten < 0 && errno == EINTR)
nwritten = 0; /* and call send() again */
else
return (-1); /* error */
}
nleft -= nwritten;
ptr += nwritten;
}
return (n);
}