修正部分编译错误
This commit is contained in:
279
plugin/business/traffic-mirror/src/rebuild.cpp
Normal file
279
plugin/business/traffic-mirror/src/rebuild.cpp
Normal file
@@ -0,0 +1,279 @@
|
||||
|
||||
#include <tfe_proxy.h>
|
||||
#include <tfe_utils.h>
|
||||
#include <tfe_types.h>
|
||||
#include <traffic_mirror.h>
|
||||
|
||||
struct traffic_mirror_rebuild
|
||||
{
|
||||
struct tfe_stream_addr * addr;
|
||||
struct profile_table_ex_data * target;
|
||||
struct traffic_mirror_ethdev * ethdev;
|
||||
|
||||
uint32_t c_seq;
|
||||
uint32_t s_seq;
|
||||
uint32_t c_ipid;
|
||||
uint32_t s_ipid;
|
||||
uint8_t c_ttl;
|
||||
uint8_t s_ttl;
|
||||
};
|
||||
|
||||
/* The definition of vlan_hdr and tcp_hdr is from DPDK 17.05 */
|
||||
struct vlan_hdr
|
||||
{
|
||||
uint16_t vlan_tci; /**< Priority (3) + CFI (1) + Identifier Code (12) */
|
||||
uint16_t eth_proto;/**< Ethernet type of encapsulated frame. */
|
||||
} __attribute__((__packed__));
|
||||
|
||||
struct tcp_hdr {
|
||||
uint16_t src_port; /**< TCP source port. */
|
||||
uint16_t dst_port; /**< TCP destination port. */
|
||||
uint32_t sent_seq; /**< TX data sequence number. */
|
||||
uint32_t recv_ack; /**< RX data acknowledgement sequence number. */
|
||||
uint8_t data_off; /**< Data offset. */
|
||||
uint8_t tcp_flags; /**< TCP flags */
|
||||
uint16_t rx_win; /**< RX flow control window. */
|
||||
uint16_t cksum; /**< TCP checksum. */
|
||||
uint16_t tcp_urp; /**< TCP urgent pointer, if any. */
|
||||
} __attribute__((__packed__));
|
||||
|
||||
#define TCP_URG_FLAG 0x20
|
||||
#define TCP_ACK_FLAG 0x10
|
||||
#define TCP_PSH_FLAG 0x08
|
||||
#define TCP_RST_FLAG 0x04
|
||||
#define TCP_SYN_FLAG 0x02
|
||||
#define TCP_FIN_FLAG 0x01
|
||||
#define TCP_FLAG_ALL 0x3F
|
||||
|
||||
static int tcp_header_construct(unsigned char *buf, unsigned short sp,
|
||||
unsigned short dp, unsigned int seq, unsigned int ack,
|
||||
unsigned char flags, unsigned short win, unsigned short urg)
|
||||
{
|
||||
struct tcp_hdr * tcp_hdr = (struct tcp_hdr *) buf;
|
||||
assert(tcp_hdr != NULL);
|
||||
|
||||
tcp_hdr->src_port = sp;
|
||||
tcp_hdr->dst_port = dp;
|
||||
tcp_hdr->sent_seq = htonl(seq);
|
||||
tcp_hdr->recv_ack = htonl(ack);
|
||||
tcp_hdr->data_off = 5;
|
||||
tcp_hdr->tcp_flags = flags;
|
||||
tcp_hdr->rx_win = htons(win);
|
||||
tcp_hdr->cksum = 0;
|
||||
tcp_hdr->tcp_urp = 0;
|
||||
return sizeof(struct tcp_hdr);
|
||||
}
|
||||
|
||||
static int tcp_header_construct_by_stream_addr(struct tfe_stream_addr * addr, unsigned char *buf,
|
||||
unsigned int seq, unsigned int ack, unsigned char flags, unsigned short win, unsigned short urg)
|
||||
{
|
||||
unsigned short sport;
|
||||
unsigned short dport;
|
||||
|
||||
if (addr->addrtype == TFE_ADDR_STREAM_TUPLE4_V4)
|
||||
{
|
||||
sport = addr->tuple4_v4->source;
|
||||
dport = addr->tuple4_v4->dest;
|
||||
}
|
||||
else if (addr->addrtype == TFE_ADDR_STREAM_TUPLE4_V6)
|
||||
{
|
||||
sport = addr->tuple4_v6->source;
|
||||
dport = addr->tuple4_v6->dest;
|
||||
}
|
||||
else
|
||||
{
|
||||
assert(0);
|
||||
}
|
||||
|
||||
return tcp_header_construct(buf, sport, dport, seq, ack, flags, win, urg);
|
||||
}
|
||||
|
||||
static int ipv4_header_construct(unsigned char *buf, unsigned short carry_layer_len,
|
||||
unsigned char tos, unsigned short id, unsigned short frag, unsigned char ttl,
|
||||
unsigned char protocol, unsigned int src, unsigned int dst)
|
||||
{
|
||||
struct iphdr * ip_hdr = (struct iphdr *) buf;
|
||||
ip_hdr->version = 4;
|
||||
ip_hdr->ihl = 5;
|
||||
ip_hdr->tos = tos;
|
||||
ip_hdr->tot_len = htons(sizeof(struct iphdr) + carry_layer_len);
|
||||
ip_hdr->id = htons(id);
|
||||
ip_hdr->frag_off = 0;
|
||||
ip_hdr->ttl = ttl;
|
||||
ip_hdr->protocol = protocol;
|
||||
ip_hdr->check = 0;
|
||||
ip_hdr->saddr = src;
|
||||
ip_hdr->daddr = dst;
|
||||
|
||||
return sizeof(struct iphdr);
|
||||
}
|
||||
|
||||
static int ip_header_construct_by_stream_addr(struct tfe_stream_addr * addr,
|
||||
unsigned char *buf, unsigned short carry_layer_len, unsigned char tos,
|
||||
unsigned short id, unsigned short frag, unsigned char ttl, unsigned char protocol)
|
||||
{
|
||||
if (addr->addrtype == TFE_ADDR_STREAM_TUPLE4_V4)
|
||||
{
|
||||
return ipv4_header_construct(buf, carry_layer_len, tos, id,
|
||||
frag, ttl, protocol, addr->tuple4_v4->saddr.s_addr, addr->tuple4_v4->daddr.s_addr);
|
||||
}
|
||||
else if (addr->addrtype == TFE_ADDR_STREAM_TUPLE4_V6)
|
||||
{
|
||||
/* TODO: IPv6 */
|
||||
assert(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
assert(0);
|
||||
}
|
||||
|
||||
assert(0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void ether_header_construct(unsigned char *buf, unsigned char *dst,
|
||||
unsigned char *src, unsigned short type)
|
||||
{
|
||||
struct ethhdr * eth_hdr = (struct ethhdr *) buf;
|
||||
memcpy(eth_hdr->h_dest, dst, ETHER_ADDR_LEN);
|
||||
memcpy(eth_hdr->h_source, src, ETHER_ADDR_LEN);
|
||||
eth_hdr->h_proto = htons(type);
|
||||
}
|
||||
|
||||
static void vlan_tag_construct(unsigned char *buf, unsigned short tci, unsigned short type)
|
||||
{
|
||||
struct vlan_hdr * vlan_hdr = (struct vlan_hdr *)buf;
|
||||
vlan_hdr->vlan_tci = htons(tci);
|
||||
vlan_hdr->eth_proto = htons(type);
|
||||
}
|
||||
|
||||
static void l2_send_to_target(struct traffic_mirror_ethdev * ethdev,
|
||||
unsigned char * snd_buffer, unsigned int l3_data_offset, unsigned int l3_data_len,
|
||||
struct ether_addr * target_addr, unsigned int vlan_tci, unsigned l3_protocol)
|
||||
{
|
||||
assert(l3_data_offset >= (sizeof(struct ethhdr) + sizeof(struct vlan_hdr)));
|
||||
unsigned int header_offset = l3_data_offset;
|
||||
|
||||
/* need to construct vlan header */
|
||||
if (vlan_tci > 0)
|
||||
{
|
||||
header_offset -= sizeof(struct vlan_hdr);
|
||||
vlan_tag_construct(snd_buffer + header_offset, vlan_tci, l3_protocol);
|
||||
}
|
||||
|
||||
unsigned int eth_protocol = vlan_tci > 0 ? ETH_P_8021Q : l3_protocol;
|
||||
header_offset -= sizeof(struct ethhdr);
|
||||
|
||||
ether_header_construct(snd_buffer + header_offset, (unsigned char *)target_addr->ether_addr_octet,
|
||||
(unsigned char *)ethdev->local_ether_addr, eth_protocol);
|
||||
}
|
||||
|
||||
static void l2_send_to_target_group(struct traffic_mirror_ethdev * ethdev, struct profile_table_ex_data * t_group,
|
||||
unsigned char * snd_buffer, unsigned int l3_data_offset, unsigned int l3_data_len, unsigned l3_protocol)
|
||||
{
|
||||
for(unsigned int i = 0; i < t_group->nr_targets; i++)
|
||||
{
|
||||
l2_send_to_target(ethdev, snd_buffer, l3_data_offset, l3_data_len,
|
||||
&t_group->ether_addrs[i], t_group->vlans[i], l3_protocol);
|
||||
}
|
||||
}
|
||||
|
||||
static void tcp_segment_send_to_target_group(struct tfe_stream_addr * addr, struct traffic_mirror_ethdev * ethdev,
|
||||
struct profile_table_ex_data * t_group, const char * payload, unsigned int payload_len,
|
||||
unsigned int seq, unsigned int ack, char flags)
|
||||
{
|
||||
char pkt[ETHER_MAX_LEN];
|
||||
unsigned sz_pkt_prepend = sizeof(struct ethhdr) + sizeof(struct iphdr) + sizeof(struct tcphdr);
|
||||
unsigned l3_l4_header_len = 0;
|
||||
|
||||
l3_l4_header_len += tcp_header_construct_by_stream_addr(addr,
|
||||
(unsigned char *)pkt + sz_pkt_prepend, seq, ack, flags, 0xffff, 0);
|
||||
|
||||
l3_l4_header_len += ip_header_construct_by_stream_addr(addr, (unsigned char *)pkt + sz_pkt_prepend,
|
||||
sizeof(struct tcphdr) + payload_len, 0, 0x1000, 0, 128, IPPROTO_TCP);
|
||||
|
||||
sz_pkt_prepend -= l3_l4_header_len;
|
||||
|
||||
l2_send_to_target_group(ethdev, t_group, (unsigned char *)pkt,
|
||||
sz_pkt_prepend, l3_l4_header_len + payload_len, ETHERTYPE_IP);
|
||||
}
|
||||
|
||||
static void tcp_send_to_target_group(struct tfe_stream_addr * addr, struct traffic_mirror_ethdev * ethdev,
|
||||
struct profile_table_ex_data * t_group, const char * payload,
|
||||
unsigned int payload_len, unsigned int seq, unsigned int ack, char flags)
|
||||
{
|
||||
unsigned int payload_offset = 0;
|
||||
unsigned mss = ethdev->mtu - (sizeof(struct iphdr) + sizeof(struct tcphdr));
|
||||
|
||||
while(payload_offset < payload_len)
|
||||
{
|
||||
unsigned int payload_sz_seg = MIN(payload_offset, mss);
|
||||
const char * payload_ptr_seg = payload + payload_offset;
|
||||
|
||||
tcp_segment_send_to_target_group(addr, ethdev, t_group, payload_ptr_seg, payload_sz_seg, seq, ack, flags);
|
||||
seq += payload_sz_seg;
|
||||
payload_offset += payload_sz_seg;
|
||||
}
|
||||
}
|
||||
|
||||
struct traffic_mirror_rebuild * traffic_mirror_rebuild_create(struct tfe_stream_addr * addr,
|
||||
struct profile_table_ex_data * target, struct traffic_mirror_ethdev * ethdev)
|
||||
{
|
||||
struct traffic_mirror_rebuild * instance = ALLOC(struct traffic_mirror_rebuild, 1);
|
||||
instance->target = target;
|
||||
instance->ethdev = ethdev;
|
||||
|
||||
/* the c_seq, s_seq, c_ipid, s_ipid is random
|
||||
* TODO: use the fast random algorithm like Linux TCP/IP stack */
|
||||
instance->c_seq = random();
|
||||
instance->s_seq = random();
|
||||
instance->c_ipid = random();
|
||||
instance->s_ipid = random();
|
||||
instance->s_ttl = 128;
|
||||
instance->c_ttl = 64;
|
||||
return instance;
|
||||
}
|
||||
|
||||
void traffic_mirror_rebuild_destroy(struct traffic_mirror_rebuild * instance)
|
||||
{
|
||||
free(instance);
|
||||
}
|
||||
|
||||
void traffic_mirror_rebuild_handshake(struct traffic_mirror_rebuild * instance)
|
||||
{
|
||||
tcp_send_to_target_group(instance->addr, instance->ethdev, instance->target,
|
||||
NULL, 0, instance->c_seq, 0, TCP_SYN_FLAG);
|
||||
|
||||
tcp_send_to_target_group(instance->addr, instance->ethdev, instance->target,
|
||||
NULL, 0, instance->s_seq, instance->c_seq + 1, TCP_SYN_FLAG | TCP_ACK_FLAG);
|
||||
|
||||
tcp_send_to_target_group(instance->addr, instance->ethdev, instance->target,
|
||||
NULL, 0, instance->c_seq + 1, instance->s_seq + 1, TCP_ACK_FLAG);
|
||||
|
||||
instance->s_seq++;
|
||||
instance->c_seq++;
|
||||
}
|
||||
|
||||
void traffic_mirror_rebuild_data(struct traffic_mirror_rebuild * instance,
|
||||
const char * data, unsigned int datalen, enum tfe_conn_dir dir)
|
||||
{
|
||||
if (dir == CONN_DIR_DOWNSTREAM)
|
||||
{
|
||||
tcp_send_to_target_group(instance->addr, instance->ethdev, instance->target,
|
||||
NULL, 0, instance->c_seq, instance->s_seq + 1, TCP_ACK_FLAG);
|
||||
|
||||
instance->c_seq += datalen;
|
||||
}
|
||||
else
|
||||
{
|
||||
tcp_send_to_target_group(instance->addr, instance->ethdev, instance->target,
|
||||
NULL, 0, instance->s_seq, instance->c_seq + 1, TCP_ACK_FLAG);
|
||||
|
||||
instance->s_seq += datalen;
|
||||
}
|
||||
}
|
||||
|
||||
void traffic_mirror_rebuild_farewell(struct traffic_mirror_rebuild * instance)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Reference in New Issue
Block a user