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