内容纲要

https://github.com/upa/vxlan

此项目截获了以太包,添加 VXLAN 头并封装在 UDP 包里再发出去。

[cpp]
void send_etherflame_from_vxlan_to_local (struct vxlan_instance * vins,
struct ether_header * ether, int len);

void send_etherflame_from_local_to_vxlan (struct vxlan_instance * vins,
struct ether_header * ether, int len);

[/cpp]

[cpp]
void
send_etherflame_from_vxlan_to_local (struct vxlan_instance * vins,
struct ether_header * ether, int len)
{

if (write (vins->tap_sock, ether, len) < 0) {
error_warn("Write etherflame to local network failed");
}

return;
}

void
send_etherflame_from_local_to_vxlan (struct vxlan_instance * vins,
struct ether_header * ether, int len)
{
struct vxlan_hdr vhdr;
struct fdb_entry * entry;
struct msghdr mhdr;
struct iovec iov[2];

/* Forwarding */
memset (&vhdr, 0, sizeof (vhdr));
vhdr.vxlan_flags = VXLAN_VALIDFLAG;
memcpy (vhdr.vxlan_vni, &(vins->vni), VXLAN_VNISIZE);

iov[0].iov_base = &vhdr;
iov[0].iov_len = sizeof (vhdr);
iov[1].iov_base = ether;
iov[1].iov_len = len;

mhdr.msg_iov = iov;
mhdr.msg_iovlen = 2;
mhdr.msg_controllen = 0;

if ((entry = fdb_search_entry (vins->fdb, ether->ether_dhost)) == NULL) {
mhdr.msg_name = &vxlan.mcast_addr;
mhdr.msg_namelen = sizeof (vxlan.mcast_addr);
if (sendmsg (vxlan.udp_sock, &mhdr, 0) < 0)
error_warn ("sendmsg to multicast failed %s",
strerror (errno));
} else {
EXTRACT_PORT (entry->vtep_addr) = htons (VXLAN_PORT_BASE);
mhdr.msg_name = &entry->vtep_addr;
mhdr.msg_namelen = sizeof (entry->vtep_addr);
if (sendmsg (vxlan.udp_sock, &mhdr, 0) < 0)
error_warn ("sendmsg to unicast failed : %s",
strerror (errno));
}

return;
}

[/cpp]

发表评论

电子邮件地址不会被公开。 必填项已用*标注