VirtualBox

source: vbox/trunk/src/VBox/Additions/solaris/Virtio/VirtioNet-solaris.c@ 57260

Last change on this file since 57260 was 55980, checked in by vboxsync, 10 years ago

iprt/log.h,++: Added extended logger instance getters that also checks whether the given logger and group-flags are enabled, making the LogRel* checks more efficient in avoid uncessary RTLogLoggerEx parameter building and calls. Ditto for debug logging. The LOG_INSTANCE and LOG_REL_INSTANCE tricks are gone for now.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 26.3 KB
Line 
1/* $Id: VirtioNet-solaris.c 55980 2015-05-20 17:35:22Z vboxsync $ */
2/** @file
3 * VirtualBox Guest Additions - Virtio Network Driver for Solaris.
4 */
5
6/*
7 * Copyright (C) 2010-2011 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27/*******************************************************************************
28* Header Files *
29*******************************************************************************/
30#include "Virtio-solaris.h"
31#include "VirtioPci-solaris.h"
32
33#include <sys/conf.h>
34#include <sys/sunddi.h>
35#include <sys/mac_provider.h>
36#include <sys/strsun.h>
37#include <sys/cmn_err.h>
38
39#include <iprt/assert.h>
40#include <iprt/initterm.h>
41#include <iprt/err.h>
42#include <VBox/log.h>
43#include <iprt/mem.h>
44#include <iprt/rand.h>
45#include <iprt/string.h>
46
47/*******************************************************************************
48* Defined Constants And Macros *
49*******************************************************************************/
50#define DEVICE_NAME "virtnet"
51/** The module descriptions as seen in 'modinfo'. */
52#define DEVICE_DESC_DRV "VirtualBox VirtioNet"
53
54/** Copied from "mac_ether.h" - why the heck is this not public?? All Solaris
55 * mac clients use it... */
56#define MAC_PLUGIN_IDENT_ETHER "mac_ether"
57
58/* Copied from our Virtio Device emulation. */
59#define VIRTIO_NET_CSUM 0x00000001 /* Host handles pkts w/ partial csum */
60#define VIRTIO_NET_GUEST_CSUM 0x00000002 /* Guest handles pkts w/ partial csum */
61#define VIRTIO_NET_MAC 0x00000020 /* Host has given MAC address. */
62#define VIRTIO_NET_GSO 0x00000040 /* Host handles pkts w/ any GSO type */
63#define VIRTIO_NET_GUEST_TSO4 0x00000080 /* Guest can handle TSOv4 in. */
64#define VIRTIO_NET_GUEST_TSO6 0x00000100 /* Guest can handle TSOv6 in. */
65#define VIRTIO_NET_GUEST_ECN 0x00000200 /* Guest can handle TSO[6] w/ ECN in. */
66#define VIRTIO_NET_GUEST_UFO 0x00000400 /* Guest can handle UFO in. */
67#define VIRTIO_NET_HOST_TSO4 0x00000800 /* Host can handle TSOv4 in. */
68#define VIRTIO_NET_HOST_TSO6 0x00001000 /* Host can handle TSOv6 in. */
69#define VIRTIO_NET_HOST_ECN 0x00002000 /* Host can handle TSO[6] w/ ECN in. */
70#define VIRTIO_NET_HOST_UFO 0x00004000 /* Host can handle UFO in. */
71#define VIRTIO_NET_MRG_RXBUF 0x00008000 /* Host can merge receive buffers. */
72#define VIRTIO_NET_STATUS 0x00010000 /* virtio_net_config.status available */
73#define VIRTIO_NET_CTRL_VQ 0x00020000 /* Control channel available */
74#define VIRTIO_NET_CTRL_RX 0x00040000 /* Control channel RX mode support */
75#define VIRTIO_NET_CTRL_VLAN 0x00080000 /* Control channel VLAN filtering */
76
77
78/*******************************************************************************
79* Internal Functions *
80*******************************************************************************/
81static void *VirtioNetDevAlloc(PVIRTIODEVICE pDevice);
82static void VirtioNetDevFree(PVIRTIODEVICE pDevice);
83static int VirtioNetDevAttach(PVIRTIODEVICE pDevice);
84static int VirtioNetDevDetach(PVIRTIODEVICE pDevice);
85
86static int VirtioNetAttach(dev_info_t *pDip, ddi_attach_cmd_t Cmd);
87static int VirtioNetDetach(dev_info_t *pDip, ddi_detach_cmd_t Cmd);
88
89static int VirtioNetStat(void *pvArg, uint_t cmdStat, uint64_t *pu64Val);
90static int VirtioNetStart(void *pvArg);
91static void VirtioNetStop(void *pvArg);
92static int VirtioNetSetPromisc(void *pvArg, boolean_t fPromiscOn);
93static int VirtioNetSetMulticast(void *pvArg, boolean_t fAdd, const uint8_t *pbMac);
94static int VirtioNetSetUnicast(void *pvArg, const uint8_t *pbMac);
95static boolean_t VirtioNetGetCapab(void *pvArg, mac_capab_t Capab, void *pvCapabData);
96static mblk_t *VirtioNetXmit(void *pvArg, mblk_t *pMsg);
97static uint_t VirtioNetISR(caddr_t addrArg);
98
99static int VirtioNetAttachQueues(PVIRTIODEVICE pDevice);
100static void VirtioNetDetachQueues(PVIRTIODEVICE pDevice);
101
102
103/*******************************************************************************
104* Structures and Typedefs *
105*******************************************************************************/
106/**
107 * Device operations for Virtio Net.
108 */
109VIRTIODEVICEOPS g_VirtioDeviceOpsNet =
110{
111 VirtioNetDevAlloc,
112 VirtioNetDevFree,
113 VirtioNetDevAttach,
114 VirtioNetDevDetach
115};
116
117/**
118 * virtio_net_t: Private data per Virtio Device.
119 */
120typedef struct virtio_net_t
121{
122 mac_handle_t hMac; /* Handle to the MAC layer. */
123 RTMAC MacAddr; /* MAC address. */
124 PVIRTIOQUEUE pRxQueue; /* Receive Queue. */
125 PVIRTIOQUEUE pTxQueue; /* Xmit Queue. */
126 PVIRTIOQUEUE pCtrlQueue; /* Control Queue. */
127 kmem_cache_t *pTxCache; /* TX buffer cache. */
128} virtio_net_t;
129
130/**
131 * virtio_txbuf_t: Virtio Net TX buffer.
132 */
133typedef struct virtio_net_txbuf_t
134{
135 ddi_dma_handle_t hDMA; /* DMA TX handle. */
136} virtio_net_txbuf_t;
137
138/*
139 * virtio_net_header_t: Virtio Net TX/RX buffer header.
140 */
141typedef struct virtio_net_header_t
142{
143 uint8_t u8Flags; /* Flags. */
144 uint8_t u8GSOType; /* GSO type. */
145 uint16_t u16HdrLen; /* Length of this header. */
146 uint16_t u16GSOSize; /* GSO length if applicable. */
147 uint16_t u16CSumStart; /* Checksum start.*/
148 uint16_t u16CSumOffset; /* Checksum offset.*/
149} virtio_net_header_t;
150
151/**
152 * MAC layer hooks for VirtioNet.
153 */
154static mac_callbacks_t g_VirtioNetCallbacks =
155{
156 MC_GETCAPAB, /* Mask of available extra hooks. */
157 VirtioNetStat,
158 VirtioNetStart,
159 VirtioNetStop,
160 VirtioNetSetPromisc,
161 VirtioNetSetMulticast,
162 VirtioNetSetUnicast,
163 VirtioNetXmit,
164 NULL, /* Reserved. */
165 NULL, /* IOCtl. */
166 VirtioNetGetCapab,
167};
168
169/**
170 * DMA transfer attributes for Xmit/Recv. buffers.
171 */
172static ddi_dma_attr_t g_VirtioNetBufDmaAttr =
173{
174 DMA_ATTR_V0, /* Version. */
175 0, /* Lowest usable address. */
176 0xffffffffffffffffULL, /* Highest usable address. */
177 0x7fffffff, /* Maximum DMAable byte count. */
178 MMU_PAGESIZE, /* Alignment in bytes. */
179 0x7ff, /* Bitmap of burst sizes */
180 1, /* Minimum transfer. */
181 0xffffffffU, /* Maximum transfer. */
182 0xffffffffffffffffULL, /* Maximum segment length. */
183 1, /* Maximum number of segments. */
184 1, /* Granularity. */
185 0 /* Flags (reserved). */
186};
187
188/**
189 * cb_ops: driver char/block entry points
190 */
191static struct cb_ops g_VirtioNetCbOps =
192{
193 nulldev, /* cb open */
194 nulldev, /* cb close */
195 nodev, /* b strategy */
196 nodev, /* b dump */
197 nodev, /* b print */
198 nodev, /* cb read */
199 nodev, /* cb write */
200 nodev, /* cb ioctl */
201 nodev, /* c devmap */
202 nodev, /* c mmap */
203 nodev, /* c segmap */
204 nochpoll, /* c poll */
205 ddi_prop_op, /* property ops */
206 NULL, /* streamtab */
207 D_MP, /* compat. flag */
208 CB_REV /* revision */
209};
210
211/**
212 * dev_ops: driver entry/exit and other ops.
213 */
214static struct dev_ops g_VirtioNetDevOps =
215{
216 DEVO_REV, /* driver build revision */
217 0, /* ref count */
218 NULL, /* get info */
219 nulldev, /* identify */
220 nulldev, /* probe */
221 VirtioNetAttach,
222 VirtioNetDetach,
223 nodev, /* reset */
224 &g_VirtioNetCbOps,
225 (struct bus_ops *)0,
226 nodev /* power */
227};
228
229/**
230 * modldrv: export driver specifics to kernel
231 */
232static struct modldrv g_VirtioNetDriver =
233{
234 &mod_driverops, /* extern from kernel */
235 DEVICE_DESC_DRV " " VBOX_VERSION_STRING "r" RT_XSTR(VBOX_SVN_REV),
236 &g_VirtioNetDevOps
237};
238
239/**
240 * modlinkage: export install/remove/info to the kernel
241 */
242static struct modlinkage g_VirtioNetModLinkage =
243{
244 MODREV_1, /* loadable module system revision */
245 {
246 &g_VirtioNetDriver, /* driver framework */
247 NULL /* terminate array of linkage structures */
248 }
249};
250
251
252/**
253 * Kernel entry points
254 */
255int _init(void)
256{
257 LogFlowFunc((VIRTIOLOGNAME ":_init\n"));
258
259 /*
260 * Prevent module autounloading.
261 */
262 modctl_t *pModCtl = mod_getctl(&g_VirtioNetModLinkage);
263 if (pModCtl)
264 pModCtl->mod_loadflags |= MOD_NOAUTOUNLOAD;
265 else
266 LogRel((VIRTIOLOGNAME ":failed to disable autounloading!\n"));
267
268 /*
269 * Initialize IPRT.
270 */
271 int rc = RTR0Init(0);
272 if (RT_SUCCESS(rc))
273 {
274 /*
275 * Initialize Solaris specific globals here.
276 */
277 mac_init_ops(&g_VirtioNetDevOps, DEVICE_NAME);
278 rc = mod_install(&g_VirtioNetModLinkage);
279 if (!rc)
280 return rc;
281
282 LogRel((VIRTIOLOGNAME ":mod_install failed. rc=%d\n", rc));
283 mac_fini_ops(&g_VirtioNetDevOps);
284 RTR0Term();
285 return rc;
286 }
287 else
288 LogRel((VIRTIOLOGNAME ":failed to initialize IPRT (rc=%d)\n", rc));
289
290 return RTErrConvertToErrno(rc);
291}
292
293
294int _fini(void)
295{
296 int rc;
297 LogFlowFunc((VIRTIOLOGNAME ":_fini\n"));
298
299 rc = mod_remove(&g_VirtioNetModLinkage);
300 if (!rc)
301 {
302 mac_fini_ops(&g_VirtioNetDevOps);
303 RTR0Term();
304 }
305 return rc;
306}
307
308
309int _info(struct modinfo *pModInfo)
310{
311 LogFlowFunc((VIRTIOLOGNAME ":_info\n"));
312
313 int rc = mod_info(&g_VirtioNetModLinkage, pModInfo);
314
315 LogFlow((VIRTIOLOGNAME ":_info returns %d\n", rc));
316 return rc;
317}
318
319
320/**
321 * Attach entry point, to attach a device to the system or resume it.
322 *
323 * @param pDip The module structure instance.
324 * @param enmCmd Operation type (attach/resume).
325 *
326 * @return corresponding solaris error code.
327 */
328static int VirtioNetAttach(dev_info_t *pDip, ddi_attach_cmd_t Cmd)
329{
330 return VirtioAttach(pDip, Cmd, &g_VirtioDeviceOpsNet, &g_VirtioHyperOpsPci);
331}
332
333
334/**
335 * Detach entry point, to detach a device to the system or suspend it.
336 *
337 * @param pDip The module structure instance.
338 * @param enmCmd Operation type (detach/suspend).
339 *
340 * @return corresponding solaris error code.
341 */
342static int VirtioNetDetach(dev_info_t *pDip, ddi_detach_cmd_t Cmd)
343{
344 return VirtioDetach(pDip, Cmd);
345}
346
347
348/**
349 * Virtio Net TX buffer constructor for kmem_cache_create().
350 *
351 * @param pvBuf Pointer to the allocated buffer.
352 * @param pvArg Opaque private data.
353 * @param fFlags Propagated KM flag values.
354 *
355 * @return 0 on success, or -1 on failure.
356 */
357static int VirtioNetTxBufCreate(void *pvBuf, void *pvArg, int fFlags)
358{
359 virtio_net_txbuf_t *pTxBuf = pvBuf;
360 PVIRTIODEVICE pDevice = pvArg;
361
362 /* @todo ncookies handles? */
363 int rc = ddi_dma_alloc_handle(pDevice->pDip, &g_VirtioNetBufDmaAttr,
364 fFlags & KM_NOSLEEP ? DDI_DMA_DONTWAIT : DDI_DMA_SLEEP,
365 0 /* Arg */, &pTxBuf->hDMA);
366 if (rc == DDI_SUCCESS)
367 return 0;
368 return -1;
369}
370
371
372/**
373 * Virtio Net TX buffer destructor for kmem_cache_create().
374 *
375 * @param pvBuf Pointer to the allocated buffer.
376 * @param pvArg
377 */
378static void VirtioNetTxBufDestroy(void *pvBuf, void *pvArg)
379{
380 NOREF(pvArg);
381 virtio_net_txbuf_t *pTxBuf = pvBuf;
382
383 ddi_dma_free_handle(&pTxBuf->hDMA);
384}
385
386
387/**
388 * Virtio Net private data allocation routine.
389 *
390 * @param pDevice Pointer to the Virtio device instance.
391 *
392 * @return Allocated private data that must only be freed by calling
393 * VirtioNetDevFree().
394 */
395static void *VirtioNetDevAlloc(PVIRTIODEVICE pDevice)
396{
397 LogFlowFunc((VIRTIOLOGNAME ":VirtioNetDevAlloc pDevice=%p\n", pDevice));
398
399 AssertReturn(pDevice, NULL);
400 virtio_net_t *pNet = RTMemAllocZ(sizeof(virtio_net_t));
401 if (RT_LIKELY(pNet))
402 {
403 /*
404 * Create a kernel memory cache for frequently allocated/deallocated
405 * buffers.
406 */
407 char szCachename[KSTAT_STRLEN];
408 RTStrPrintf(szCachename, sizeof(szCachename), "VirtioNet_Cache_%d", ddi_get_instance(pDevice->pDip));
409 pNet->pTxCache = kmem_cache_create(szCachename, /* Cache name */
410 sizeof(virtio_net_txbuf_t), /* Size of buffers in cache */
411 0, /* Align */
412 VirtioNetTxBufCreate, /* Buffer constructor */
413 VirtioNetTxBufDestroy, /* Buffer destructor */
414 NULL, /* pfnReclaim */
415 pDevice, /* Private data */
416 NULL, /* "vmp", MBZ (man page) */
417 0 /* "cflags", MBZ (man page) */
418 );
419 if (RT_LIKELY(pNet->pTxCache))
420 return pNet;
421 else
422 LogRel((VIRTIOLOGNAME ":kmem_cache_create failed.\n"));
423 }
424 else
425 LogRel((VIRTIOLOGNAME ":failed to alloc %u bytes for Net instance.\n", sizeof(virtio_net_t)));
426
427 return NULL;
428}
429
430
431/**
432 * Virtio Net private data free routine.
433 *
434 * @param pDevice Pointer to the Virtio device instance.
435 */
436static void VirtioNetDevFree(PVIRTIODEVICE pDevice)
437{
438 LogFlowFunc((VIRTIOLOGNAME ":VirtioNetDevFree pDevice=%p\n", pDevice));
439 AssertReturnVoid(pDevice);
440
441 virtio_net_t *pNet = pDevice->pvDevice;
442 kmem_cache_destroy(pNet->pTxCache);
443 RTMemFree(pNet);
444 pDevice->pvDevice = NULL;
445}
446
447
448/**
449 * Virtio Net device attach rountine.
450 *
451 * @param pDevice Pointer to the Virtio device instance.
452 *
453 * @return corresponding solaris error code.
454 */
455static int VirtioNetDevAttach(PVIRTIODEVICE pDevice)
456{
457 LogFlowFunc((VIRTIOLOGNAME ":VirtioNetDevAttach pDevice=%p\n", pDevice));
458
459 virtio_net_t *pNet = pDevice->pvDevice;
460 mac_register_t *pMacRegHandle = mac_alloc(MAC_VERSION);
461 if (pMacRegHandle)
462 {
463 pMacRegHandle->m_driver = pDevice;
464 pMacRegHandle->m_dip = pDevice->pDip;
465 pMacRegHandle->m_callbacks = &g_VirtioNetCallbacks;
466 pMacRegHandle->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
467 pMacRegHandle->m_min_sdu = 0;
468 pMacRegHandle->m_max_sdu = 1500; /* @todo verify */
469 /* @todo should we set the margin size? */
470 pMacRegHandle->m_src_addr = pNet->MacAddr.au8;
471
472 /*
473 * Get MAC from the host or generate a random MAC address.
474 */
475 if (pDevice->fHostFeatures & VIRTIO_NET_MAC)
476 {
477 pDevice->pHyperOps->pfnGet(pDevice, 0 /* offset */, &pNet->MacAddr.au8, sizeof(pNet->MacAddr));
478 LogFlow((VIRTIOLOGNAME ":VirtioNetDevAttach: Obtained MAC address from host: %.6Rhxs\n", pNet->MacAddr.au8));
479 }
480 else
481 {
482 pNet->MacAddr.au8[0] = 0x08;
483 pNet->MacAddr.au8[1] = 0x00;
484 pNet->MacAddr.au8[2] = 0x27;
485 RTRandBytes(&pNet->MacAddr.au8[3], 3);
486 LogFlow((VIRTIOLOGNAME ":VirtioNetDevAttach: Generated MAC address %.6Rhxs\n", pNet->MacAddr.au8));
487 }
488
489 int rc = VirtioNetAttachQueues(pDevice);
490 if (rc == DDI_SUCCESS)
491 {
492 rc = mac_register(pMacRegHandle, &pNet->hMac);
493 if (rc == 0)
494 {
495 mac_link_update(pNet->hMac, LINK_STATE_DOWN);
496 mac_free(pMacRegHandle);
497 LogFlow((VIRTIOLOGNAME ":VirtioNetDevAttach: successfully registered mac.\n"));
498 return DDI_SUCCESS;
499 }
500 else
501 LogRel((VIRTIOLOGNAME ":VirtioNetDevAttach: mac_register failed. rc=%d\n", rc));
502
503 VirtioNetDetachQueues(pDevice);
504 }
505 else
506 LogRel((VIRTIOLOGNAME ":VirtioNetDevAttach: VirtioNetAttachQueues failed. rc=%d\n", rc));
507
508 mac_free(pMacRegHandle);
509 }
510 else
511 LogRel((VIRTIOLOGNAME ":VirtioNetDevAttach: mac_alloc failed. Invalid version!?!\n"));
512
513 return DDI_FAILURE;
514}
515
516
517/**
518 * Virtio Net device detach routine.
519 *
520 * @param pDevice Pointer to the Virtio device instance.
521 *
522 * @return corresponding solaris error code.
523 */
524static int VirtioNetDevDetach(PVIRTIODEVICE pDevice)
525{
526 LogFlowFunc((VIRTIOLOGNAME ":VirtioNetDevDetach pDevice=%p\n", pDevice));
527 virtio_net_t *pNet = pDevice->pvDevice;
528
529 int rc = mac_unregister(pNet->hMac);
530 if (rc == 0)
531 {
532 VirtioNetDetachQueues(pDevice);
533 return DDI_SUCCESS;
534 }
535 else
536 LogRel((VIRTIOLOGNAME ":VirtioNetDevDetach: mac_unregister failed. rc=%d\n", rc));
537
538 return DDI_FAILURE;
539}
540
541
542/**
543 * Attach the Virtio Net TX, RX and control queues.
544 *
545 * @param pDevice Pointer to the Virtio device instance.
546 *
547 * @return corresponding solaris error code.
548 */
549static int VirtioNetAttachQueues(PVIRTIODEVICE pDevice)
550{
551 LogFlowFunc((VIRTIOLOGNAME ":VirtioNetAttachQueues pDevice=%p\n", pDevice));
552
553 virtio_net_t *pNet = pDevice->pvDevice;
554
555 pNet->pRxQueue = VirtioGetQueue(pDevice, 0 /* index */ );
556 if (pNet->pRxQueue)
557 {
558 pNet->pTxQueue = VirtioGetQueue(pDevice, 1 /* index */);
559 if (pNet->pTxQueue)
560 {
561 if (pDevice->fHostFeatures & VIRTIO_NET_CTRL_VQ)
562 {
563 pNet->pCtrlQueue = VirtioGetQueue(pDevice, 2 /* index */);
564 if (pNet->pCtrlQueue)
565 {
566 LogFlow((VIRTIOLOGNAME ":VirtioNetAttachQueues successfully obtained 3 queues.\n"));
567 return DDI_SUCCESS;
568 }
569 else
570 LogRel((VIRTIOLOGNAME ":VirtioNetAttachQueues: failed to get Control queue.\n"));
571 }
572 else
573 {
574 LogFlow((VIRTIOLOGNAME ":VirtioNetAttachQueues successfully obtained 2 queues.\n"));
575 return DDI_SUCCESS;
576 }
577
578 VirtioPutQueue(pDevice, pNet->pTxQueue);
579 pNet->pTxQueue = NULL;
580 }
581 else
582 LogRel((VIRTIOLOGNAME ":VirtioNetAttachQueues: failed to get TX queue.\n"));
583
584 VirtioPutQueue(pDevice, pNet->pRxQueue);
585 pNet->pRxQueue = NULL;
586 }
587 else
588 LogRel((VIRTIOLOGNAME ":VirtioNetAttachQueues: failed to get RX queue.\n"));
589
590 return DDI_FAILURE;
591}
592
593
594/**
595 * Detach the Virtio Net TX, RX and control queues.
596 *
597 * @param pDevice Pointer to the Virtio device instance.
598 */
599static void VirtioNetDetachQueues(PVIRTIODEVICE pDevice)
600{
601 LogFlowFunc((VIRTIOLOGNAME ":VirtioNetDetachQueues pDevice=%p\n", pDevice));
602 virtio_net_t *pNet = pDevice->pvDevice;
603
604 if ( pDevice->fHostFeatures & VIRTIO_NET_CTRL_VQ
605 && pNet->pCtrlQueue)
606 VirtioPutQueue(pDevice, pNet->pCtrlQueue);
607
608 if (pNet->pTxCache)
609 VirtioPutQueue(pDevice, pNet->pTxQueue);
610
611 if (pNet->pRxQueue)
612 VirtioPutQueue(pDevice, pNet->pRxQueue);
613}
614
615
616
617/* -=-=-=-=- Virtio Net MAC layer callbacks -=-=-=-=- */
618
619/**
620 * Virtio Net statistics.
621 *
622 * @param pvArg Pointer to private data.
623 * @param cmdStat Which statistics to provide.
624 * @param pu64Val Where to write statistics data.
625 *
626 * @return corresponding solaris error code.
627 */
628static int VirtioNetStat(void *pvArg, uint_t cmdStat, uint64_t *pu64Val)
629{
630 NOREF(pvArg);
631 NOREF(cmdStat);
632 NOREF(pu64Val);
633 return ENOTSUP;
634}
635
636
637/**
638 * Virtio Net Start.
639 *
640 * @param pvArg Pointer to private data.
641 *
642 * @return corresponding solaris error code.
643 */
644static int VirtioNetStart(void *pvArg)
645{
646 PVIRTIODEVICE pDevice = pvArg;
647 virtio_net_t *pNet = pDevice->pvDevice;
648 mac_link_update(pNet->hMac, LINK_STATE_UP);
649
650 pDevice->pHyperOps->pfnSetStatus(pDevice, VIRTIO_PCI_STATUS_DRV_OK);
651 return 0;
652}
653
654
655/**
656 * Virtio Net Stop.
657 *
658 * @param pvArg Pointer to private data.
659 */
660static void VirtioNetStop(void *pvArg)
661{
662 PVIRTIODEVICE pDevice = pvArg;
663 virtio_net_t *pNet = pDevice->pvDevice;
664 mac_link_update(pNet->hMac, LINK_STATE_DOWN);
665
666 /*
667 * I don't think we should set status here as the host checks the status on every Xmit. This means pending Xmits
668 * would also be dropped.
669 * @todo: Not sure what's the best way to signal connect/disconnect of the link to the host. Figure it out.
670 */
671}
672
673
674/**
675 * Virtio Net toggle Promiscuous mode.
676 *
677 * @param pvArg Pointer to private data.
678 * @param fPromiscOn Promiscuous On/Off.
679 *
680 * @return corresponding solaris error code.
681 */
682static int VirtioNetSetPromisc(void *pvArg, boolean_t fPromiscOn)
683{
684 NOREF(pvArg);
685 NOREF(fPromiscOn);
686 return 0;
687}
688
689
690/**
691 * Virtio Net set/add multicast address.
692 *
693 * @param pvArg Pointer to private data.
694 * @param fAdd Whether to add multicast address or not.
695 * @param pbMac Pointer to the multicast MAC address to set/add.
696 *
697 * @return corresponding solaris error code.
698 */
699static int VirtioNetSetMulticast(void *pvArg, boolean_t fAdd, const uint8_t *pbMac)
700{
701 NOREF(pvArg);
702 NOREF(fAdd);
703 NOREF(pbMac);
704 return 1;
705}
706
707
708/**
709 * Virtio Net set unicast address.
710 *
711 * @param pvArg Pointer to private data.
712 * @param pbMac Pointer to the unicast MAC address to set.
713 *
714 * @return corresponding solaris error code.
715 */
716static int VirtioNetSetUnicast(void *pvArg, const uint8_t *pbMac)
717{
718 NOREF(pvArg);
719 NOREF(pbMac);
720 return ENOTSUP;
721}
722
723
724/**
725 * Virtio Net get capabilities hook.
726 *
727 * @param pvArg Pointer to private data.
728 * @param Capab MAC layer capabilities.
729 * @param pvCapabData Pointer to store capability info.
730 *
731 * @return B_TRUE upon success, otherwise B_FALSE.
732 */
733static boolean_t VirtioNetGetCapab(void *pvArg, mac_capab_t Capab, void *pvCapabData)
734{
735 NOREF(pvArg);
736 NOREF(Capab);
737 NOREF(pvCapabData);
738 return B_FALSE;
739}
740
741
742/**
743 * Virtio Net Xmit hook.
744 *
745 * @param pvArg Pointer to private data.
746 * @param pMsg Pointer to the message.
747 *
748 * @return Pointer to message not Xmited.
749 */
750static mblk_t *VirtioNetXmit(void *pvArg, mblk_t *pMsg)
751{
752 LogFlowFunc((VIRTIOLOGNAME ":VirtioNetXmit pMsg=%p\n", pMsg));
753 cmn_err(CE_NOTE, "Xmit pMsg=%p\n", pMsg);
754
755 PVIRTIODEVICE pDevice = pvArg;
756 virtio_net_t *pNet = pDevice->pvDevice;
757 bool fNotify = false;
758
759 while (pMsg)
760 {
761 mblk_t *pNextMsg = pMsg->b_next;
762
763#if 0
764 mblk_t *pHdr = allocb(sizeof(virtio_net_header_t), BPRI_HI);
765 if (RT_UNLIKELY(!pHdr))
766 break;
767
768 virtio_net_header_t *pNetHdr = pHdr->b_rptr;
769 memset(pNetHdr, 0, sizeof(virtio_net_header_t));
770 pNetHdr->u8Flags = VIRTIO_NET_GUEST_CSUM;
771 pNetHdr->u16HdrLen = sizeof(virtio_net_header_t);
772
773 pHdr->b_wptr += sizeof(virtio_net_header_t);
774 pHdr->b_cont = pMsg;
775#endif
776
777 virtio_net_txbuf_t *pTxBuf = kmem_cache_alloc(pNet->pTxCache, KM_SLEEP);
778 if (!pTxBuf)
779 break;
780
781 ddi_dma_cookie_t DmaCookie;
782 uint_t cCookies;
783 int rc = ddi_dma_addr_bind_handle(pTxBuf->hDMA, NULL /* addrspace */, (char *)pMsg->b_rptr, MBLKL(pMsg),
784 DDI_DMA_WRITE | DDI_DMA_STREAMING, DDI_DMA_SLEEP, 0 /* addr */,
785 &DmaCookie, &cCookies);
786 cmn_err(CE_NOTE, "VirtioNetXmit: MBLKL pMsg=%u\n", MBLKL(pMsg));
787 if (rc != DDI_DMA_MAPPED)
788 {
789 LogRel((VIRTIOLOGNAME ":VirtioNetXmit failed to map address to DMA handle. rc=%d\n", rc));
790 kmem_cache_free(pNet->pTxCache, pTxBuf);
791 break;
792 }
793
794 /** @todo get 'cCookies' slots from the ring. */
795
796 for (uint_t i = 0; i < cCookies; i++)
797 {
798 uint16_t fFlags = 0;
799 if (i < cCookies - 1)
800 fFlags |= VIRTIO_FLAGS_RING_DESC_NEXT;
801
802 rc = VirtioRingPush(pNet->pTxQueue, DmaCookie.dmac_laddress, DmaCookie.dmac_size, fFlags);
803 if (RT_FAILURE(rc))
804 {
805 LogRel((VIRTIOLOGNAME ":VirtioNetXmit failed. rc=%Rrc\n", rc));
806 break;
807 }
808
809 ddi_dma_nextcookie(pTxBuf->hDMA, &DmaCookie);
810 }
811
812 pMsg = pNextMsg;
813 fNotify = true;
814 if (RT_FAILURE(rc))
815 {
816 ddi_dma_unbind_handle(pTxBuf->hDMA);
817 break;
818 }
819 }
820
821 if (fNotify)
822 pDevice->pHyperOps->pfnNotifyQueue(pDevice, pNet->pTxQueue);
823
824 return pMsg;
825}
826
827
828/**
829 * Interrupt Service Routine for Virtio Net.
830 *
831 * @param Arg Private data (unused, will be NULL).
832 * @returns DDI_INTR_CLAIMED if it's our interrupt, DDI_INTR_UNCLAIMED if it isn't.
833 */
834static uint_t VirtioNetISR(caddr_t Arg)
835{
836 cmn_err(CE_NOTE, "VirtioNetISR Arg=%p\n", Arg);
837 NOREF(Arg);
838 return DDI_INTR_UNCLAIMED;
839}
840
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette