net_wire.c 5.71 KB
Newer Older
Antoine Kaufmann's avatar
Antoine Kaufmann committed
1
/*
Antoine Kaufmann's avatar
Antoine Kaufmann committed
2
3
 * Copyright 2021 Max Planck Institute for Software Systems, and
 * National University of Singapore
Antoine Kaufmann's avatar
Antoine Kaufmann committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 *
 * 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.
 */

25
#include <assert.h>
Antoine Kaufmann's avatar
Antoine Kaufmann committed
26
#include <fcntl.h>
27
28
29
#include <linux/if.h>
#include <linux/if_tun.h>
#include <pcap/pcap.h>
Antoine Kaufmann's avatar
Antoine Kaufmann committed
30
#include <pthread.h>
31
#include <signal.h>
Antoine Kaufmann's avatar
Antoine Kaufmann committed
32
#include <stdio.h>
33
#include <stdlib.h>
Antoine Kaufmann's avatar
Antoine Kaufmann committed
34
35
36
37
38
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <unistd.h>

39
#include <simbricks/netif/netsim.h>
40
#include <simbricks/proto/base.h>
Antoine Kaufmann's avatar
Antoine Kaufmann committed
41

Antoine Kaufmann's avatar
Antoine Kaufmann committed
42
43
static uint64_t sync_period = (500 * 1000ULL);  // 500ns
static uint64_t eth_latency = (500 * 1000ULL);  // 500ns
44
45
46
47
static uint64_t cur_ts;
static int exiting = 0;
static pcap_dumper_t *dumpfile = NULL;

48
49
static void sigint_handler(int dummy) {
  exiting = 1;
50
51
}

52
53
static void sigusr1_handler(int dummy) {
  fprintf(stderr, "main_time = %lu\n", cur_ts);
54
55
}

56
57
static void move_pkt(struct SimbricksNetIf *from,
                     struct SimbricksNetIf *to) {
58
  volatile union SimbricksProtoNetD2N *msg_from =
59
      SimbricksNetIfD2NPoll(from, cur_ts);
60
61
62
  volatile union SimbricksProtoNetN2D *msg_to;
  volatile struct SimbricksProtoNetD2NSend *tx;
  volatile struct SimbricksProtoNetN2DRecv *rx;
63
64
65
66
67
68
  struct pcap_pkthdr ph;
  uint8_t type;

  if (msg_from == NULL)
    return;

69
70
  type = msg_from->dummy.own_type & SIMBRICKS_PROTO_NET_D2N_MSG_MASK;
  if (type == SIMBRICKS_PROTO_NET_D2N_MSG_SEND) {
71
72
73
74
75
76
77
78
79
80
    tx = &msg_from->send;

    // log to pcap file if initialized
    if (dumpfile) {
      memset(&ph, 0, sizeof(ph));
      ph.ts.tv_sec = cur_ts / 1000000000000ULL;
      ph.ts.tv_usec = (cur_ts % 1000000000000ULL) / 1000ULL;
      ph.caplen = tx->len;
      ph.len = tx->len;
      pcap_dump((unsigned char *)dumpfile, &ph, (unsigned char *)tx->data);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
81
82
    }

83
    msg_to = SimbricksNetIfN2DAlloc(to, cur_ts, eth_latency);
84
85
86
87
88
    if (msg_to != NULL) {
      rx = &msg_to->recv;
      rx->len = tx->len;
      rx->port = 0;
      memcpy((void *)rx->data, (void *)tx->data, tx->len);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
89

90
      // WMB();
91
92
      rx->own_type = SIMBRICKS_PROTO_NET_N2D_MSG_RECV |
          SIMBRICKS_PROTO_NET_N2D_OWN_DEV;
93
94
    } else {
      fprintf(stderr, "move_pkt: dropping packet\n");
Antoine Kaufmann's avatar
Antoine Kaufmann committed
95
    }
96
  } else if (type == SIMBRICKS_PROTO_NET_D2N_MSG_SYNC) {
97
98
99
100
  } else {
    fprintf(stderr, "move_pkt: unsupported type=%u\n", type);
    abort();
  }
Antoine Kaufmann's avatar
Antoine Kaufmann committed
101

102
  SimbricksNetIfD2NDone(from, msg_from);
103
}
104

105
int main(int argc, char *argv[]) {
106
  struct SimbricksNetIf nsif_a, nsif_b;
107
108
109
  uint64_t ts_a, ts_b;
  int sync_a, sync_b;
  pcap_t *pc = NULL;
110
  int sync_mode = SIMBRICKS_PROTO_SYNC_SIMBRICKS;
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137

  if (argc < 3 && argc > 7) {
    fprintf(stderr,
            "Usage: net_wire SOCKET-A SOCKET-B [SYNC-MODE] "
            "[SYNC-PERIOD] [ETH-LATENCY] [PCAP-FILE]\n");
    return EXIT_FAILURE;
  }

  signal(SIGINT, sigint_handler);
  signal(SIGTERM, sigint_handler);
  signal(SIGUSR1, sigusr1_handler);

  if (argc >= 4)
    sync_mode = strtol(argv[3], NULL, 0);

  if (argc >= 5)
    sync_period = strtoull(argv[4], NULL, 0) * 1000ULL;

  if (argc >= 6)
    eth_latency = strtoull(argv[5], NULL, 0) * 1000ULL;

  if (argc >= 7) {
    pc = pcap_open_dead_with_tstamp_precision(DLT_EN10MB, 65535,
                                              PCAP_TSTAMP_PRECISION_NANO);
    if (pc == NULL) {
      perror("pcap_open_dead failed");
      return EXIT_FAILURE;
138
139
    }

140
141
142
    dumpfile = pcap_dump_open(pc, argv[6]);
  }

143
144
  assert(sync_mode == SIMBRICKS_PROTO_SYNC_SIMBRICKS ||
      sync_mode == SIMBRICKS_PROTO_SYNC_BARRIER);
145
146

  sync_a = sync_b = 1;
147
  if (SimbricksNetIfInit(&nsif_a, argv[1], &sync_a) != 0) {
148
149
    return -1;
  }
150
  if (SimbricksNetIfInit(&nsif_b, argv[2], &sync_b) != 0) {
151
152
153
154
155
    return -1;
  }

  printf("start polling\n");
  while (!exiting) {
156
157
158
    if (SimbricksNetIfN2DSync(&nsif_a, cur_ts, eth_latency, sync_period,
        sync_mode) != 0) {
      fprintf(stderr, "SimbricksNetIfN2DSync(nsif_a) failed\n");
159
      abort();
Antoine Kaufmann's avatar
Antoine Kaufmann committed
160
    }
161
162
163
    if (SimbricksNetIfN2DSync(&nsif_b, cur_ts, eth_latency, sync_period,
        sync_mode) != 0) {
      fprintf(stderr, "SimbricksNetIfN2DSync(nsif_a) failed\n");
164
      abort();
Antoine Kaufmann's avatar
Antoine Kaufmann committed
165
    }
166
    SimbricksNetIfAdvanceEpoch(cur_ts, sync_period, sync_mode);
167
168
169
170

    do {
      move_pkt(&nsif_a, &nsif_b);
      move_pkt(&nsif_b, &nsif_a);
171
172
      ts_a = SimbricksNetIfD2NTimestamp(&nsif_a);
      ts_b = SimbricksNetIfD2NTimestamp(&nsif_b);
173
174
175
176
    } while (!exiting &&
             ((sync_a && ts_a <= cur_ts) || (sync_b && ts_b <= cur_ts)));

    if (sync_a && sync_b)
177
178
      cur_ts = SimbricksNetIfAdvanceTime(ts_a <= ts_b ? ts_a : ts_b,
                                   sync_period, sync_mode);
179
    else if (sync_a)
180
      cur_ts = SimbricksNetIfAdvanceTime(ts_a, sync_period, sync_mode);
181
    else if (sync_b)
182
      cur_ts = SimbricksNetIfAdvanceTime(ts_b, sync_period, sync_mode);
183
184
185
186
187
  }

  if (dumpfile)
    pcap_dump_close(dumpfile);
  return 0;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
188
}