forked from iqiyi/dpvs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.c
172 lines (148 loc) · 4.63 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
/*
* 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 <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_DPDKAPIFAIL, "failed dpdk api" },
{ EDPVS_IDLE, "nothing to do" },
{ EDPVS_BUSY, "resource busy" },
{ EDPVS_NOTSUPP, "not support" },
{ 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_SYSCALL, "system call failed"},
{ EDPVS_KNICONTINUE, "kni to continue"},
{ EDPVS_PKTSTOLEN, "stolen packet"},
{ EDPVS_INPROGRESS, "in progress"},
{ EDPVS_NODEV, "no such device"},
};
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;
}
/* 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);
}