-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathvirtio-net.c
752 lines (611 loc) · 22.8 KB
/
virtio-net.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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
/****************************************************************************
* drivers/virtio/virtio-net.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <debug.h>
#include <errno.h>
#include <stdint.h>
#include <string.h>
#include <nuttx/compiler.h>
#include <nuttx/kmalloc.h>
#include <nuttx/net/ip.h>
#include <nuttx/net/netdev_lowerhalf.h>
#include <nuttx/virtio/virtio.h>
#include <nuttx/net/wifi_sim.h>
#include "virtio-net.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Virtio net feature bits */
#define VIRTIO_NET_F_MAC 5
/* Virtio net header size and packet buffer size */
#define VIRTIO_NET_HDRSIZE (sizeof(struct virtio_net_hdr_s))
#define VIRTIO_NET_LLHDRSIZE (sizeof(struct virtio_net_llhdr_s))
#define VIRTIO_NET_BUFSIZE (CONFIG_NET_ETH_PKTSIZE + CONFIG_NET_GUARDSIZE)
/* Virtio net virtqueue index and number */
#define VIRTIO_NET_RX 0
#define VIRTIO_NET_TX 1
#define VIRTIO_NET_NUM 2
#define VIRTIO_NET_MAX_PKT_SIZE \
((CONFIG_NET_LL_GUARDSIZE - ETH_HDRLEN) + VIRTIO_NET_BUFSIZE)
#define VIRTIO_NET_MAX_NIOB \
((VIRTIO_NET_MAX_PKT_SIZE + CONFIG_IOB_BUFSIZE - 1) / CONFIG_IOB_BUFSIZE)
/****************************************************************************
* Private Types
****************************************************************************/
/* Virtio net header, just define it here for further, now only use it
* to calculate the virto net header size, see marco VIRTIO_NET_HDRSIZE
*/
begin_packed_struct struct virtio_net_hdr_s
{
uint8_t flags;
uint8_t gso_type;
uint16_t hdr_len;
uint16_t gso_size;
uint16_t csum_start;
uint16_t csum_offset;
} end_packed_struct;
/* The definition of the struct virtio_net_config refers to the link
* https://docs.oasis-open.org/virtio/virtio/v1.2/cs01/
* virtio-v1.2-cs01.html#x1-2230004.
*/
begin_packed_struct struct virtio_net_config_s
{
uint8_t mac[IFHWADDRLEN]; /* VIRTIO_NET_F_MAC */
uint16_t status; /* VIRTIO_NET_F_STATUS */
uint16_t max_virtqueue_pairs; /* VIRTIO_NET_F_MQ */
uint16_t mtu; /* VIRTIO_NET_F_MTU */
uint32_t speed; /* VIRTIO_NET_F_SPEED_DUPLEX */
uint8_t duplex;
uint8_t rss_max_key_size; /* VIRTIO_NET_F_RSS */
uint16_t rss_max_indirection_table_length;
uint32_t supported_hash_types;
} end_packed_struct;
struct virtio_net_priv_s
{
#ifdef CONFIG_DRIVERS_WIFI_SIM
/* wifi device information, which includes the netdev lowerhalf */
struct wifi_sim_lowerhalf_s lower;
#else
/* This holds the information visible to the NuttX network */
struct netdev_lowerhalf_s lower; /* The netdev lowerhalf */
#endif
spinlock_t lock[VIRTIO_NET_NUM];
/* Virtio device information */
FAR struct virtio_device *vdev; /* Virtio device pointer */
int bufnum; /* TX and RX Buffer number */
};
/* Virtio Link Layer Header, follow shows the iob buffer layout:
*
* |<-- CONFIG_NET_LL_GUARDSIZE -->|
* +---------------+---------------+------------+------+ +-------------+
* | Virtio Header | ETH Header | data | free | --> | next netpkt |
* +---------------+---------------+------------+------+ +-------------+
* | |<--------- datalen -------->|
* ^base ^data
*
* CONFIG_NET_LL_GUARDSIZE >= VIRTIO_NET_LLHDRSIZE + ETH_HDR_SIZE
* = sizeof(uintptr) + 10 + 14
* = 32 (64-Bit)
* = 28 (32-Bit)
*/
begin_packed_struct struct virtio_net_llhdr_s
{
FAR netpkt_t *pkt; /* Netpaket pointer */
struct virtio_net_hdr_s vhdr; /* Virtio net header */
} end_packed_struct;
static_assert(CONFIG_NET_LL_GUARDSIZE >= VIRTIO_NET_LLHDRSIZE + ETH_HDRLEN,
"CONFIG_NET_LL_GUARDSIZE cannot be less than ETH_HDRLEN"
" + VIRTIO_NET_LLHDRSIZE");
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static int virtio_net_ifup(FAR struct netdev_lowerhalf_s *dev);
static int virtio_net_ifdown(FAR struct netdev_lowerhalf_s *dev);
static int virtio_net_send(FAR struct netdev_lowerhalf_s *dev,
FAR netpkt_t *pkt);
static netpkt_t *virtio_net_recv(FAR struct netdev_lowerhalf_s *dev);
#ifdef CONFIG_NET_MCASTGROUP
static int virtio_net_addmac(FAR struct netdev_lowerhalf_s *dev,
FAR const uint8_t *mac);
static int virtio_net_rmmac(FAR struct netdev_lowerhalf_s *dev,
FAR const uint8_t *mac);
#endif
#ifdef CONFIG_NETDEV_IOCTL
static int virtio_net_ioctl(FAR struct netdev_lowerhalf_s *dev,
int cmd, unsigned long arg);
#endif
static void virtio_net_txfree(FAR struct netdev_lowerhalf_s *dev);
static int virtio_net_probe(FAR struct virtio_device *vdev);
static void virtio_net_remove(FAR struct virtio_device *vdev);
/****************************************************************************
* Private Data
****************************************************************************/
static struct virtio_driver g_virtio_net_driver =
{
LIST_INITIAL_VALUE(g_virtio_net_driver.node), /* node */
VIRTIO_ID_NETWORK, /* device id */
virtio_net_probe, /* probe */
virtio_net_remove, /* remove */
};
static const struct netdev_ops_s g_virtio_net_ops =
{
virtio_net_ifup,
virtio_net_ifdown,
virtio_net_send,
virtio_net_recv,
#ifdef CONFIG_NET_MCASTGROUP
virtio_net_addmac,
virtio_net_rmmac,
#endif
#ifdef CONFIG_NETDEV_IOCTL
virtio_net_ioctl,
#endif
virtio_net_txfree
};
#ifdef CONFIG_DRIVERS_WIFI_SIM
static uint8_t g_netdev_num = 0;
#endif
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: virtio_net_addbuffer
****************************************************************************/
static int virtio_net_addbuffer(FAR struct netdev_lowerhalf_s *dev,
FAR struct virtqueue *vq, FAR netpkt_t *pkt,
unsigned int vq_id)
{
FAR struct virtio_net_priv_s *priv = (FAR struct virtio_net_priv_s *)dev;
FAR struct virtio_net_llhdr_s *hdr;
struct virtqueue_buf vb[VIRTIO_NET_MAX_NIOB + 1];
struct iovec iov[VIRTIO_NET_MAX_NIOB];
int iov_cnt;
int i;
/* Convert netpkt to virtqueue_buf */
iov_cnt = netpkt_to_iov(dev, pkt, iov, VIRTIO_NET_MAX_NIOB);
/* Alloc cookie and net header from transport layer */
hdr = (FAR struct virtio_net_llhdr_s *)
((FAR uint8_t *)iov[0].iov_base - VIRTIO_NET_LLHDRSIZE);
DEBUGASSERT((FAR uint8_t *)hdr >= netpkt_getbase(pkt));
memset(&hdr->vhdr, 0, sizeof(hdr->vhdr));
hdr->pkt = pkt;
/* Prepare buffers depends on the feature VIRTIO_F_ANY_LAYOUT */
if (virtio_has_feature(priv->vdev, VIRTIO_F_ANY_LAYOUT))
{
/* Append the virtio net header to the first buffer */
vb[0].buf = &hdr->vhdr;
vb[0].len = iov[0].iov_len + VIRTIO_NET_HDRSIZE;
#if VIRTIO_NET_MAX_NIOB > 1
for (i = 1; i < iov_cnt; i++)
{
vb[i].buf = iov[i].iov_base;
vb[i].len = iov[i].iov_len;
}
#endif
}
else
{
/* Buffer 0 is only for virtio net header */
vb[0].buf = &hdr->vhdr;
vb[0].len = VIRTIO_NET_HDRSIZE;
for (i = 0; i < iov_cnt; i++)
{
vb[i + 1].buf = iov[i].iov_base;
vb[i + 1].len = iov[i].iov_len;
}
iov_cnt++;
}
vrtinfo("Fill vq=%u, hdr=%p, count=%d\n", vq_id, hdr, iov_cnt);
if (vq_id == VIRTIO_NET_RX)
{
return virtqueue_add_buffer_lock(vq, vb, 0, iov_cnt, hdr,
&priv->lock[vq_id]);
}
else
{
return virtqueue_add_buffer_lock(vq, vb, iov_cnt, 0, hdr,
&priv->lock[vq_id]);
}
}
/****************************************************************************
* Name: virtio_net_rxfill
****************************************************************************/
static void virtio_net_rxfill(FAR struct netdev_lowerhalf_s *dev)
{
FAR struct virtio_net_priv_s *priv = (FAR struct virtio_net_priv_s *)dev;
FAR struct virtqueue *vq = priv->vdev->vrings_info[VIRTIO_NET_RX].vq;
FAR netpkt_t *pkt;
int i;
for (i = 0; i < priv->bufnum; i++)
{
/* IOB Offload, Alloc buffer from RX netpkt */
pkt = netpkt_alloc(dev, NETPKT_RX);
if (pkt == NULL)
{
vrtinfo("Has ran out of the RX buffer, i=%d\n", i);
break;
}
/* Preserve data length */
if (netpkt_setdatalen(dev, pkt, VIRTIO_NET_BUFSIZE) <
VIRTIO_NET_BUFSIZE)
{
vrtwarn("No enough buffer to prepare RX buffer, i=%d\n", i);
netpkt_free(dev, pkt, NETPKT_RX);
break;
}
/* Add buffer to RX virtqueue */
virtio_net_addbuffer(dev, vq, pkt, VIRTIO_NET_RX);
}
if (i > 0)
{
virtqueue_kick_lock(vq, &priv->lock[VIRTIO_NET_RX]);
}
}
/****************************************************************************
* Name: virtio_net_txfree
****************************************************************************/
static void virtio_net_txfree(FAR struct netdev_lowerhalf_s *dev)
{
FAR struct virtio_net_priv_s *priv = (FAR struct virtio_net_priv_s *)dev;
FAR struct virtqueue *vq = priv->vdev->vrings_info[VIRTIO_NET_TX].vq;
FAR struct virtio_net_llhdr_s *hdr;
while (1)
{
/* Get buffer from tx virtqueue */
hdr = virtqueue_get_buffer_lock(vq, NULL, NULL,
&priv->lock[VIRTIO_NET_TX]);
if (hdr == NULL)
{
break;
}
netpkt_free(dev, hdr->pkt, NETPKT_TX);
vrtinfo("Free, hdr: %p, pkt: %p\n", hdr, hdr->pkt);
}
}
/****************************************************************************
* Name: virtio_net_ifup
****************************************************************************/
static int virtio_net_ifup(FAR struct netdev_lowerhalf_s *dev)
{
FAR struct virtio_net_priv_s *priv = (FAR struct virtio_net_priv_s *)dev;
#ifdef CONFIG_NET_IPv4
vrtinfo("Bringing up: %u.%u.%u.%u\n",
ip4_addr1(dev->netdev.d_ipaddr), ip4_addr2(dev->netdev.d_ipaddr),
ip4_addr3(dev->netdev.d_ipaddr), ip4_addr4(dev->netdev.d_ipaddr));
#endif
#ifdef CONFIG_NET_IPv6
vrtinfo("Bringing up: %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
dev->netdev.d_ipv6addr[0], dev->netdev.d_ipv6addr[1],
dev->netdev.d_ipv6addr[2], dev->netdev.d_ipv6addr[3],
dev->netdev.d_ipv6addr[4], dev->netdev.d_ipv6addr[5],
dev->netdev.d_ipv6addr[6], dev->netdev.d_ipv6addr[7]);
#endif
/* Prepare interrupt and packets for receiving */
virtqueue_enable_cb_lock(priv->vdev->vrings_info[VIRTIO_NET_RX].vq,
&priv->lock[VIRTIO_NET_RX]);
virtio_net_rxfill(dev);
#ifdef CONFIG_DRIVERS_WIFI_SIM
if (priv->lower.wifi == NULL)
#endif
{
netdev_lower_carrier_on(dev);
}
return OK;
}
/****************************************************************************
* Name: virtio_net_ifdown
****************************************************************************/
static int virtio_net_ifdown(FAR struct netdev_lowerhalf_s *dev)
{
FAR struct virtio_net_priv_s *priv = (FAR struct virtio_net_priv_s *)dev;
int i;
/* Disable the Ethernet interrupt */
for (i = 0; i < VIRTIO_NET_NUM; i++)
{
virtqueue_disable_cb_lock(priv->vdev->vrings_info[i].vq,
&priv->lock[i]);
}
#ifdef CONFIG_DRIVERS_WIFI_SIM
if (priv->lower.wifi)
{
return dev->iw_ops->disconnect(dev);
}
else
#endif
{
netdev_lower_carrier_off(dev);
return OK;
}
}
/****************************************************************************
* Name: virtio_net_send
****************************************************************************/
static int virtio_net_send(FAR struct netdev_lowerhalf_s *dev,
FAR netpkt_t *pkt)
{
FAR struct virtio_net_priv_s *priv = (FAR struct virtio_net_priv_s *)dev;
FAR struct virtqueue *vq = priv->vdev->vrings_info[VIRTIO_NET_TX].vq;
/* Check the send length */
if (netpkt_getdatalen(dev, pkt) > VIRTIO_NET_BUFSIZE)
{
vrterr("net send buffer too large\n");
return -EINVAL;
}
/* Add buffer to vq and notify the other side */
virtio_net_addbuffer(dev, vq, pkt, VIRTIO_NET_TX);
virtqueue_kick_lock(vq, &priv->lock[VIRTIO_NET_TX]);
/* Try return Netpkt TX buffer to upper-half. */
virtio_net_txfree(dev);
/* If we have no buffer left, enable TX done callback. */
if (netdev_lower_quota_load(dev, NETPKT_TX) <= 0)
{
virtqueue_enable_cb_lock(vq, &priv->lock[VIRTIO_NET_TX]);
}
return OK;
}
/****************************************************************************
* Name: virtio_net_recv
****************************************************************************/
static netpkt_t *virtio_net_recv(FAR struct netdev_lowerhalf_s *dev)
{
FAR struct virtio_net_priv_s *priv = (FAR struct virtio_net_priv_s *)dev;
FAR struct virtqueue *vq = priv->vdev->vrings_info[VIRTIO_NET_RX].vq;
FAR struct virtio_net_llhdr_s *hdr;
irqstate_t flags;
uint32_t len;
/* Fill the free Netpkt RX buffer to the RX virtqueue */
virtio_net_rxfill(dev);
/* Get received buffer form RX virtqueue */
flags = spin_lock_irqsave(&priv->lock[VIRTIO_NET_RX]);
hdr = virtqueue_get_buffer(vq, &len, NULL);
if (hdr == NULL)
{
/* If we have no buffer left, enable RX callback. */
virtqueue_enable_cb(vq);
spin_unlock_irqrestore(&priv->lock[VIRTIO_NET_RX], flags);
vrtinfo("get NULL buffer\n");
return NULL;
}
else
{
spin_unlock_irqrestore(&priv->lock[VIRTIO_NET_RX], flags);
}
/* Set the received pkt length */
netpkt_setdatalen(dev, hdr->pkt, len - VIRTIO_NET_HDRSIZE);
vrtinfo("Recv, hdr=%p, pkt=%p, len=%" PRIu32 "\n", hdr, hdr->pkt, len);
return hdr->pkt;
}
#ifdef CONFIG_NET_MCASTGROUP
/****************************************************************************
* Name: virtio_net_addmac
****************************************************************************/
static int virtio_net_addmac(FAR struct netdev_lowerhalf_s *dev,
FAR const uint8_t *mac)
{
return -ENOSYS;
}
/****************************************************************************
* Name: virtio_net_rmmac
****************************************************************************/
static int virtio_net_rmmac(FAR struct netdev_lowerhalf_s *dev,
FAR const uint8_t *mac)
{
return -ENOSYS;
}
#endif
#ifdef CONFIG_NETDEV_IOCTL
/****************************************************************************
* Name: virtio_net_ioctl
****************************************************************************/
static int virtio_net_ioctl(FAR struct netdev_lowerhalf_s *dev,
int cmd, unsigned long arg)
{
return -ENOTTY;
}
#endif
/****************************************************************************
* Name: virtio_net_rxready
****************************************************************************/
static void virtio_net_rxready(FAR struct virtqueue *vq)
{
FAR struct virtio_net_priv_s *priv = vq->vq_dev->priv;
virtqueue_disable_cb_lock(vq, &priv->lock[VIRTIO_NET_RX]);
netdev_lower_rxready((FAR struct netdev_lowerhalf_s *)priv);
}
/****************************************************************************
* Name: virtio_net_txdone
****************************************************************************/
static void virtio_net_txdone(FAR struct virtqueue *vq)
{
FAR struct virtio_net_priv_s *priv = vq->vq_dev->priv;
virtqueue_disable_cb_lock(vq, &priv->lock[VIRTIO_NET_TX]);
netdev_lower_txdone((FAR struct netdev_lowerhalf_s *)priv);
}
/****************************************************************************
* Name: virtio_net_init
****************************************************************************/
static int virtio_net_init(FAR struct virtio_net_priv_s *priv,
FAR struct virtio_device *vdev)
{
FAR const char *vqnames[VIRTIO_NET_NUM];
vq_callback callbacks[VIRTIO_NET_NUM];
int ret;
spin_lock_init(&priv->lock[VIRTIO_NET_RX]);
spin_lock_init(&priv->lock[VIRTIO_NET_TX]);
priv->vdev = vdev;
vdev->priv = priv;
/* Initialize the virtio device */
virtio_set_status(vdev, VIRTIO_CONFIG_STATUS_DRIVER);
virtio_negotiate_features(vdev, (1UL << VIRTIO_NET_F_MAC) |
(1UL << VIRTIO_F_ANY_LAYOUT), NULL);
virtio_set_status(vdev, VIRTIO_CONFIG_FEATURES_OK);
vqnames[VIRTIO_NET_RX] = "virtio_net_rx";
vqnames[VIRTIO_NET_TX] = "virtio_net_tx";
callbacks[VIRTIO_NET_RX] = virtio_net_rxready;
callbacks[VIRTIO_NET_TX] = virtio_net_txdone;
ret = virtio_create_virtqueues(vdev, 0, VIRTIO_NET_NUM, vqnames,
callbacks, NULL);
if (ret < 0)
{
vrterr("virtio_device_create_virtqueue failed, ret=%d\n", ret);
return ret;
}
virtio_set_status(vdev, VIRTIO_CONFIG_STATUS_DRIVER_OK);
#if CONFIG_DRIVERS_VIRTIO_NET_BUFNUM > 0
priv->bufnum = CONFIG_DRIVERS_VIRTIO_NET_BUFNUM;
#else
/* Calculate the virtio network buffer number:
* 1/4 for the TX netpkts, 1/4 for the RX netpkts.
*/
priv->bufnum = CONFIG_IOB_NBUFFERS / VIRTIO_NET_MAX_NIOB / 4;
#endif
priv->bufnum = MIN(vdev->vrings_info[VIRTIO_NET_RX].info.num_descs /
(VIRTIO_NET_MAX_NIOB + 1), priv->bufnum);
priv->bufnum = MIN(vdev->vrings_info[VIRTIO_NET_TX].info.num_descs /
(VIRTIO_NET_MAX_NIOB + 1), priv->bufnum);
return OK;
}
static void virtio_net_set_macaddr(FAR struct virtio_net_priv_s *priv)
{
FAR struct net_driver_s *dev =
&((FAR struct netdev_lowerhalf_s *)&priv->lower)->netdev;
FAR struct virtio_device *vdev = priv->vdev;
FAR uint8_t *mac = dev->d_mac.ether.ether_addr_octet;
if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
{
virtio_read_config(vdev, offsetof(struct virtio_net_config_s, mac),
mac, IFHWADDRLEN);
}
else
{
/* Assign a random locally-created MAC address.
*
* TODO: The generated MAC address should be checked to see if it
* conflicts with something else on the network.
*/
srand(time(NULL) +
#ifdef CONFIG_NETDEV_IFINDEX
dev->d_ifindex
#else
(uintptr_t)dev % 256
#endif
);
mac[0] = 0x42;
mac[1] = rand() % 256;
mac[2] = rand() % 256;
mac[3] = rand() % 256;
mac[4] = rand() % 256;
mac[5] = rand() % 256;
}
}
/****************************************************************************
* Name: virtio_net_probe
****************************************************************************/
static int virtio_net_probe(FAR struct virtio_device *vdev)
{
FAR struct netdev_lowerhalf_s *netdev;
FAR struct virtio_net_priv_s *priv;
int ret;
priv = kmm_zalloc(sizeof(*priv));
if (priv == NULL)
{
vrterr("Virtio net driver priv alloc failed\n");
return -ENOMEM;
}
ret = virtio_net_init(priv, vdev);
if (ret < 0)
{
vrterr("virtio_net_init failed, ret=%d\n", ret);
goto err_with_priv;
}
/* Initialize the netdev lower half */
netdev = (FAR struct netdev_lowerhalf_s *)priv;
netdev->quota[NETPKT_RX] = priv->bufnum;
netdev->quota[NETPKT_TX] = priv->bufnum;
netdev->ops = &g_virtio_net_ops;
#ifdef CONFIG_DRIVERS_WIFI_SIM
/* If the WiFi interfaces has reached the setting value,
* no more WiFi interfaces will be created.
*/
if (g_netdev_num < CONFIG_WIFI_SIM_NUMBER)
{
ret = wifi_sim_init(&priv->lower);
if (ret < 0)
{
goto err_with_virtqueues;
}
}
#endif
/* Register the net device */
ret = netdev_lower_register(netdev,
#ifdef CONFIG_DRIVERS_WIFI_SIM
g_netdev_num++ < CONFIG_WIFI_SIM_NUMBER ?
NET_LL_IEEE80211 : NET_LL_ETHERNET
#else
NET_LL_ETHERNET
#endif
);
if (ret < 0)
{
vrterr("netdev_lower_register failed, ret=%d\n", ret);
#ifdef CONFIG_DRIVERS_WIFI_SIM
wifi_sim_remove(&priv->lower);
g_netdev_num--;
#endif
goto err_with_virtqueues;
}
virtio_net_set_macaddr(priv);
return ret;
err_with_virtqueues:
virtio_reset_device(vdev);
virtio_delete_virtqueues(vdev);
err_with_priv:
kmm_free(priv);
return ret;
}
/****************************************************************************
* Name: virtio_net_remove
****************************************************************************/
static void virtio_net_remove(FAR struct virtio_device *vdev)
{
FAR struct virtio_net_priv_s *priv = vdev->priv;
netdev_lower_unregister((FAR struct netdev_lowerhalf_s *)priv);
virtio_reset_device(vdev);
virtio_delete_virtqueues(vdev);
#ifdef CONFIG_DRIVERS_WIFI_SIM
g_netdev_num--;
wifi_sim_remove(&priv->lower);
#endif
kmm_free(priv);
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: virtio_register_net_driver
****************************************************************************/
int virtio_register_net_driver(void)
{
return virtio_register_driver(&g_virtio_net_driver);
}