netmem.cc 8.59 KB
Newer Older
GAO Bin's avatar
GAO Bin committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
 * Copyright 2022 Max Planck Institute for Software Systems, and
 * National University of Singapore
 *
 * 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>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

#include <cassert>
#include <ctime>
#include <iostream>
#include <vector>

39
40
41
42
#include <arpa/inet.h>
#include <netinet/udp.h>
#include <linux/ip.h>
#include <linux/if_ether.h>
GAO Bin's avatar
GAO Bin committed
43

44
#include <simbricks/base/cxxatomicfix.h>
GAO Bin's avatar
GAO Bin committed
45
extern "C" {
46
47
#include <simbricks/network/if.h>
#include <simbricks/mem/memop.h>
GAO Bin's avatar
GAO Bin committed
48
49
};

Hejing Li's avatar
Hejing Li committed
50
//#define NETMEM_DEBUG 1
GAO Bin's avatar
GAO Bin committed
51

52
static int exiting = 0, sync_mem = 1;
GAO Bin's avatar
GAO Bin committed
53
static uint64_t cur_ts = 0;
54
55
56
uint8_t *mem_array;
uint64_t size;
uint64_t base_addr;
57
uint32_t ip_addr = 0x0A0B0C0D;
58
59
60
61
62
63
64

union mac_addr_{
  uint64_t mac_64;
  uint8_t mac_byte[6];
}; 

union mac_addr_ mac_addr;
GAO Bin's avatar
GAO Bin committed
65
66
67
68
69
70
71
72
73
74

static void sigint_handler(int dummy) {
  exiting = 1;
}

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


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
int HandleRequest (SimbricksNetIf *netif, volatile struct SimbricksProtoNetMsgPacket *packet){
  
  volatile union SimbricksProtoNetMsg *msg_to = SimbricksNetIfOutAlloc(netif, cur_ts);
  if (msg_to == NULL){
    return 0;
  }
  volatile struct SimbricksProtoNetMsgPacket *packet_to = &msg_to->packet;

  uint8_t type;
  uint16_t pkt_len = sizeof(struct ethhdr) + sizeof(struct iphdr) +
                     sizeof(struct udphdr) + sizeof(struct MemOp);
  uint16_t ip_total_len;
  struct ethhdr *eth_hdr = (struct ethhdr *)packet->data;
  struct iphdr *ip_hdr = (struct iphdr *)(eth_hdr + 1);
  struct udphdr *udp_hdr = (struct udphdr *)(ip_hdr + 1);
  struct MemOp *memop = (struct MemOp *)(udp_hdr + 1);
  void *data = (void *)(memop + 1);

  struct ethhdr *to_eth_hdr = (struct ethhdr *)packet_to->data;
  struct iphdr *to_ip_hdr = (struct iphdr *)(to_eth_hdr + 1);
  struct udphdr *to_udp_hdr = (struct udphdr *)(to_ip_hdr + 1);
  struct MemOp *to_memop = (struct MemOp *)(to_udp_hdr + 1);
  void *to_data = (void *)(to_memop + 1);

  type = memop->OpType;
  // Add Ethernet Header
  to_eth_hdr->h_proto = eth_hdr->h_proto;
  memcpy(to_eth_hdr->h_dest, eth_hdr->h_source, ETH_ALEN);
  memcpy(to_eth_hdr->h_source, mac_addr.mac_byte, ETH_ALEN);

  // Add IP header
  to_ip_hdr->saddr = ip_addr;
  to_ip_hdr->daddr = ip_hdr->saddr;
  ip_total_len = sizeof(struct iphdr) + sizeof(struct udphdr) +
                  sizeof(struct MemOp);

  if (type == SIMBRICKS_PROTO_MEM_H2M_MSG_READ){
    ip_total_len += memop->len;
  }
  to_ip_hdr->tot_len = htons(ip_total_len);

  // Add UDP header
  to_udp_hdr->uh_sport = udp_hdr->uh_dport;
  to_udp_hdr->uh_dport = udp_hdr->uh_sport;
  to_udp_hdr->uh_ulen = sizeof(struct udphdr) + sizeof(struct MemOp);
  if (type == SIMBRICKS_PROTO_MEM_H2M_MSG_READ){
    to_udp_hdr->uh_ulen += memop->len;
  }

  packet_to->len = pkt_len;
125

126
127
128
129
130
131
132
133
  // Fill MemOp structure
  to_memop->req_id = memop->req_id;
  to_memop->as_id = memop->as_id;
  to_memop->addr = memop->addr;
  to_memop->len = memop->len;

  switch (type) {
    case SIMBRICKS_PROTO_MEM_H2M_MSG_READ:
Hejing Li's avatar
Hejing Li committed
134
      //printf("received read request\n");
135
136
137
138
139
140
141
142
      // send read complete message
      to_memop->OpType = SIMBRICKS_PROTO_MEM_M2H_MSG_READCOMP;

      memcpy((void *)to_data, &mem_array[memop->addr], memop->len);

      packet_to->len += memop->len;
      break;
    case SIMBRICKS_PROTO_MEM_H2M_MSG_WRITE:
Hejing Li's avatar
Hejing Li committed
143
      //printf("received write request\n");
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
      // write the data in local memory
      memcpy(&mem_array[memop->addr], data, memop->len);

      // send write complete message
      to_memop->OpType = SIMBRICKS_PROTO_MEM_M2H_MSG_WRITECOMP;

      // printf("to_eth_source: ");
      // for (i = 0; i < ETH_ALEN; i++) {
      //   printf("%X: ", to_eth_hdr->h_source[i]);
      // }
      // printf("--> to_eth_dest: ");
      // for (i = 0; i < ETH_ALEN; i++) {
      //   printf("%X: ", to_eth_hdr->h_dest[i]);
      // }
      break;
    default:
      fprintf(stderr, "poll_n2m: unsupported type=%u\n", type);
  }

  SimbricksNetIfOutSend(netif, msg_to, SIMBRICKS_PROTO_NET_MSG_PACKET);
  return 1;

}
167
168
169

void PollN2M(struct SimbricksNetIf *netif, uint64_t cur_ts) {
  
GAO Bin's avatar
GAO Bin committed
170
  volatile union SimbricksProtoNetMsg *msg = SimbricksNetIfInPoll(netif, cur_ts);
171
  
GAO Bin's avatar
GAO Bin committed
172
173
174
  if (msg == NULL){
    return;
  }
175

176
177

  uint8_t type;  
178
  volatile struct SimbricksProtoNetMsgPacket *packet = &msg->packet;
GAO Bin's avatar
GAO Bin committed
179
180
181
182

  type = SimbricksNetIfInType(netif, msg);
  switch (type) {
    case SIMBRICKS_PROTO_NET_MSG_PACKET:
Hejing Li's avatar
Hejing Li committed
183
      //printf("received network packet\n");
184
185
      if (!HandleRequest(netif, packet)){
        return;
186
      }
GAO Bin's avatar
GAO Bin committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
      break;

    case SIMBRICKS_PROTO_MSG_TYPE_SYNC:
      break;

    default:
      fprintf(stderr, "poll_n2m: unsupported type=%u\n", type);
  }

  SimbricksNetIfInDone(netif, msg);
}

int main(int argc, char *argv[]) {
  
201
202
  int asid = 0;

GAO Bin's avatar
GAO Bin committed
203
204
205
  signal(SIGINT, sigint_handler);
  signal(SIGUSR1, sigusr1_handler);

206
  uint64_t next_ts = 0;
GAO Bin's avatar
GAO Bin committed
207
  struct SimbricksBaseIfParams netParams;
208
209
  struct SimbricksNetIf netif;
  const char *shmPath;
GAO Bin's avatar
GAO Bin committed
210
211
212
  
  SimbricksNetIfDefaultParams(&netParams);

213
  if (argc < 7 || argc > 11) {
GAO Bin's avatar
GAO Bin committed
214
    fprintf(stderr,
215
216
            "Usage: netmem [SIZE] [BASE-ADDR] [ASID] [ETH-SOCKET] "
            "[SHM] [MAC-ADDR] [SYNC-MODE] [START-TICK] [SYNC-PERIOD] [ETH-LATENCY]\n");
GAO Bin's avatar
GAO Bin committed
217
218
    return -1;
  }
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
  if (argc >= 9)
     cur_ts = strtoull(argv[8], NULL, 0);
  if (argc >= 10)
    netParams.sync_interval =  strtoull(argv[9], NULL, 0) * 1000ULL;
  if (argc >= 11)
    netParams.link_latency = strtoull(argv[10], NULL, 0) * 1000ULL;

  size = strtoull(argv[1], NULL, 0);
  base_addr = strtoull(argv[2], NULL, 0);
  asid = atoi(argv[3]);
  netParams.sock_path = argv[4];
  shmPath = argv[5];
  mac_addr.mac_64 = strtoull(argv[6], NULL, 16);
  printf("mac_byte: %lx\n", mac_addr.mac_64);
  printf("mac_8: %X:%X:%X:%X:%X:%X\n", mac_addr.mac_byte[0], mac_addr.mac_byte[1],mac_addr.mac_byte[2],mac_addr.mac_byte[3],mac_addr.mac_byte[4],mac_addr.mac_byte[5]);
GAO Bin's avatar
GAO Bin committed
234

235
236
237
  netParams.sync_mode = kSimbricksBaseIfSyncOptional;
  netParams.blocking_conn = false;
  //netif.base.sync = sync_mem;
GAO Bin's avatar
GAO Bin committed
238

239
  mem_array = (uint8_t *) malloc(size * sizeof(uint8_t));
GAO Bin's avatar
GAO Bin committed
240

241
242
243
  if (!mem_array){
    perror("no array allocated\n");
  }
GAO Bin's avatar
GAO Bin committed
244

245
246
247
  size_t shm_size = 0;
  shm_size += netParams.in_num_entries * netParams.in_entries_size;
  shm_size += netParams.out_num_entries * netParams.out_entries_size;
GAO Bin's avatar
GAO Bin committed
248

249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
  std::string shm_path_ = shmPath;
  struct SimbricksBaseIfSHMPool pool_;
  memset(&pool_, 0, sizeof(pool_));
  
  if (SimbricksBaseIfInit(&netif.base, &netParams)){
    perror("Init: SimbricksBaseIfInit failed\n");
    return EXIT_FAILURE;
  }
  
  if (SimbricksBaseIfSHMPoolCreate(&pool_, shm_path_.c_str(), shm_size) !=
      0) {
      perror("NetMemIfInit: SimbricksBaseIfSHMPoolCreate failed");
      return false;
    }


  if (SimbricksBaseIfListen(&netif.base, &pool_)){
    perror("SimbricksBaseIfConnect failed");
    return false;
GAO Bin's avatar
GAO Bin committed
268
  }
269
270
271
272
273
274
275
276
277
278
279
280
281
282
  
  struct SimBricksBaseIfEstablishData ests[1];
  struct SimbricksProtoNetIntro intro;
  ests[0].base_if = &netif.base;
  ests[0].tx_intro = &intro;
  ests[0].tx_intro_len = sizeof(intro);
  ests[0].rx_intro = &intro;
  ests[0].rx_intro_len = sizeof(intro);

  if (SimBricksBaseIfEstablish(ests, 1)) {
    fprintf(stderr, "SimBricksBaseIfEstablish failed\n");
    return false;
  }
  sync_mem = SimbricksBaseIfSyncEnabled(&netif.base);
GAO Bin's avatar
GAO Bin committed
283

284
  printf("start polling\n");
GAO Bin's avatar
GAO Bin committed
285
  while (!exiting){
286
287
    while (SimbricksNetIfOutSync(&netif, cur_ts)) {
        //fprintf(stderr, "warn: SimbricksNetIfSync failed (t=%lu)\n", cur_ts);
GAO Bin's avatar
GAO Bin committed
288
289
290
    }

    do {
291
292
293
294
295
296
      
      PollN2M(&netif, cur_ts);

      if (sync_mem){
        next_ts = SimbricksNetIfInTimestamp(&netif);
      }
GAO Bin's avatar
GAO Bin committed
297

298
    } while (!exiting && next_ts <= cur_ts);
GAO Bin's avatar
GAO Bin committed
299

300
301
    cur_ts = next_ts;

GAO Bin's avatar
GAO Bin committed
302
303
304
  }
  return 0;
}