aboutsummaryrefslogtreecommitdiff
path: root/platform/linux-generic/pktio/netmap.c
blob: 774e8137deb2aa3cb3e7d84ef1aa8abe2a823016 (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
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
297
298
299
300
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/* Copyright (c) 2015, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#ifdef ODP_NETMAP

#include <odp_posix_extensions.h>

#include <odp_packet_netmap.h>
#include <odp_packet_socket.h>
#include <odp_packet_io_internal.h>
#include <odp_debug_internal.h>
#include <odp/helper/eth.h>

#include <sys/ioctl.h>
#include <poll.h>
#include <linux/ethtool.h>
#include <linux/sockios.h>
#include <odp_classification_datamodel.h>
#include <odp_classification_inlines.h>
#include <odp_classification_internal.h>

#define NETMAP_WITH_LIBS
#include <net/netmap_user.h>

static struct nm_desc mmap_desc;	/** Used to store the mmap address;
					  filled in first time, used for
					  subsequent calls to nm_open */

#define NM_OPEN_RETRIES 5
#define NM_INJECT_RETRIES 10

static int netmap_do_ioctl(pktio_entry_t *pktio_entry, unsigned long cmd,
			   int subcmd)
{
	pkt_netmap_t *pkt_nm = &pktio_entry->s.pkt_nm;
	struct ethtool_value eval;
	struct ifreq ifr;
	int err;
	int fd = pkt_nm->sockfd;

	memset(&ifr, 0, sizeof(ifr));
	snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s",
		 pktio_entry->s.name);

	switch (cmd) {
	case SIOCSIFFLAGS:
		ifr.ifr_flags = pkt_nm->if_flags & 0xffff;
		break;
	case SIOCETHTOOL:
		eval.cmd = subcmd;
		eval.data = 0;
		ifr.ifr_data = (caddr_t)&eval;
		break;
	default:
		break;
	}
	err = ioctl(fd, cmd, &ifr);
	if (err)
		goto done;

	switch (cmd) {
	case SIOCGIFFLAGS:
		pkt_nm->if_flags = (ifr.ifr_flags << 16) |
			(0xffff & ifr.ifr_flags);
		break;
	case SIOCETHTOOL:
		if (subcmd == ETHTOOL_GLINK)
			return !eval.data;
		break;
	default:
		break;
	}
done:
	if (err)
		ODP_ERR("ioctl err %d %lu: %s\n", err, cmd, strerror(errno));

	return err;
}

static int netmap_close(pktio_entry_t *pktio_entry)
{
	pkt_netmap_t *pkt_nm = &pktio_entry->s.pkt_nm;

	if (pkt_nm->rx_desc != NULL) {
		nm_close(pkt_nm->rx_desc);
		mmap_desc.mem = NULL;
	}
	if (pkt_nm->tx_desc != NULL)
		nm_close(pkt_nm->tx_desc);

	if (pkt_nm->sockfd != -1 && close(pkt_nm->sockfd) != 0) {
		__odp_errno = errno;
		ODP_ERR("close(sockfd): %s\n", strerror(errno));
		return -1;
	}
	return 0;
}

static int netmap_open(odp_pktio_t id ODP_UNUSED, pktio_entry_t *pktio_entry,
		       const char *netdev, odp_pool_t pool)
{
	char ifname[IFNAMSIZ + 7]; /* netmap:<ifname> */
	int err;
	int sockfd;
	int i;
	pkt_netmap_t *pkt_nm = &pktio_entry->s.pkt_nm;

	if (getenv("ODP_PKTIO_DISABLE_NETMAP"))
		return -1;

	if (pool == ODP_POOL_INVALID)
		return -1;

	/* Init pktio entry */
	memset(pkt_nm, 0, sizeof(*pkt_nm));
	pkt_nm->sockfd = -1;
	pkt_nm->pool = pool;

	/* max frame len taking into account the l2-offset */
	pkt_nm->max_frame_len = ODP_CONFIG_PACKET_BUF_LEN_MAX -
		odp_buffer_pool_headroom(pool) -
		odp_buffer_pool_tailroom(pool);

	snprintf(pktio_entry->s.name, sizeof(pktio_entry->s.name), "%s",
		 netdev);
	snprintf(ifname, sizeof(ifname), "netmap:%s", netdev);

	if (mmap_desc.mem == NULL)
		pkt_nm->rx_desc = nm_open(ifname, NULL, NETMAP_NO_TX_POLL,
					  NULL);
	else
		pkt_nm->rx_desc = nm_open(ifname, NULL, NETMAP_NO_TX_POLL |
					  NM_OPEN_NO_MMAP, &mmap_desc);
	pkt_nm->tx_desc = nm_open(ifname, NULL, NM_OPEN_NO_MMAP, &mmap_desc);

	if (pkt_nm->rx_desc == NULL || pkt_nm->tx_desc == NULL) {
		ODP_ERR("nm_open(%s) failed\n", ifname);
		goto error;
	}

	if (mmap_desc.mem == NULL) {
		mmap_desc.mem = pkt_nm->rx_desc->mem;
		mmap_desc.memsize = pkt_nm->rx_desc->memsize;
	}

	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	if (sockfd == -1) {
		ODP_ERR("Cannot get device control socket\n");
		goto error;
	}
	pkt_nm->sockfd = sockfd;

	err = netmap_do_ioctl(pktio_entry, SIOCGIFFLAGS, 0);
	if (err)
		goto error;
	if ((pkt_nm->if_flags & IFF_UP) == 0)
		ODP_DBG("%s is down\n", pktio_entry->s.name);

	err = mac_addr_get_fd(sockfd, netdev, pkt_nm->if_mac);
	if (err)
		goto error;

	/* Wait for the link to come up */
	for (i = 0; i < NM_OPEN_RETRIES; i++) {
		err = netmap_do_ioctl(pktio_entry, SIOCETHTOOL, ETHTOOL_GLINK);
		/* nm_open() causes the physical link to reset. When using a
		 * direct attached loopback cable there may be a small delay
		 * until the opposing end's interface comes back up again. In
		 * this case without the additional sleep pktio validation
		 * tests fail. */
		sleep(1);
		if (err == 0)
			return 0;
	}
	ODP_ERR("%s didn't come up\n", pktio_entry->s.name);

error:
	netmap_close(pktio_entry);
	return -1;
}

/**
 * Create ODP packet from netmap packet
 *
 * @param pktio_entry    Packet IO handle
 * @param pkt_out        Storage for new ODP packet handle
 * @param buf            Netmap buffer address
 * @param len            Netmap buffer length
 *
 * @retval 0 on success
 * @retval <0 on failure
 */
static inline int netmap_pkt_to_odp(pktio_entry_t *pktio_entry,
				    odp_packet_t *pkt_out, const char *buf,
				    uint16_t len)
{
	odp_packet_t pkt;
	int ret;

	if (odp_unlikely(len > pktio_entry->s.pkt_nm.max_frame_len)) {
		ODP_ERR("RX: frame too big %" PRIu16 " %zu!\n", len,
			pktio_entry->s.pkt_nm.max_frame_len);
		return -1;
	}

	if (odp_unlikely(len < ODPH_ETH_LEN_MIN)) {
		ODP_ERR("RX: Frame truncated: %" PRIu16 "\n", len);
		return -1;
	}

	if (pktio_cls_enabled(pktio_entry)) {
		ret = _odp_packet_cls_enq(pktio_entry, (const uint8_t *)buf,
					  len, pkt_out);
		if (ret)
			return 0;
		return -1;
	} else {
		odp_packet_hdr_t *pkt_hdr;

		pkt = packet_alloc(pktio_entry->s.pkt_nm.pool, len, 1);
		if (pkt == ODP_PACKET_INVALID)
			return -1;

		pkt_hdr = odp_packet_hdr(pkt);

		/* For now copy the data in the mbuf,
		   worry about zero-copy later */
		if (odp_packet_copydata_in(pkt, 0, len, buf) != 0) {
			odp_packet_free(pkt);
			return -1;
		}

		packet_parse_l2(pkt_hdr);
		*pkt_out = pkt;
	}

	return 0;
}

static int netmap_recv(pktio_entry_t *pktio_entry, odp_packet_t pkt_table[],
		       unsigned num)
{
	struct netmap_ring *ring;
	struct nm_desc *desc = pktio_entry->s.pkt_nm.rx_desc;
	struct pollfd polld;
	char *buf;
	int i;
	int num_rings = desc->last_rx_ring - desc->first_rx_ring + 1;
	int ring_id = desc->cur_rx_ring;
	unsigned num_rx = 0;
	uint32_t slot_id;

	polld.fd = desc->fd;
	polld.events = POLLIN;

	for (i = 0; i < num_rings && num_rx != num; i++) {
		ring_id = desc->cur_rx_ring + i;

		if (ring_id > desc->last_rx_ring)
			ring_id = desc->first_rx_ring;

		ring = NETMAP_RXRING(desc->nifp, ring_id);

		while (!nm_ring_empty(ring) && num_rx != num) {
			slot_id = ring->cur;
			buf = NETMAP_BUF(ring, ring->slot[slot_id].buf_idx);

			odp_prefetch(buf);

			if (!netmap_pkt_to_odp(pktio_entry, &pkt_table[num_rx],
					       buf, ring->slot[slot_id].len))
				num_rx++;

			ring->cur = nm_ring_next(ring, slot_id);
			ring->head = ring->cur;
		}
	}
	desc->cur_rx_ring = ring_id;

	if (num_rx == 0) {
		if (odp_unlikely(poll(&polld, 1, 0) < 0))
			ODP_ERR("RX: poll error\n");
	}
	return num_rx;
}

static int netmap_send(pktio_entry_t *pktio_entry, odp_packet_t pkt_table[],
		       unsigned num)
{
	struct pollfd polld;
	struct nm_desc *nm_desc = pktio_entry->s.pkt_nm.tx_desc;
	unsigned i, nb_tx;
	uint8_t *frame;
	uint32_t frame_len;

	polld.fd = nm_desc->fd;
	polld.events = POLLOUT;

	for (nb_tx = 0; nb_tx < num; nb_tx++) {
		frame_len = 0;
		frame = odp_packet_l2_ptr(pkt_table[nb_tx], &frame_len);
		for (i = 0; i < NM_INJECT_RETRIES; i++) {
			if (nm_inject(nm_desc, frame, frame_len) == 0)
				poll(&polld, 1, 0);
			else
				break;
		}
		if (odp_unlikely(i == NM_INJECT_RETRIES)) {
			ioctl(nm_desc->fd, NIOCTXSYNC, NULL);
			break;
		}
	}
	/* Send pending packets */
	poll(&polld, 1, 0);

	for (i = 0; i < nb_tx; i++)
		odp_packet_free(pkt_table[i]);

	return nb_tx;
}

static int netmap_mac_addr_get(pktio_entry_t *pktio_entry, void *mac_addr)
{
	memcpy(mac_addr, pktio_entry->s.pkt_nm.if_mac, ETH_ALEN);
	return ETH_ALEN;
}

static int netmap_mtu_get(pktio_entry_t *pktio_entry)
{
	return mtu_get_fd(pktio_entry->s.pkt_nm.sockfd, pktio_entry->s.name);
}

static int netmap_promisc_mode_set(pktio_entry_t *pktio_entry,
				   odp_bool_t enable)
{
	return promisc_mode_set_fd(pktio_entry->s.pkt_nm.sockfd,
				   pktio_entry->s.name, enable);
}

static int netmap_promisc_mode_get(pktio_entry_t *pktio_entry)
{
	return promisc_mode_get_fd(pktio_entry->s.pkt_nm.sockfd,
				   pktio_entry->s.name);
}

const pktio_if_ops_t netmap_pktio_ops = {
	.init = NULL,
	.term = NULL,
	.open = netmap_open,
	.close = netmap_close,
	.start = NULL,
	.stop = NULL,
	.recv = netmap_recv,
	.send = netmap_send,
	.mtu_get = netmap_mtu_get,
	.promisc_mode_set = netmap_promisc_mode_set,
	.promisc_mode_get = netmap_promisc_mode_get,
	.mac_get = netmap_mac_addr_get
};

#endif /* ODP_NETMAP */