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
|
#include "sapp_api.h"
#include "sapp_private_api.h"
#include "sapp_declaration.h"
#include "support/MESA_feedback.h"
/*
2020-09-28 add:
��ͳ�����ϵ�packet_io, һ�㲻����ԭʼ�����ݵ�, ԭ���ա�ԭ��ת��,
������inline����ģʽ��, ��vxlan��, ��Ҫ���յ��İ����з�ת, ��ײ�mac, �ײ�ip��.
����ijЩ��ַ������������ԭʼ�������, Ҫͨ��һЩtes�ӿ�����ת������, ��A�豸��ȡvlan������ת��vlan, �ȵ�,
ԭ������mrtunnat���, ��sapp��, mrtunnat��ȡ����, �����˲�, ���������������.
*/
#ifdef __cplusplus
extern "C" {
#endif
extern int marsio_get_route_dir_from_mbuff(void *pkt_reference);
extern unsigned int marsio_get_vlan_id_from_mbuff(void *pkt_reference);
extern int marsio_set_vlan_id_to_mbuff(void *pkt_reference, unsigned int vlan_id_host_order);
extern void marsio_dl_io_set_rawpkt_meta(const raw_pkt_t *raw_pkt, void *send_pkt_io_reference, unsigned char route_dir);
extern int vxlan_packet_is_myself(const raw_pkt_t *rawpkt);
#if 0
static const sapp_vlan_flipping_map_t *vlan_flipping_get_opposite(int cori, unsigned short vlan_id)
{
const sapp_vlan_flipping_map_t *config_vf, *result_vf = NULL;
int i, vf_num;
config_vf = sapp_global_val->config.packet_io.packet_io_tunnel.vlan_flipping_map_array;
vf_num = sapp_global_val->config.packet_io.packet_io_tunnel.vlan_flipping_map_num;
for(i = 0; i < vf_num; i++){
if(('C' == cori) && (vlan_id == config_vf[i].c_router_vlan_id)){
result_vf = &config_vf[i];
break;
}else if(('I' == cori) && (vlan_id == config_vf[i].i_router_vlan_id)){
result_vf = &config_vf[i];
break;
}
}
return result_vf;
}
#endif
static int packet_io_hook_output_vlan_flipping(raw_pkt_t *raw_pkt, unsigned char route_dir, void *raw_pkt_data, void *io_lib_pkt_reference, const char *action)
{
struct mesa_ethernet_hdr *ehdr;
unsigned char tmp_mac_addr[ETHER_ADDR_LEN];
if(raw_pkt->mac_flipping_enable != 0){
ehdr = (struct mesa_ethernet_hdr *)raw_pkt_data;
memcpy(tmp_mac_addr, ehdr->ether_shost, ETHER_ADDR_LEN);
memcpy(ehdr->ether_shost, ehdr->ether_dhost, ETHER_ADDR_LEN);
memcpy(ehdr->ether_dhost, tmp_mac_addr, ETHER_ADDR_LEN);
}
if(raw_pkt->route_dir == route_dir){
/* ԭʼ��ͬ����, ��Ҫ���÷���vlanΪcouple vlan id */
marsio_set_vlan_id_to_mbuff((void *)io_lib_pkt_reference, raw_pkt->vlan_flipping_couple[1]);
sapp_runtime_log(RLOG_LV_DEBUG, "%s: input_vlan_id:%u, output_vlan_id:%u, mac_flapping:%d",
action, raw_pkt->vlan_flipping_couple[0], raw_pkt->vlan_flipping_couple[1], raw_pkt->mac_flipping_enable);
}else{
/* ����ע��, ��Ҫ���÷���vlanΪ�հ�ʱ��vlan id */
marsio_set_vlan_id_to_mbuff((void *)io_lib_pkt_reference, raw_pkt->vlan_flipping_couple[0]);
sapp_runtime_log(RLOG_LV_DEBUG, "%s: input_vlan_id:%u, output_vlan_id:%u, mac_flapping:%d",
action, raw_pkt->vlan_flipping_couple[0], raw_pkt->vlan_flipping_couple[0], raw_pkt->mac_flipping_enable);
}
return 0;
}
static int packet_io_hook_output_vxlan(raw_pkt_t *raw_pkt, void *raw_pkt_data, unsigned char route_dir)
{
const struct mesa_ethernet_hdr *raw_ethh = (struct mesa_ethernet_hdr *)(raw_pkt->raw_pkt_data);
struct mesa_ethernet_hdr *send_ethh = (struct mesa_ethernet_hdr *)(raw_pkt_data);
const struct mesa_ip4_hdr *raw_ip4h = (struct mesa_ip4_hdr *)((char *)raw_pkt->raw_pkt_data + sizeof(struct mesa_ethernet_hdr));
struct mesa_ip4_hdr *send_ip4h = (struct mesa_ip4_hdr *)((char *)raw_pkt_data + sizeof(struct mesa_ethernet_hdr));
/* vxlan udp�㲻�÷�ת, checksum��Ϊ0���� */
struct mesa_udp_hdr *send_outer_udp_hdr = (struct mesa_udp_hdr *)((char *)raw_pkt_data + sizeof(struct mesa_ethernet_hdr)+sizeof(struct mesa_ip4_hdr));
unsigned char tmp_mac_addr[ETH_ALEN];
unsigned int tmp_ip_addr;
/* ע��, ��forwardʱ,raw_pkt->raw_pkt_data��raw_pkt_dataʵ������һ����ַ, ��Ҫ�ݴ�һ��,����ֱ������������copy: */
#if 0
memcpy((void *)send_ethh->ether_shost, raw_ethh->ether_dhost, ETH_ALEN);
memcpy((void *)send_ethh->ether_dhost, raw_ethh->ether_shost, ETH_ALEN);
#else
memcpy(tmp_mac_addr, raw_ethh->ether_shost, ETH_ALEN);
memcpy((void *)send_ethh->ether_shost, raw_ethh->ether_dhost, ETH_ALEN);
memcpy((void *)send_ethh->ether_dhost, tmp_mac_addr, ETH_ALEN);
#endif
/* ע��, ��forwardʱ,raw_pkt->raw_pkt_data��raw_pkt_dataʵ������һ����ַ, ��Ҫ�ݴ�һ��,����ֱ������������copy: */
#if 0
send_ip4h->ip_src.s_addr = raw_ip4h->ip_dst.s_addr;
send_ip4h->ip_dst.s_addr = raw_ip4h->ip_src.s_addr;
#else
tmp_ip_addr = raw_ip4h->ip_src.s_addr;
send_ip4h->ip_src.s_addr = raw_ip4h->ip_dst.s_addr;
send_ip4h->ip_dst.s_addr = tmp_ip_addr;
#endif
/* keepalive icmpӦ����udpЭ��, ��g_dev_plug���ֱ��ע��ԭʼ��, �Ǹ�����, �˴�Ҫ�ж�һ�� */
if(IPPROTO_UDP == raw_ip4h->ip_p){
send_outer_udp_hdr->uh_sum = 0;
//send_vxlan_hdr->dir = route_dir; //20220415 liuxueli for TSG-10227
}
return 0;
}
static int packet_io_hook_update_vlan_couple(raw_pkt_t *raw_pkt, unsigned short vlan_id)
{
const sapp_vlan_flipping_map_t *vlan_map = sapp_global_val->config.packet_io.packet_io_tunnel.vlan_flipping_map_array;
raw_pkt->vlan_flipping_couple[0] = vlan_id;
raw_pkt->vlan_flipping_couple[1] = vlan_map[vlan_id].couple_vlan_id;
raw_pkt->mac_flipping_enable = vlan_map[vlan_id].mac_flipping_enable;
if('C' == vlan_map[vlan_id].this_vlan_route_location){
/* ���ݰ�����C·������, ��C2I(I2E)����, ����inbound_route_dir��ֵ, ���µ�ǰ��route_dir��ֵ */
raw_pkt->route_dir = sapp_global_val->config.packet_io.inbound_route_dir ^ 1;
}else{
/* ���ݰ�����I·������, ��I2C(E2I)����, ����inbound_route_dir��ֵ, ���µ�ǰ��route_dir��ֵ */
raw_pkt->route_dir = sapp_global_val->config.packet_io.inbound_route_dir;
}
sapp_runtime_log(RLOG_LV_DEBUG, "packet_io_hook_update_vlan(), this packet vlan:%u, the couple vlan:%u\n", vlan_id, vlan_map[vlan_id].couple_vlan_id);
raw_pkt->is_overlay_pkt = 1;
return 0;
}
/*
�����metadata���ȡ����, ��ԭʼ�������vlanͷ����ȡ.
*/
static unsigned short get_vlan_id_from_rawpkt(raw_pkt_t *raw_pkt)
{
//todo!!!
return 0;
}
static int packet_io_hook_input_vlan_flipping(raw_pkt_t *raw_pkt, unsigned char dir, int thread_num)
{
unsigned short vlan_id = 1;
const sapp_vlan_flipping_map_t *vlan_map = sapp_global_val->config.packet_io.packet_io_tunnel.vlan_flipping_map_array;
int ret = 0;
vlan_id = marsio_get_vlan_id_from_mbuff((void *)raw_pkt->io_lib_pkt_reference);
if((vlan_id > 1) && (vlan_map[vlan_id].couple_vlan_id > 1)){ /* vlan_id ����vlan flipping����, ������overlay packet */
packet_io_hook_update_vlan_couple(raw_pkt, vlan_id);
ret= 0;
raw_pkt->overlay_layer_bytes = 0;
}else{
vlan_id = get_vlan_id_from_rawpkt(raw_pkt);
if((vlan_id > 1) && (vlan_map[vlan_id].couple_vlan_id > 1)){ /* vlan_id ����vlan flipping����, ������overlay packet */
packet_io_hook_update_vlan_couple(raw_pkt, vlan_id);
}else{
sapp_runtime_log(RLOG_LV_DEBUG, "packet_io_hook_input(), not found vlan_id:%u in vlan flipping table\n", vlan_id);
ret = -1;
}
}
return ret;
}
static int packet_io_hook_input_vxlan(raw_pkt_t *raw_pkt, unsigned char dir, int thread_num)
{
int ret = 0;
const struct mesa_ip4_hdr *ip4hdr = (struct mesa_ip4_hdr *)((char *)raw_pkt->raw_pkt_data + sizeof(struct mesa_ethernet_hdr));
const struct mesa_udp_hdr *carry_vxlan_udp_hdr;
const inline_vxlan_hdr_t *vxlan_hdr;
const struct mesa_icmp_echo_hdr *icmp_hdr;
if(IPPROTO_UDP == ip4hdr->ip_p){
carry_vxlan_udp_hdr = (struct mesa_udp_hdr *)((char *)raw_pkt->raw_pkt_data + sizeof(struct mesa_ethernet_hdr) + sizeof(struct mesa_ip4_hdr));
if(carry_vxlan_udp_hdr->uh_dport != htons(VXLAN_KEEPALIVE_PKT_PORT)
&& carry_vxlan_udp_hdr->uh_dport != htons(VXLAN_OVERLAY_PKT_PORT)){
sapp_runtime_log(RLOG_LV_DEBUG, "packet_io_hook_input_vxlan: recv dst udp port:%u, not %u and not %u in vxlan mode\n",
ntohs(carry_vxlan_udp_hdr->uh_dport), VXLAN_KEEPALIVE_PKT_PORT, VXLAN_OVERLAY_PKT_PORT);
return -1;
}
}else if(IPPROTO_ICMP == ip4hdr->ip_p){
icmp_hdr = (struct mesa_icmp_echo_hdr *)((char *)ip4hdr + ip4hdr->ip_hl*4);
if(icmp_hdr->icmp_type != ICMP_ECHO){
return -1;
}
}else{
sapp_runtime_log(RLOG_LV_DEBUG, "packet_io_hook_input_vxlan: recv not udp and icmp packet, IPPROTO:%d", ip4hdr->ip_p);
return -1;
}
if(vxlan_packet_is_myself(raw_pkt) != 0 || CAP_MODEL_PCAP_DUMPFILE == sapp_global_val->config.packet_io.internal.interface.type_bin){
vxlan_hdr = (inline_vxlan_hdr_t *)((char *)raw_pkt->raw_pkt_data + sizeof(struct mesa_ethernet_hdr) + sizeof(struct mesa_ip4_hdr) + sizeof(struct mesa_udp_hdr));
raw_pkt->is_overlay_pkt = 1; /* vxlan */
raw_pkt->route_dir = vxlan_hdr->dir;
raw_pkt->overlay_layer_bytes = VXLAN_HDR_RESERVED_LEN;
}else{
ret = -1;
}
return ret;
}
int packet_io_hook_input(raw_pkt_t *raw_pkt, unsigned char dir, int thread_num)
{
int ret = 0;
/* vlan flipping��overlay�ɹ���, ���metadata��ԭʼ������vlan������flip����, ����з�ת,�����DZ����Լ�����; */
ret = packet_io_hook_input_vlan_flipping(raw_pkt, dir, thread_num);
if(ret >= 0){
return 0;
}
if(OVERLAY_MODE_VXLAN == sapp_global_val->config.packet_io.packet_io_tunnel.overlay_mode_bin){
ret = packet_io_hook_input_vxlan(raw_pkt, dir, thread_num);
}else if(OVERLAY_MODE_NONE == sapp_global_val->config.packet_io.packet_io_tunnel.overlay_mode_bin){
ret = 0;
}else if(OVERLAY_MODE_NF == sapp_global_val->config.packet_io.packet_io_tunnel.overlay_mode_bin){
ret = marsio_get_route_dir_from_mbuff((void *)raw_pkt->io_lib_pkt_reference);
if(ret >= 0)
{
raw_pkt->route_dir = ret;
}
ret = 0;
}else{
sapp_runtime_log(RLOG_LV_INFO, "packet_io_hook_input() error, unsupport overlay_mode:%d\n",
sapp_global_val->config.packet_io.packet_io_tunnel.overlay_mode_bin);
}
return ret;
}
int packet_io_hook_input_mirror(raw_pkt_t *raw_pkt, unsigned char dir, int thread_num)
{
const unsigned char *p_mac_addr;
p_mac_addr = (unsigned char *)raw_pkt->raw_pkt_data;
if(OVERLAY_MODE_VXLAN == sapp_global_val->config.packet_io.packet_io_tunnel.overlay_mode_bin && CAP_MODEL_PCAP_DUMPFILE == sapp_global_val->config.packet_io.internal.interface.type_bin){
packet_io_hook_input_vxlan(raw_pkt, dir, thread_num);
}
if(p_mac_addr[sapp_global_val->config.packet_io.extract_linkdir_from_mac_byte_index] & sapp_global_val->config.packet_io.extract_linkdir_from_mac_bit_value){
raw_pkt->route_dir = 1;
}else{
raw_pkt->route_dir = 0;
}
return 0;
}
int packet_io_hook_forward(raw_pkt_t *raw_pkt, unsigned char route_dir, int thread_num)
{
int ret=0;
if(raw_pkt->vlan_flipping_couple[0] > 1){
ret = packet_io_hook_output_vlan_flipping(raw_pkt, route_dir, (void *)raw_pkt->raw_pkt_data, (void *)raw_pkt->io_lib_pkt_reference, "forward pkt");
return ret;
}
if(OVERLAY_MODE_VXLAN == sapp_global_val->config.packet_io.packet_io_tunnel.overlay_mode_bin){
ret = packet_io_hook_output_vxlan(raw_pkt, (void *)raw_pkt->raw_pkt_data, route_dir);
}else if(OVERLAY_MODE_NONE == sapp_global_val->config.packet_io.packet_io_tunnel.overlay_mode_bin){
ret = 0;
}else if(OVERLAY_MODE_NF == sapp_global_val->config.packet_io.packet_io_tunnel.overlay_mode_bin){
ret = 0;
}else{
sapp_runtime_log(20, "packet_io_hook_forward() error, unsupport overlay_mode:%d\n", sapp_global_val->config.packet_io.packet_io_tunnel.overlay_mode_bin);
}
return ret;
}
int packet_io_hook_sendto(const raw_pkt_t *raw_pkt, unsigned char route_dir, char *send_pkt_data, void *send_pkt_io_reference)
{
int ret=0;
if(raw_pkt->vlan_flipping_couple[0] > 1){
return packet_io_hook_output_vlan_flipping((raw_pkt_t *)raw_pkt, route_dir, (void *)send_pkt_data, send_pkt_io_reference, "send pkt");
}
marsio_dl_io_set_rawpkt_meta(raw_pkt, send_pkt_io_reference, route_dir);
if(OVERLAY_MODE_VXLAN == sapp_global_val->config.packet_io.packet_io_tunnel.overlay_mode_bin){
ret = packet_io_hook_output_vxlan((raw_pkt_t *)raw_pkt, (void *)send_pkt_data, route_dir);
}else if(OVERLAY_MODE_NONE == sapp_global_val->config.packet_io.packet_io_tunnel.overlay_mode_bin || OVERLAY_MODE_NF == sapp_global_val->config.packet_io.packet_io_tunnel.overlay_mode_bin){
ret = 0;
}else{
sapp_runtime_log(20, "packet_io_hook_sendto() error, unsupport overlay_mode:%d\n", sapp_global_val->config.packet_io.packet_io_tunnel.overlay_mode_bin);
}
return ret;
}
#ifdef __cplusplus
}
#endif
|