perf: VLAN封装在Eth头后插入VLAN头时memmove耗时较高

This commit is contained in:
luwenpeng
2023-11-21 10:46:00 +08:00
parent dc305adb3b
commit b3841fcaff

View File

@@ -430,22 +430,27 @@ static void overwrite_vlan_id(struct vlan_hdr *vlan_hdr, uint16_t vlan_id)
void vlan_encapsulate(marsio_buff_t *mbuff, int vlan_id, int replace_orig_vlan_header) void vlan_encapsulate(marsio_buff_t *mbuff, int vlan_id, int replace_orig_vlan_header)
{ {
struct ethhdr *old_eth_hdr = (struct ethhdr *)marsio_buff_mtod(mbuff);
if (replace_orig_vlan_header == 0) if (replace_orig_vlan_header == 0)
{ {
append: append:
struct ethhdr *eth_hdr = (struct ethhdr *)marsio_buff_prepend(mbuff, sizeof(struct vlan_hdr)); struct ethhdr *new_eth_hdr = (struct ethhdr *)marsio_buff_prepend(mbuff, sizeof(struct vlan_hdr));
struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)((char *)eth_hdr + sizeof(struct ethhdr)); char *dst = (char *)new_eth_hdr;
char *src = (char *)old_eth_hdr;
memcpy(dst, src, 4);
memcpy(dst + 4, src + 4, 4);
memcpy(dst + 8, src + 8, 4);
new_eth_hdr->h_proto = htons(ETH_P_8021Q);
memmove(eth_hdr, (char *)eth_hdr + sizeof(struct vlan_hdr), sizeof(struct ethhdr)); struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)((char *)new_eth_hdr + sizeof(struct ethhdr));
build_vlan_header(vlan_hdr, vlan_id, ntohs(eth_hdr->h_proto)); build_vlan_header(vlan_hdr, vlan_id, ntohs(old_eth_hdr->h_proto));
eth_hdr->h_proto = htons(ETH_P_8021Q);
return; return;
} }
else else
{ {
struct ethhdr *eth_hdr = (struct ethhdr *)marsio_buff_mtod(mbuff); uint16_t next_proto = old_eth_hdr->h_proto;
int next_proto = eth_hdr->h_proto; char *start_layer = (char *)old_eth_hdr + sizeof(struct ethhdr);
char *start_layer = (char *)eth_hdr + sizeof(struct ethhdr);
char *next_layer = start_layer; char *next_layer = start_layer;
while (next_proto == htons(ETH_P_8021Q) || next_proto == htons(ETH_P_8021AD)) while (next_proto == htons(ETH_P_8021Q) || next_proto == htons(ETH_P_8021AD))
@@ -477,7 +482,7 @@ void vlan_encapsulate(marsio_buff_t *mbuff, int vlan_id, int replace_orig_vlan_h
struct ethhdr *new_eth_hdr = (struct ethhdr *)((char *)vlan_hdr - sizeof(struct ethhdr)); struct ethhdr *new_eth_hdr = (struct ethhdr *)((char *)vlan_hdr - sizeof(struct ethhdr));
overwrite_vlan_id(vlan_hdr, vlan_id); overwrite_vlan_id(vlan_hdr, vlan_id);
memmove(new_eth_hdr, (char *)eth_hdr, sizeof(struct ethhdr)); memmove(new_eth_hdr, (char *)old_eth_hdr, sizeof(struct ethhdr));
new_eth_hdr->h_proto = htons(ETH_P_8021Q); new_eth_hdr->h_proto = htons(ETH_P_8021Q);
marsio_buff_adj(mbuff, offset - sizeof(struct vlan_hdr)); marsio_buff_adj(mbuff, offset - sizeof(struct vlan_hdr));