nicbm.cc 12.2 KB
Newer Older
1
2
3
4
5
6
7
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <signal.h>
#include <cassert>
8
9
#include <ctime>
#include <iostream>
10
11
12

#include <nicbm.h>

13
//#define DEBUG_NICBM 1
14

15
#define DMA_MAX_PENDING 64
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36


using namespace nicbm;

static volatile int exiting = 0;

static uint64_t main_time = 0;


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

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

volatile union cosim_pcie_proto_d2h *Runner::d2h_alloc(void)
{
37
38
    volatile union cosim_pcie_proto_d2h *msg;
    while ((msg = nicsim_d2h_alloc(&nsparams, main_time)) == NULL) {
39
40
41
42
43
44
45
        fprintf(stderr, "d2h_alloc: no entry available\n");
    }
    return msg;
}

volatile union cosim_eth_proto_d2n *Runner::d2n_alloc(void)
{
46
47
    volatile union cosim_eth_proto_d2n *msg;
    while ((msg = nicsim_d2n_alloc(&nsparams, main_time)) == NULL) {
48
49
50
51
52
53
        fprintf(stderr, "d2n_alloc: no entry available\n");
    }
    return msg;
}

void Runner::issue_dma(DMAOp &op)
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
{
    if (dma_pending < DMA_MAX_PENDING) {
        // can directly issue
#ifdef DEBUG_NICBM
        printf("nicbm: issuing dma op %p addr %lx len %zu pending %zu\n", &op,
                op.dma_addr, op.len, dma_pending);
#endif
        dma_do(op);
    } else {
#ifdef DEBUG_NICBM
        printf("nicbm: enqueuing dma op %p addr %lx len %zu pending %zu\n", &op,
                op.dma_addr, op.len, dma_pending);
#endif
        dma_queue.push_back(&op);
    }
}

void Runner::dma_trigger()
{
    if (dma_queue.empty() || dma_pending == DMA_MAX_PENDING)
        return;

    DMAOp *op = dma_queue.front();
    dma_queue.pop_front();

    dma_do(*op);
}

void Runner::dma_do(DMAOp &op)
83
84
{
    volatile union cosim_pcie_proto_d2h *msg = d2h_alloc();
85
    dma_pending++;
86
#ifdef DEBUG_NICBM
87
88
    printf("nicbm: executing dma op %p addr %lx len %zu pending %zu\n", &op,
            op.dma_addr, op.len, dma_pending);
89
90
91
92
#endif

    if (op.write) {
        volatile struct cosim_pcie_proto_d2h_write *write = &msg->write;
93
94
95
96
97
98
        if (dintro.d2h_elen < sizeof(*write) + op.len) {
            fprintf(stderr, "issue_dma: write too big (%zu), can only fit up "
                    "to (%zu)\n", op.len, dintro.d2h_elen - sizeof(*write));
            abort();
        }

99
100
101
102
103
104
105
106
107
        write->req_id = (uintptr_t) &op;
        write->offset = op.dma_addr;
        write->len = op.len;
        memcpy((void *)write->data, (void *)op.data, op.len);
        // WMB();
        write->own_type = COSIM_PCIE_PROTO_D2H_MSG_WRITE |
            COSIM_PCIE_PROTO_D2H_OWN_HOST;
    } else {
        volatile struct cosim_pcie_proto_d2h_read *read = &msg->read;
108
109
110
111
112
113
114
115
        if (dintro.h2d_elen < sizeof(struct cosim_pcie_proto_h2d_readcomp) +
                op.len) {
            fprintf(stderr, "issue_dma: write too big (%zu), can only fit up "
                    "to (%zu)\n", op.len, dintro.h2d_elen -
                    sizeof(struct cosim_pcie_proto_h2d_readcomp));
            abort();
        }

116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
        read->req_id = (uintptr_t) &op;
        read->offset = op.dma_addr;
        read->len = op.len;
        // WMB();
        read->own_type = COSIM_PCIE_PROTO_D2H_MSG_READ |
            COSIM_PCIE_PROTO_D2H_OWN_HOST;
    }
}

void Runner::msi_issue(uint8_t vec)
{
    volatile union cosim_pcie_proto_d2h *msg = d2h_alloc();
#ifdef DEBUG_NICBM
    printf("nicbm: issue MSI interrupt vec %u\n", vec);
#endif
    volatile struct cosim_pcie_proto_d2h_interrupt *intr = &msg->interrupt;
    intr->vector = vec;
    intr->inttype = COSIM_PCIE_PROTO_INT_MSI;

    // WMB();
    intr->own_type = COSIM_PCIE_PROTO_D2H_MSG_INTERRUPT |
Antoine Kaufmann's avatar
Antoine Kaufmann committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
        COSIM_PCIE_PROTO_D2H_OWN_HOST;
}

void Runner::msix_issue(uint8_t vec)
{
    volatile union cosim_pcie_proto_d2h *msg = d2h_alloc();
#ifdef DEBUG_NICBM
    printf("nicbm: issue MSI-X interrupt vec %u\n", vec);
#endif
    volatile struct cosim_pcie_proto_d2h_interrupt *intr = &msg->interrupt;
    intr->vector = vec;
    intr->inttype = COSIM_PCIE_PROTO_INT_MSIX;

    // WMB();
    intr->own_type = COSIM_PCIE_PROTO_D2H_MSG_INTERRUPT |
152
153
154
        COSIM_PCIE_PROTO_D2H_OWN_HOST;
}

155
156
157
158
159
160
161
162
163
164
void Runner::event_schedule(TimedEvent &evt)
{
    events.insert(&evt);
}

void Runner::event_cancel(TimedEvent &evt)
{
    events.erase(&evt);
}

165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
void Runner::h2d_read(volatile struct cosim_pcie_proto_h2d_read *read)
{
    volatile union cosim_pcie_proto_d2h *msg;
    volatile struct cosim_pcie_proto_d2h_readcomp *rc;

    msg = d2h_alloc();
    rc = &msg->readcomp;

    dev.reg_read(read->bar, read->offset, (void *) rc->data, read->len);
    rc->req_id = read->req_id;

#ifdef DEBUG_NICBM
    uint64_t dbg_val = 0;
    memcpy(&dbg_val, (const void *) rc->data, read->len <= 8 ? read->len : 8);
    printf("nicbm: read(off=0x%lx, len=%u, val=0x%lx)\n", read->offset,
            read->len, dbg_val);
#endif

    //WMB();
    rc->own_type = COSIM_PCIE_PROTO_D2H_MSG_READCOMP |
        COSIM_PCIE_PROTO_D2H_OWN_HOST;
}

void Runner::h2d_write(volatile struct cosim_pcie_proto_h2d_write *write)
{
    volatile union cosim_pcie_proto_d2h *msg;
    volatile struct cosim_pcie_proto_d2h_writecomp *wc;

    msg = d2h_alloc();
    wc = &msg->writecomp;

#ifdef DEBUG_NICBM
    uint64_t dbg_val = 0;
    memcpy(&dbg_val, (const void *) write->data, write->len <= 8 ? write->len : 8);
    printf("nicbm: write(off=0x%lx, len=%u, val=0x%lx)\n", write->offset,
            write->len, dbg_val);
#endif
    dev.reg_write(write->bar, write->offset, (void *) write->data, write->len);
    wc->req_id = write->req_id;

    //WMB();
    wc->own_type = COSIM_PCIE_PROTO_D2H_MSG_WRITECOMP |
        COSIM_PCIE_PROTO_D2H_OWN_HOST;
}

void Runner::h2d_readcomp(volatile struct cosim_pcie_proto_h2d_readcomp *rc)
{
    DMAOp *op = (DMAOp *)(uintptr_t)rc->req_id;

#ifdef DEBUG_NICBM
    printf("nicbm: completed dma read op %p addr %lx len %zu\n", op,
            op->dma_addr, op->len);
#endif

    memcpy(op->data, (void *)rc->data, op->len);
    dev.dma_complete(*op);
221
222
223

    dma_pending--;
    dma_trigger();
224
225
226
227
228
229
230
231
232
233
234
235
}

void Runner::h2d_writecomp(volatile struct cosim_pcie_proto_h2d_writecomp *wc)
{
    DMAOp *op = (DMAOp *)(uintptr_t)wc->req_id;

#ifdef DEBUG_NICBM
    printf("nicbm: completed dma write op %p addr %lx len %zu\n", op,
            op->dma_addr, op->len);
#endif

    dev.dma_complete(*op);
236
237
238

    dma_pending--;
    dma_trigger();
239
240
}

241
242
243
244
245
void Runner::h2d_devctrl(volatile struct cosim_pcie_proto_h2d_devctrl *dc)
{
    dev.devctrl_update(*(struct cosim_pcie_proto_h2d_devctrl *) dc);
}

246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
void Runner::eth_recv(volatile struct cosim_eth_proto_n2d_recv *recv)
{
#ifdef DEBUG_NICBM
    printf("nicbm: eth rx: port %u len %u\n", recv->port, recv->len);
#endif

    dev.eth_rx(recv->port, (void *) recv->data, recv->len);
}

void Runner::eth_send(const void *data, size_t len)
{
#ifdef DEBUG_NICBM
    printf("nicbm: eth tx: len %zu\n", len);
#endif

    volatile union cosim_eth_proto_d2n *msg = d2n_alloc();
    volatile struct cosim_eth_proto_d2n_send *send = &msg->send;
    send->port = 0; // single port
    send->len = len;
    memcpy((void *)send->data, data, len);
    send->own_type = COSIM_ETH_PROTO_D2N_MSG_SEND |
        COSIM_ETH_PROTO_D2N_OWN_NET;
}

void Runner::poll_h2d()
{
    volatile union cosim_pcie_proto_h2d *msg =
        nicif_h2d_poll(&nsparams, main_time);
    uint8_t type;

    if (msg == NULL)
        return;

    type = msg->dummy.own_type & COSIM_PCIE_PROTO_H2D_MSG_MASK;
    switch (type) {
        case COSIM_PCIE_PROTO_H2D_MSG_READ:
            h2d_read(&msg->read);
            break;

        case COSIM_PCIE_PROTO_H2D_MSG_WRITE:
            h2d_write(&msg->write);
            break;

        case COSIM_PCIE_PROTO_H2D_MSG_READCOMP:
            h2d_readcomp(&msg->readcomp);
            break;

        case COSIM_PCIE_PROTO_H2D_MSG_WRITECOMP:
            h2d_writecomp(&msg->writecomp);
            break;

297
298
299
300
        case COSIM_PCIE_PROTO_H2D_MSG_DEVCTRL:
            h2d_devctrl(&msg->devctrl);
            break;

301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
        case COSIM_PCIE_PROTO_H2D_MSG_SYNC:
            break;

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

    nicif_h2d_done(msg);
    nicif_h2d_next();
}

void Runner::poll_n2d()
{
    volatile union cosim_eth_proto_n2d *msg =
        nicif_n2d_poll(&nsparams, main_time);
    uint8_t t;

    if (msg == NULL)
        return;

    t = msg->dummy.own_type & COSIM_ETH_PROTO_N2D_MSG_MASK;
    switch (t) {
        case COSIM_ETH_PROTO_N2D_MSG_RECV:
            eth_recv(&msg->recv);
            break;

        case COSIM_ETH_PROTO_N2D_MSG_SYNC:
            break;

        default:
            fprintf(stderr, "poll_n2d: unsupported type=%u", t);
    }

    nicif_n2d_done(msg);
    nicif_n2d_next();
}

338
339
340
341
342
uint64_t Runner::time_ps() const
{
    return main_time;
}

343
344
345
346
347
uint64_t Runner::get_mac_addr() const
{
    return mac_addr;
}

348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
bool Runner::event_next(uint64_t &retval)
{
    if (events.empty())
        return false;

    retval = (*events.begin())->time;
    return true;
}

void Runner::event_trigger()
{
    auto it = events.begin();
    if (it == events.end())
        return;

    TimedEvent *ev = *it;

    // event is in the future
    if (ev->time > main_time)
        return;

    events.erase(it);
    dev.timed_event(*ev);
}

373
Runner::Runner(Device &dev_)
374
    : dev(dev_), events(event_cmp())
375
{
376
    //mac_addr = lrand48() & ~(3ULL << 46);
377
    dma_pending = 0;
378
379
380
381
    srand48(time(NULL) ^ getpid());
    mac_addr = lrand48();
    mac_addr <<= 16;
    mac_addr ^= lrand48();
382
    mac_addr &= ~3ULL;
383
384

    std::cerr << std::hex << mac_addr << std::endl;
385
386
387
388
389
}

int Runner::runMain(int argc, char *argv[])
{
    uint64_t next_ts;
390
    uint64_t max_step = 10000;
391
392
393
    uint64_t sync_period = 100 * 1000ULL;
    uint64_t pci_latency = 500 * 1000ULL;
    uint64_t eth_latency = 500 * 1000ULL;
394
    int sync_mode = SYNC_MODES;
395

396
    if (argc < 4 && argc > 9) {
397
        fprintf(stderr, "Usage: corundum_bm PCI-SOCKET ETH-SOCKET "
398
                "SHM [SYNC-MODE] [START-TICK] [SYNC-PERIOD] [PCI-LATENCY] [ETH-LATENCY]\n");
399
400
        return EXIT_FAILURE;
    }
401
    if (argc >= 5)
402
        sync_mode = strtol(argv[4], NULL, 0);
403
    if (argc >= 6)
404
        main_time = strtoull(argv[5], NULL, 0);
405
    if (argc >= 7)
406
        sync_period = strtoull(argv[6], NULL, 0) * 1000ULL;
407
    if (argc >= 8)
408
409
410
        pci_latency = strtoull(argv[7], NULL, 0) * 1000ULL;
    if (argc >= 9)
        eth_latency = strtoull(argv[8], NULL, 0) * 1000ULL;
411
412
413
414

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

415
416
    memset(&dintro, 0, sizeof(dintro));
    dev.setup_intro(dintro);
417
418
419
420
421
422

    nsparams.sync_pci = 1;
    nsparams.sync_eth = 1;
    nsparams.pci_socket_path = argv[1];
    nsparams.eth_socket_path = argv[2];
    nsparams.shm_path = argv[3];
423
424
425
    nsparams.pci_latency = pci_latency;
    nsparams.eth_latency = eth_latency;
    nsparams.sync_delay = sync_period;
426
427
428
    assert(sync_mode == SYNC_MODES || sync_mode == SYNC_BARRIER);
    nsparams.sync_mode = sync_mode;

429
    if (nicsim_init(&nsparams, &dintro)) {
430
431
432
433
434
        return EXIT_FAILURE;
    }
    fprintf(stderr, "sync_pci=%d sync_eth=%d\n", nsparams.sync_pci,
        nsparams.sync_eth);

435
436
    bool is_sync = nsparams.sync_pci || nsparams.sync_eth;

437
438
439
440
    while (!exiting) {
        while (nicsim_sync(&nsparams, main_time)) {
            fprintf(stderr, "warn: nicsim_sync failed (t=%lu)\n", main_time);
        }
441
        nicsim_advance_epoch(&nsparams, main_time);
442
443
444
445

        do {
            poll_h2d();
            poll_n2d();
446
447
448
            event_trigger();

            if (is_sync) {
449
                next_ts = nicsim_next_timestamp(&nsparams);
450
451
                if (next_ts > main_time + max_step)
                    next_ts = main_time + max_step;
452
            } else {
453
                next_ts = main_time + max_step;
454
455
456
457
458
459
460
            }

            uint64_t ev_ts;
            if (event_next(ev_ts) && ev_ts < next_ts)
                next_ts = ev_ts;

        } while (next_ts <= main_time && !exiting);
461
        main_time = nicsim_advance_time(&nsparams, next_ts);
462
463
464
465
466
467
    }

    fprintf(stderr, "exit main_time: %lu\n", main_time);
    nicsim_cleanup();
    return 0;
}
468
469
470
471

void Runner::Device::timed_event(TimedEvent &te)
{
}
472
473
474
475
476
477
478
479

void Runner::Device::devctrl_update(
        struct cosim_pcie_proto_h2d_devctrl &devctrl)
{
    int_intx_en = devctrl.flags & COSIM_PCIE_PROTO_CTRL_INTX_EN;
    int_msi_en = devctrl.flags & COSIM_PCIE_PROTO_CTRL_MSI_EN;
    int_msix_en = devctrl.flags & COSIM_PCIE_PROTO_CTRL_MSIX_EN;
}