"vscode:/vscode.git/clone" did not exist on "6130394165d2fe0e5013db93b2aa8cac7f3ef3d9"
Commit 4d0690d1 authored by Antoine Kaufmann's avatar Antoine Kaufmann
Browse files

sims/net/tap: new API refactor

parent 2f308d01
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
#include <sys/mman.h> #include <sys/mman.h>
#include <unistd.h> #include <unistd.h>
#include <simbricks/netif/netif.h> #include <simbricks/network/if.h>
// #define DEBUG_PKTMETA // #define DEBUG_PKTMETA
...@@ -51,7 +51,13 @@ static int tap_open(const char *name) { ...@@ -51,7 +51,13 @@ static int tap_open(const char *name) {
memset(&ifr, 0, sizeof(ifr)); memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TAP | IFF_NO_PI; ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
/* fix gcc warning here: this is okay, kernel will nul-terminate ifr name,
if neeeded, no need for us to worry */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstringop-truncation"
strncpy(ifr.ifr_name, name, IFNAMSIZ); strncpy(ifr.ifr_name, name, IFNAMSIZ);
#pragma GCC diagnostic pop
if (ioctl(fd, TUNSETIFF, &ifr) != 0) { if (ioctl(fd, TUNSETIFF, &ifr) != 0) {
perror("tap_open: ioctl failed"); perror("tap_open: ioctl failed");
...@@ -62,7 +68,7 @@ static int tap_open(const char *name) { ...@@ -62,7 +68,7 @@ static int tap_open(const char *name) {
return fd; return fd;
} }
static void d2n_send(volatile struct SimbricksProtoNetD2NSend *s) { static void d2n_send(volatile struct SimbricksProtoNetMsgPacket *s) {
#ifdef DEBUG_PKTMETA #ifdef DEBUG_PKTMETA
printf("sent packet: len=%u\n", s->len); printf("sent packet: len=%u\n", s->len);
#endif #endif
...@@ -73,40 +79,41 @@ static void d2n_send(volatile struct SimbricksProtoNetD2NSend *s) { ...@@ -73,40 +79,41 @@ static void d2n_send(volatile struct SimbricksProtoNetD2NSend *s) {
} }
static void poll_d2n(void) { static void poll_d2n(void) {
volatile union SimbricksProtoNetD2N *msg = SimbricksNetIfD2NPoll(&nsif, 0); volatile union SimbricksProtoNetMsg *msg = SimbricksNetIfInPoll(&nsif, 0);
uint8_t type; uint8_t type;
/* message not ready */ /* message not ready */
if (msg == NULL) if (msg == NULL)
return; return;
type = msg->dummy.own_type & SIMBRICKS_PROTO_NET_D2N_MSG_MASK; type = SimbricksNetIfInType(&nsif, msg);
switch (type) { switch (type) {
case SIMBRICKS_PROTO_NET_D2N_MSG_SEND: case SIMBRICKS_PROTO_NET_MSG_PACKET:
d2n_send(&msg->send); d2n_send(&msg->packet);
break; break;
default: default:
fprintf(stderr, "poll_d2n: unsupported type=%u\n", type); fprintf(stderr, "poll_d2n: unsupported type=%u\n", type);
} }
SimbricksNetIfD2NDone(&nsif, msg); SimbricksNetIfInDone(&nsif, msg);
} }
static void *rx_handler(void *arg) { static void *rx_handler(void *arg) {
volatile union SimbricksProtoNetN2D *msg; volatile union SimbricksProtoNetMsg *msg;
volatile struct SimbricksProtoNetN2DRecv *rx; volatile struct SimbricksProtoNetMsgPacket *rx;
ssize_t len; ssize_t len;
while (1) { while (1) {
msg = SimbricksNetIfN2DAlloc(&nsif, 0, 0); msg = SimbricksNetIfOutAlloc(&nsif, 0);
if (msg == NULL) { if (msg == NULL) {
fprintf(stderr, "coudl not allocate message for rx\n"); fprintf(stderr, "coudl not allocate message for rx\n");
abort(); abort();
} }
rx = &msg->recv; rx = &msg->packet;
len = read(tap_fd, (void *)rx->data, nsif.n2d_elen - sizeof(*msg)); len = read(tap_fd, (void *)rx->data,
SimbricksBaseIfOutMsgLen(&nsif.base) - sizeof(*msg));
if (len <= 0) { if (len <= 0) {
perror("rx handler: read failed"); perror("rx handler: read failed");
} }
...@@ -116,9 +123,7 @@ static void *rx_handler(void *arg) { ...@@ -116,9 +123,7 @@ static void *rx_handler(void *arg) {
printf("received packet: len=%u\n", rx->len); printf("received packet: len=%u\n", rx->len);
#endif #endif
// WMB(); SimbricksNetIfOutSend(&nsif, msg, SIMBRICKS_PROTO_NET_MSG_PACKET);
rx->own_type =
SIMBRICKS_PROTO_NET_N2D_MSG_RECV | SIMBRICKS_PROTO_NET_N2D_OWN_DEV;
} }
} }
...@@ -134,8 +139,11 @@ int main(int argc, char *argv[]) { ...@@ -134,8 +139,11 @@ int main(int argc, char *argv[]) {
return -1; return -1;
} }
struct SimbricksBaseIfParams params;
SimbricksNetIfDefaultParams(&params);
sync = 0; sync = 0;
if (SimbricksNetIfInit(&nsif, argv[2], &sync) != 0) { if (SimbricksNetIfInit(&nsif, &params, argv[2], &sync) != 0) {
close(tap_fd); close(tap_fd);
return -1; return -1;
} }
......
...@@ -28,7 +28,7 @@ OBJS := $(d)net_tap.o ...@@ -28,7 +28,7 @@ OBJS := $(d)net_tap.o
$(OBJS): CPPFLAGS := $(CPPFLAGS) -I$(d)include/ $(OBJS): CPPFLAGS := $(CPPFLAGS) -I$(d)include/
$(bin_net_tap): $(OBJS) $(lib_netif) -lpcap -lpthread $(bin_net_tap): $(OBJS) $(lib_netif) $(lib_base) -lpcap -lpthread
CLEAN := $(bin_net_tap) $(OBJS) CLEAN := $(bin_net_tap) $(OBJS)
ALL := $(bin_net_tap) ALL := $(bin_net_tap)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment