Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update vport.c to be general #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions vport.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,27 @@ struct vport_t
struct sockaddr_in vswitch_addr; // VSwitch address
};

void vport_init(struct vport_t *vport, const char *server_ip_str, int server_port);
const char* defalut_ifname = "tapyuan";

void vport_init(struct vport_t *vport, const char *server_ip_str, int server_port, const char* interface_name);
void *forward_ether_data_to_vswitch(void *raw_vport);
void *forward_ether_data_to_tap(void *raw_vport);

int main(int argc, char const *argv[])
{
// parse arguments
if (argc != 3)
if (argc < 3)
{
ERROR_PRINT_THEN_EXIT("Usage: vport {server_ip} {server_port}\n");
ERROR_PRINT_THEN_EXIT("Usage: vport {server_ip} {server_port} {interface_name}\n");
}
const char *server_ip_str = argv[1];
int server_port = atoi(argv[2]);
const char *interface_name = (argc == 4) ? argv[3] : NULL;

// vport init
struct vport_t vport;
vport_init(&vport, server_ip_str, server_port);
vport_init(&vport, server_ip_str, server_port, interface_name);

// up forwarder
pthread_t up_forwarder;
Expand Down Expand Up @@ -61,10 +65,15 @@ int main(int argc, char const *argv[])
/**
* init VPort instance: create TAP device and client socket
*/
void vport_init(struct vport_t *vport, const char *server_ip_str, int server_port)
void vport_init(struct vport_t *vport, const char *server_ip_str, int server_port, const char* interface_name)
{
// alloc tap device
char ifname[IFNAMSIZ] = "tapyuan";
char ifname[IFNAMSIZ];
if ( !interface_name )
strcpy(ifname, defalut_ifname);
else
strcpy(ifname, interface_name);

int tapfd = tap_alloc(ifname);
if (tapfd < 0)
{
Expand Down Expand Up @@ -115,7 +124,7 @@ void *forward_ether_data_to_vswitch(void *raw_vport)
ssize_t sendsz = sendto(vport->vport_sockfd, ether_data, ether_datasz, 0, (struct sockaddr *)&vport->vswitch_addr, sizeof(vport->vswitch_addr));
if (sendsz != ether_datasz)
{
fprintf(stderr, "sendto size mismatch: ether_datasz=%d, sendsz=%d\n", ether_datasz, sendsz);
fprintf(stderr, "sendto size mismatch: ether_datasz=%d, sendsz=%ld\n", ether_datasz, sendsz);
}

printf("[VPort] Sent to VSwitch:"
Expand Down Expand Up @@ -153,7 +162,7 @@ void *forward_ether_data_to_tap(void *raw_vport)
ssize_t sendsz = write(vport->tapfd, ether_data, ether_datasz);
if (sendsz != ether_datasz)
{
fprintf(stderr, "sendto size mismatch: ether_datasz=%d, sendsz=%d\n", ether_datasz, sendsz);
fprintf(stderr, "sendto size mismatch: ether_datasz=%d, sendsz=%ld\n", ether_datasz, sendsz);
}

printf("[VPort] Forward to TAP device:"
Expand All @@ -167,4 +176,4 @@ void *forward_ether_data_to_tap(void *raw_vport)
ether_datasz);
}
}
}
}