-
Notifications
You must be signed in to change notification settings - Fork 0
/
netmap.c
137 lines (123 loc) · 3.13 KB
/
netmap.c
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "subr.h"
#include "global.h"
#define NETMAP_WITH_LIBS
#include <net/netmap_user.h>
static struct netmap_ring *
not_empty_txr(struct netmap_slot **pslot)
{
int i;
struct netmap_ring *txr;
if (multiplexer_get_events(0) & POLLOUT) {
return NULL;
}
for (i = current->t_nmd->first_tx_ring; i <= current->t_nmd->last_tx_ring; ++i) {
txr = NETMAP_TXRING(current->t_nmd->nifp, i);
if (!nm_ring_empty(txr)) {
if (pslot != NULL) {
*pslot = txr->slot + txr->cur;
(*pslot)->len = 0;
}
return txr;
}
}
multiplexer_pollout(0);
return NULL;
}
static void
netmap_init_if(struct thread *t)
{
char buf[IFNAMSIZ + 64];
if (t->t_rss_queue_id < RSS_QUEUE_ID_MAX) {
snprintf(buf, sizeof(buf), "netmap:%s-%d", t->t_ifname, t->t_rss_queue_id);
} else {
snprintf(buf, sizeof(buf), "netmap:%s", t->t_ifname);
}
t->t_nmd = nm_open(buf, NULL, 0, NULL);
if (t->t_nmd == NULL) {
panic(errno, "nm_open('%s') failed", buf);
}
if (t->t_nmd->req.nr_rx_rings != t->t_nmd->req.nr_tx_rings) {
panic(0, "%s: nr_rx_rings != nr_tx_rings", buf);
}
t->t_rss_queue_num = t->t_nmd->req.nr_rx_rings;
if (t->t_rss_queue_num > 1) {
t->t_rss_key_size = read_rss_key(t->t_ifname, &t->t_rss_key);
}
if ((t->t_nmd->req.nr_flags & NR_REG_MASK) == NR_REG_ONE_NIC) {
t->t_rss_queue_id = t->t_nmd->first_rx_ring;
}
strzcpy(t->t_ifname, t->t_nmd->req.nr_name, sizeof(t->t_ifname));
multiplexer_add(t, t->t_nmd->fd);
}
static void
netmap_init(struct thread *threads, int n_thread)
{
int i;
for (i = 0; i < n_threads; ++i) {
netmap_init_if(threads + i);
}
}
bool
netmap_is_tx_throttled(void)
{
return not_empty_txr(NULL) == NULL;
}
void
netmap_init_tx_packet(struct packet *pkt)
{
pkt->pkt.txr = not_empty_txr(&pkt->pkt.slot);
if (pkt->pkt.txr == NULL) {
pkt->pkt.buf = pkt->pkt_body;
} else {
pkt->pkt.buf = (u_char *)NETMAP_BUF(pkt->pkt.txr, pkt->pkt.slot->buf_idx);
}
pkt->pkt.len = 0;
}
bool
netmap_tx_packet(struct packet *pkt)
{
u_char *buf;
struct netmap_ring *txr;
if (pkt->pkt.txr == NULL) {
pkt->pkt.txr = not_empty_txr(&pkt->pkt.slot);
if (pkt->pkt.txr == NULL) {
add_pending_packet(pkt);
return false;
}
buf = (u_char *)NETMAP_BUF(pkt->pkt.txr, pkt->pkt.slot->buf_idx);
memcpy(buf, pkt->pkt.buf, pkt->pkt.len);
pkt->pkt.buf = buf;
}
assert(pkt->pkt.len);
pkt->pkt.slot->len = pkt->pkt.len;
txr = pkt->pkt.txr;
txr->head = txr->cur = nm_ring_next(txr, txr->cur);
return true;
}
int
netmap_rx(int queue_id)
{
int i, j, n, accum;
struct netmap_slot *slot;
struct netmap_ring *rxr;
accum = 0;
for (i = current->t_nmd->first_rx_ring; i <= current->t_nmd->last_rx_ring; ++i) {
rxr = NETMAP_RXRING(current->t_nmd->nifp, i);
n = nm_ring_space(rxr);
for (j = 0; j < n; ++j) {
DEV_PREFETCH(rxr);
slot = rxr->slot + rxr->cur;
io_process(NETMAP_BUF(rxr, slot->buf_idx) , slot->len);
rxr->head = rxr->cur = nm_ring_next(rxr, rxr->cur);
}
accum += n;
}
return accum;
}
struct transport_ops netmap_ops = {
.tr_io_init_op = netmap_init,
.tr_io_is_tx_throttled_op = netmap_is_tx_throttled,
.tr_io_init_tx_packet_op = netmap_init_tx_packet,
.tr_io_tx_packet_op = netmap_tx_packet,
.tr_io_rx_op = netmap_rx,
};