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