net_tap.c 4.08 KB
Newer Older
1
/*
Antoine Kaufmann's avatar
Antoine Kaufmann committed
2
3
 * Copyright 2021 Max Planck Institute for Software Systems, and
 * National University of Singapore
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <fcntl.h>
26
27
#include <linux/if.h>
#include <linux/if_tun.h>
28
#include <pthread.h>
29
#include <stdio.h>
30
#include <stdlib.h>
31
32
33
34
35
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <unistd.h>

36
#include <simbricks/network/if.h>
37

Antoine Kaufmann's avatar
Antoine Kaufmann committed
38
// #define DEBUG_PKTMETA
39

40
static struct SimbricksNetIf nsif;
41
42
static int tap_fd;

43
44
45
static int tap_open(const char *name) {
  struct ifreq ifr;
  int fd;
46

47
48
49
50
  if ((fd = open("/dev/net/tun", O_RDWR)) < 0) {
    perror("tap_open: open failed");
    return -1;
  }
51

52
53
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
54
55
56
57
58

  /* 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"
59
  strncpy(ifr.ifr_name, name, IFNAMSIZ);
60
#pragma GCC diagnostic pop
61

62
63
64
65
66
  if (ioctl(fd, TUNSETIFF, &ifr) != 0) {
    perror("tap_open: ioctl failed");
    close(fd);
    return -1;
  }
67

68
  return fd;
69
70
}

71
static void d2n_send(volatile struct SimbricksProtoNetMsgPacket *s) {
72
#ifdef DEBUG_PKTMETA
73
  printf("sent packet: len=%u\n", s->len);
74
#endif
75

76
77
78
  if (write(tap_fd, (void *)s->data, s->len) != (ssize_t)s->len) {
    perror("d2n_send: send failed");
  }
79
80
}

81
static void poll_d2n(void) {
82
  volatile union SimbricksProtoNetMsg *msg = SimbricksNetIfInPoll(&nsif, 0);
83
  uint8_t type;
84

85
86
87
  /* message not ready */
  if (msg == NULL)
    return;
88

89
  type = SimbricksNetIfInType(&nsif, msg);
90
  switch (type) {
91
92
    case SIMBRICKS_PROTO_NET_MSG_PACKET:
      d2n_send(&msg->packet);
93
      break;
94

95
96
97
    default:
      fprintf(stderr, "poll_d2n: unsupported type=%u\n", type);
  }
98

99
  SimbricksNetIfInDone(&nsif, msg);
100
101
}

102
static void *rx_handler(void *arg) {
103
104
  volatile union SimbricksProtoNetMsg *msg;
  volatile struct SimbricksProtoNetMsgPacket *rx;
105
  ssize_t len;
106

107
  while (1) {
108
    msg = SimbricksNetIfOutAlloc(&nsif, 0);
109
110
111
    if (msg == NULL) {
      fprintf(stderr, "coudl not allocate message for rx\n");
      abort();
112
    }
113
    rx = &msg->packet;
114

115
116
    len = read(tap_fd, (void *)rx->data,
               SimbricksBaseIfOutMsgLen(&nsif.base) - sizeof(*msg));
117
118
    if (len <= 0) {
      perror("rx handler: read failed");
119
    }
120
121
122
123
124
    rx->len = len;
    rx->port = 0;
#ifdef DEBUG_PKTMETA
    printf("received packet: len=%u\n", rx->len);
#endif
125

126
    SimbricksNetIfOutSend(&nsif, msg, SIMBRICKS_PROTO_NET_MSG_PACKET);
127
128
  }
}
129

130
131
132
133
134
135
136
137
138
139
140
141
int main(int argc, char *argv[]) {
  int sync;

  if (argc != 3) {
    fprintf(stderr, "Usage: net_tap TAP_DEVICE_NAME SOCKET\n");
    return EXIT_FAILURE;
  }

  if ((tap_fd = tap_open(argv[1])) < 0) {
    return -1;
  }

142
143
144
  struct SimbricksBaseIfParams params;
  SimbricksNetIfDefaultParams(&params);

145
  sync = 0;
146
  if (SimbricksNetIfInit(&nsif, &params, argv[2], &sync) != 0) {
147
148
149
150
151
152
153
154
155
156
157
158
159
160
    close(tap_fd);
    return -1;
  }

  pthread_t worker;
  if (pthread_create(&worker, NULL, rx_handler, NULL) != 0) {
    return EXIT_FAILURE;
  }

  printf("start polling\n");
  while (1) {
    poll_d2n();
  }
  return 0;
161
}