VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/solaris/NetIf-solaris.cpp@ 53364

Last change on this file since 53364 was 51092, checked in by vboxsync, 11 years ago

6813 src-client/MachineDebuggerImpl.cpp + various formatting changes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.4 KB
Line 
1/* $Id: NetIf-solaris.cpp 51092 2014-04-16 17:57:25Z vboxsync $ */
2/** @file
3 * Main - NetIfList, Solaris implementation.
4 */
5
6/*
7 * Copyright (C) 2008-2012 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
18
19
20/*******************************************************************************
21* Header Files *
22*******************************************************************************/
23#define LOG_GROUP LOG_GROUP_MAIN
24
25#include <iprt/err.h>
26#include <iprt/ctype.h>
27#include <iprt/mem.h>
28#include <iprt/path.h>
29#include <list>
30
31#include "Logging.h"
32#include "HostNetworkInterfaceImpl.h"
33#include "netif.h"
34
35#ifdef VBOX_WITH_HOSTNETIF_API
36
37#include <map>
38#include <string>
39#include <fcntl.h>
40#include <unistd.h>
41#include <stropts.h>
42#include <limits.h>
43#include <stdio.h>
44#include <libdevinfo.h>
45#include <net/if.h>
46#include <sys/socket.h>
47#include <sys/sockio.h>
48#include <net/if_arp.h>
49#include <net/if.h>
50#include <sys/types.h>
51#include <kstat.h>
52
53#include "DynLoadLibSolaris.h"
54
55static uint32_t getInstance(const char *pszIfaceName, char *pszDevName)
56{
57 /*
58 * Get the instance number from the interface name, then clip it off.
59 */
60 int cbInstance = 0;
61 int cbIface = strlen(pszIfaceName);
62 const char *pszEnd = pszIfaceName + cbIface - 1;
63 for (int i = 0; i < cbIface - 1; i++)
64 {
65 if (!RT_C_IS_DIGIT(*pszEnd))
66 break;
67 cbInstance++;
68 pszEnd--;
69 }
70
71 uint32_t uInstance = RTStrToUInt32(pszEnd + 1);
72 strncpy(pszDevName, pszIfaceName, cbIface - cbInstance);
73 pszDevName[cbIface - cbInstance] = '\0';
74 return uInstance;
75}
76
77static uint32_t kstatGet(const char *name)
78{
79 kstat_ctl_t *kc;
80 uint32_t uSpeed = 0;
81
82 if ((kc = kstat_open()) == 0)
83 {
84 LogRel(("kstat_open() -> %d\n", errno));
85 return 0;
86 }
87
88 kstat_t *ksAdapter = kstat_lookup(kc, (char *)"link", -1, (char *)name);
89 if (ksAdapter == 0)
90 {
91 char szModule[KSTAT_STRLEN];
92 uint32_t uInstance = getInstance(name, szModule);
93 ksAdapter = kstat_lookup(kc, szModule, uInstance, (char *)"phys");
94 if (ksAdapter == 0)
95 ksAdapter = kstat_lookup(kc, szModule, uInstance, (char*)name);
96 }
97 if (ksAdapter == 0)
98 LogRel(("Failed to get network statistics for %s\n", name));
99 else if (kstat_read(kc, ksAdapter, 0) == -1)
100 LogRel(("kstat_read(%s) -> %d\n", name, errno));
101 else
102 {
103 kstat_named_t *kn;
104 if ((kn = (kstat_named_t *)kstat_data_lookup(ksAdapter, (char *)"ifspeed")) == 0)
105 LogRel(("kstat_data_lookup(ifspeed) -> %d, name=%s\n", errno, name));
106 else
107 uSpeed = kn->value.ul / 1000000; /* bits -> Mbits */
108 }
109 kstat_close(kc);
110 LogFlow(("kstatGet(%s) -> %u Mbit/s\n", name, uSpeed));
111 return uSpeed;
112}
113
114static void queryIfaceSpeed(PNETIFINFO pInfo)
115{
116 /* Don't query interface speed for inactive interfaces (see @bugref{6345}). */
117 if (pInfo->enmStatus == NETIF_S_UP)
118 pInfo->uSpeedMbits = kstatGet(pInfo->szShortName);
119 else
120 pInfo->uSpeedMbits = 0;
121 LogFlow(("queryIfaceSpeed(%s) -> %u\n", pInfo->szShortName, pInfo->uSpeedMbits));
122}
123
124static void vboxSolarisAddHostIface(char *pszIface, int Instance, void *pvHostNetworkInterfaceList)
125{
126 std::list<ComObjPtr<HostNetworkInterface> > *pList =
127 (std::list<ComObjPtr<HostNetworkInterface> > *)pvHostNetworkInterfaceList;
128 Assert(pList);
129
130 typedef std::map <std::string, std::string> NICMap;
131 typedef std::pair <std::string, std::string> NICPair;
132 static NICMap SolarisNICMap;
133 if (SolarisNICMap.empty())
134 {
135 SolarisNICMap.insert(NICPair("afe", "ADMtek Centaur/Comet Fast Ethernet"));
136 SolarisNICMap.insert(NICPair("atge", "Atheros/Attansic Gigabit Ethernet"));
137 SolarisNICMap.insert(NICPair("aggr", "Link Aggregation Interface"));
138 SolarisNICMap.insert(NICPair("bfe", "Broadcom BCM4401 Fast Ethernet"));
139 SolarisNICMap.insert(NICPair("bge", "Broadcom BCM57xx Gigabit Ethernet"));
140 SolarisNICMap.insert(NICPair("bnx", "Broadcom NetXtreme Gigabit Ethernet"));
141 SolarisNICMap.insert(NICPair("bnxe", "Broadcom NetXtreme II 10 Gigabit Ethernet"));
142 SolarisNICMap.insert(NICPair("ce", "Cassini Gigabit Ethernet"));
143 SolarisNICMap.insert(NICPair("chxge", "Chelsio Ethernet"));
144 SolarisNICMap.insert(NICPair("dmfe", "Davicom 9102 Fast Ethernet"));
145 SolarisNICMap.insert(NICPair("dnet", "DEC 21040/41 21140 Ethernet"));
146 SolarisNICMap.insert(NICPair("e1000", "Intel PRO/1000 Gigabit Ethernet"));
147 SolarisNICMap.insert(NICPair("e1000g", "Intel PRO/1000 Gigabit Ethernet"));
148 SolarisNICMap.insert(NICPair("elx", "3COM Etherlink III Ethernet"));
149 SolarisNICMap.insert(NICPair("elxl", "3COM Etherlink XL Ethernet"));
150 SolarisNICMap.insert(NICPair("eri", "eri Fast Ethernet"));
151 SolarisNICMap.insert(NICPair("ge", "GEM Gigabit Ethernet"));
152 SolarisNICMap.insert(NICPair("hme", "SUNW,hme Fast-Ethernet"));
153 SolarisNICMap.insert(NICPair("hxge", "Sun Blade 10 Gigabit Ethernet"));
154 SolarisNICMap.insert(NICPair("igb", "Intel 82575 PCI-E Gigabit Ethernet"));
155 SolarisNICMap.insert(NICPair("ipge", "PCI-E Gigabit Ethernet"));
156 SolarisNICMap.insert(NICPair("iprb", "Intel 82557/58/59 Ethernet"));
157 SolarisNICMap.insert(NICPair("ixgb", "Intel 82597ex 10 Gigabit Ethernet"));
158 SolarisNICMap.insert(NICPair("ixgbe", "Intel 10 Gigabit PCI-E Ethernet"));
159 SolarisNICMap.insert(NICPair("mcxe", "Mellanox ConnectX-2 10 Gigabit Ethernet"));
160 SolarisNICMap.insert(NICPair("mxfe", "Macronix 98715 Fast Ethernet"));
161 SolarisNICMap.insert(NICPair("nfo", "Nvidia Gigabit Ethernet"));
162 SolarisNICMap.insert(NICPair("nge", "Nvidia Gigabit Ethernet"));
163 SolarisNICMap.insert(NICPair("ntxn", "NetXen 10/1 Gigabit Ethernet"));
164 SolarisNICMap.insert(NICPair("nxge", "Sun 10/1 Gigabit Ethernet"));
165 SolarisNICMap.insert(NICPair("pcelx", "3COM EtherLink III PCMCIA Ethernet"));
166 SolarisNICMap.insert(NICPair("pcn", "AMD PCnet Ethernet"));
167 SolarisNICMap.insert(NICPair("qfe", "SUNW,qfe Quad Fast-Ethernet"));
168 SolarisNICMap.insert(NICPair("rge", "Realtek Gigabit Ethernet"));
169 SolarisNICMap.insert(NICPair("rtls", "Realtek 8139 Fast Ethernet"));
170 SolarisNICMap.insert(NICPair("sfe", "SiS900 Fast Ethernet"));
171 SolarisNICMap.insert(NICPair("skge", "SksKonnect Gigabit Ethernet"));
172 SolarisNICMap.insert(NICPair("spwr", "SMC EtherPower II 10/100 (9432) Ethernet"));
173 SolarisNICMap.insert(NICPair("vboxnet", "VirtualBox Host Ethernet"));
174 SolarisNICMap.insert(NICPair("vboxvnic_template", "VirtualBox Virtual Network Interface Template"));
175 SolarisNICMap.insert(NICPair("vlan", "Virtual LAN Ethernet"));
176 SolarisNICMap.insert(NICPair("vr", "VIA Rhine Fast Ethernet"));
177 SolarisNICMap.insert(NICPair("vnic", "Virtual Network Interface Ethernet"));
178 SolarisNICMap.insert(NICPair("xge", "Neterior Xframe 10Gigabit Ethernet"));
179 SolarisNICMap.insert(NICPair("yge", "Marvell Yukon 2 Fast Ethernet"));
180 }
181
182 /*
183 * Try picking up description from our NIC map.
184 */
185 char szNICInstance[128];
186 RTStrPrintf(szNICInstance, sizeof(szNICInstance), "%s%d", pszIface, Instance);
187 char szNICDesc[256];
188 std::string Description = SolarisNICMap[pszIface];
189 if (Description != "VirtualBox Host Ethernet")
190 {
191 if (Description != "")
192 RTStrPrintf(szNICDesc, sizeof(szNICDesc), "%s - %s", szNICInstance, Description.c_str());
193 else
194 RTStrPrintf(szNICDesc, sizeof(szNICDesc), "%s - Ethernet", szNICInstance);
195 }
196 else
197 RTStrPrintf(szNICDesc, sizeof(szNICDesc), "%s", szNICInstance);
198
199 /*
200 * Try to get IP V4 address and netmask as well as Ethernet address.
201 */
202 NETIFINFO Info;
203 RT_ZERO(Info);
204 int Sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
205 if (Sock > 0)
206 {
207 struct lifreq IfReq;
208 RTStrCopy(IfReq.lifr_name, sizeof(IfReq.lifr_name), szNICInstance);
209 if (ioctl(Sock, SIOCGLIFADDR, &IfReq) >= 0)
210 {
211 memcpy(Info.IPAddress.au8, &((struct sockaddr_in *)&IfReq.lifr_addr)->sin_addr.s_addr,
212 sizeof(Info.IPAddress.au8));
213 struct arpreq ArpReq;
214 memcpy(&ArpReq.arp_pa, &IfReq.lifr_addr, sizeof(struct sockaddr_in));
215
216 /*
217 * We might fail if the interface has not been assigned an IP address.
218 * That doesn't matter; as long as it's plumbed we can pick it up.
219 * But, if it has not acquired an IP address we cannot obtain it's MAC
220 * address this way, so we just use all zeros there.
221 */
222 if (ioctl(Sock, SIOCGARP, &ArpReq) >= 0)
223 {
224 memcpy(&Info.MACAddress, ArpReq.arp_ha.sa_data, sizeof(Info.MACAddress));
225 }
226
227 }
228
229 if (ioctl(Sock, SIOCGLIFNETMASK, &IfReq) >= 0)
230 {
231 memcpy(Info.IPNetMask.au8, &((struct sockaddr_in *)&IfReq.lifr_addr)->sin_addr.s_addr,
232 sizeof(Info.IPNetMask.au8));
233 }
234 if (ioctl(Sock, SIOCGLIFFLAGS, &IfReq) >= 0)
235 {
236 Info.enmStatus = IfReq.lifr_flags & IFF_UP ? NETIF_S_UP : NETIF_S_DOWN;
237 }
238 close(Sock);
239 }
240 /*
241 * Try to get IP V6 address and netmask.
242 */
243 Sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
244 if (Sock > 0)
245 {
246 struct lifreq IfReq;
247 RTStrCopy(IfReq.lifr_name, sizeof(IfReq.lifr_name), szNICInstance);
248 if (ioctl(Sock, SIOCGLIFADDR, &IfReq) >= 0)
249 {
250 memcpy(Info.IPv6Address.au8, ((struct sockaddr_in6 *)&IfReq.lifr_addr)->sin6_addr.s6_addr,
251 sizeof(Info.IPv6Address.au8));
252 }
253 if (ioctl(Sock, SIOCGLIFNETMASK, &IfReq) >= 0)
254 {
255 memcpy(Info.IPv6NetMask.au8, ((struct sockaddr_in6 *)&IfReq.lifr_addr)->sin6_addr.s6_addr,
256 sizeof(Info.IPv6NetMask.au8));
257 }
258 close(Sock);
259 }
260
261 /*
262 * Construct UUID with interface name and the MAC address if available.
263 */
264 RTUUID Uuid;
265 RTUuidClear(&Uuid);
266 memcpy(&Uuid, szNICInstance, RT_MIN(strlen(szNICInstance), sizeof(Uuid)));
267 Uuid.Gen.u8ClockSeqHiAndReserved = (Uuid.Gen.u8ClockSeqHiAndReserved & 0x3f) | 0x80;
268 Uuid.Gen.u16TimeHiAndVersion = (Uuid.Gen.u16TimeHiAndVersion & 0x0fff) | 0x4000;
269 Uuid.Gen.au8Node[0] = Info.MACAddress.au8[0];
270 Uuid.Gen.au8Node[1] = Info.MACAddress.au8[1];
271 Uuid.Gen.au8Node[2] = Info.MACAddress.au8[2];
272 Uuid.Gen.au8Node[3] = Info.MACAddress.au8[3];
273 Uuid.Gen.au8Node[4] = Info.MACAddress.au8[4];
274 Uuid.Gen.au8Node[5] = Info.MACAddress.au8[5];
275 Info.Uuid = Uuid;
276 Info.enmMediumType = NETIF_T_ETHERNET;
277 strncpy(Info.szShortName, szNICInstance, sizeof(Info.szShortName) - 1);
278
279 HostNetworkInterfaceType_T enmType;
280 if (strncmp(szNICInstance, RT_STR_TUPLE("vboxnet")))
281 enmType = HostNetworkInterfaceType_Bridged;
282 else
283 enmType = HostNetworkInterfaceType_HostOnly;
284 queryIfaceSpeed(&Info);
285 ComObjPtr<HostNetworkInterface> IfObj;
286 IfObj.createObject();
287 if (SUCCEEDED(IfObj->init(Bstr(szNICDesc), enmType, &Info)))
288 pList->push_back(IfObj);
289}
290
291static boolean_t vboxSolarisAddLinkHostIface(const char *pszIface, void *pvHostNetworkInterfaceList)
292{
293 /*
294 * Skip IPSEC interfaces. It's at IP level.
295 */
296 if (!strncmp(pszIface, RT_STR_TUPLE("ip.tun")))
297 return _B_FALSE;
298
299 /*
300 * Skip our own dynamic VNICs but don't skip VNIC templates.
301 * These names originate from VBoxNetFltBow-solaris.c, hardcoded here for now.
302 */
303 if ( strncmp(pszIface, RT_STR_TUPLE("vboxvnic_template"))
304 && !strncmp(pszIface, RT_STR_TUPLE("vboxvnic")))
305 return _B_FALSE;
306
307 /*
308 * Clip off the zone instance number from the interface name (if any).
309 */
310 char szIfaceName[128];
311 strcpy(szIfaceName, pszIface);
312 char *pszColon = (char *)memchr(szIfaceName, ':', sizeof(szIfaceName));
313 if (pszColon)
314 *pszColon = '\0';
315
316 /*
317 * Get the instance number from the interface name, then clip it off.
318 */
319 int cbInstance = 0;
320 int cbIface = strlen(szIfaceName);
321 const char *pszEnd = pszIface + cbIface - 1;
322 for (int i = 0; i < cbIface - 1; i++)
323 {
324 if (!RT_C_IS_DIGIT(*pszEnd))
325 break;
326 cbInstance++;
327 pszEnd--;
328 }
329
330 int Instance = atoi(pszEnd + 1);
331 strncpy(szIfaceName, pszIface, cbIface - cbInstance);
332 szIfaceName[cbIface - cbInstance] = '\0';
333
334 /*
335 * Add the interface.
336 */
337 vboxSolarisAddHostIface(szIfaceName, Instance, pvHostNetworkInterfaceList);
338
339 /*
340 * Continue walking...
341 */
342 return _B_FALSE;
343}
344
345static bool vboxSolarisSortNICList(const ComObjPtr<HostNetworkInterface> Iface1, const ComObjPtr<HostNetworkInterface> Iface2)
346{
347 Bstr Iface1Str;
348 (*Iface1).COMGETTER(Name) (Iface1Str.asOutParam());
349
350 Bstr Iface2Str;
351 (*Iface2).COMGETTER(Name) (Iface2Str.asOutParam());
352
353 return Iface1Str < Iface2Str;
354}
355
356static bool vboxSolarisSameNIC(const ComObjPtr<HostNetworkInterface> Iface1, const ComObjPtr<HostNetworkInterface> Iface2)
357{
358 Bstr Iface1Str;
359 (*Iface1).COMGETTER(Name) (Iface1Str.asOutParam());
360
361 Bstr Iface2Str;
362 (*Iface2).COMGETTER(Name) (Iface2Str.asOutParam());
363
364 return (Iface1Str == Iface2Str);
365}
366
367static int vboxSolarisAddPhysHostIface(di_node_t Node, di_minor_t Minor, void *pvHostNetworkInterfaceList)
368{
369 NOREF(Minor);
370
371 /*
372 * Skip aggregations.
373 */
374 if (!strcmp(di_driver_name(Node), "aggr"))
375 return DI_WALK_CONTINUE;
376
377 /*
378 * Skip softmacs.
379 */
380 if (!strcmp(di_driver_name(Node), "softmac"))
381 return DI_WALK_CONTINUE;
382
383 vboxSolarisAddHostIface(di_driver_name(Node), di_instance(Node), pvHostNetworkInterfaceList);
384 return DI_WALK_CONTINUE;
385}
386
387int NetIfList(std::list <ComObjPtr<HostNetworkInterface> > &list)
388{
389
390 /*
391 * Use libdevinfo for determining all physical interfaces.
392 */
393 di_node_t Root;
394 Root = di_init("/", DINFOCACHE);
395 if (Root != DI_NODE_NIL)
396 {
397 di_walk_minor(Root, DDI_NT_NET, 0, &list, vboxSolarisAddPhysHostIface);
398 di_fini(Root);
399 }
400
401 /*
402 * Use libdlpi for determining all DLPI interfaces.
403 */
404 if (VBoxSolarisLibDlpiFound())
405 g_pfnLibDlpiWalk(vboxSolarisAddLinkHostIface, &list, 0);
406
407 /*
408 * This gets only the list of all plumbed logical interfaces.
409 * This is needed for zones which cannot access the device tree
410 * and in this case we just let them use the list of plumbed interfaces
411 * on the zone.
412 */
413 int Sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
414 if (Sock > 0)
415 {
416 struct lifnum IfNum;
417 RT_ZERO(IfNum);
418 IfNum.lifn_family = AF_INET;
419 int rc = ioctl(Sock, SIOCGLIFNUM, &IfNum);
420 if (!rc)
421 {
422 int cIfaces = RT_MIN(1024, IfNum.lifn_count); /* sane limit */
423 int cbIfaces = cIfaces * sizeof(struct lifreq);
424 struct lifreq *Ifaces = (struct lifreq *)RTMemTmpAlloc(cbIfaces);
425 if (Ifaces)
426 {
427 struct lifconf IfConfig;
428 RT_ZERO(IfConfig);
429 IfConfig.lifc_family = AF_INET;
430 IfConfig.lifc_len = cbIfaces;
431 IfConfig.lifc_buf = (caddr_t)Ifaces;
432 rc = ioctl(Sock, SIOCGLIFCONF, &IfConfig);
433 if (!rc)
434 {
435 for (int i = 0; i < cIfaces; i++)
436 {
437 /*
438 * Skip loopback interfaces.
439 */
440 if (!strncmp(Ifaces[i].lifr_name, RT_STR_TUPLE("lo")))
441 continue;
442
443#if 0
444 rc = ioctl(Sock, SIOCGLIFADDR, &(Ifaces[i]));
445 if (rc >= 0)
446 {
447 memcpy(Info.IPAddress.au8, ((struct sockaddr *)&Ifaces[i].lifr_addr)->sa_data,
448 sizeof(Info.IPAddress.au8));
449 // SIOCGLIFNETMASK
450 struct arpreq ArpReq;
451 memcpy(&ArpReq.arp_pa, &Ifaces[i].lifr_addr, sizeof(struct sockaddr_in));
452
453 /*
454 * We might fail if the interface has not been assigned an IP address.
455 * That doesn't matter; as long as it's plumbed we can pick it up.
456 * But, if it has not acquired an IP address we cannot obtain it's MAC
457 * address this way, so we just use all zeros there.
458 */
459 rc = ioctl(Sock, SIOCGARP, &ArpReq);
460 if (rc >= 0)
461 memcpy(&Info.MACAddress, ArpReq.arp_ha.sa_data, sizeof(Info.MACAddress));
462
463 char szNICDesc[LIFNAMSIZ + 256];
464 char *pszIface = Ifaces[i].lifr_name;
465 strcpy(szNICDesc, pszIface);
466
467 vboxSolarisAddLinkHostIface(pszIface, &list);
468 }
469#endif
470
471 char *pszIface = Ifaces[i].lifr_name;
472 vboxSolarisAddLinkHostIface(pszIface, &list);
473 }
474 }
475 RTMemTmpFree(Ifaces);
476 }
477 }
478 close(Sock);
479 }
480
481 /*
482 * Weed out duplicates caused by dlpi_walk inconsistencies across Nevadas.
483 */
484 list.sort(vboxSolarisSortNICList);
485 list.unique(vboxSolarisSameNIC);
486
487 return VINF_SUCCESS;
488}
489
490#else
491int NetIfList(std::list <ComObjPtr<HostNetworkInterface> > &list)
492{
493 return VERR_NOT_IMPLEMENTED;
494}
495#endif
496
497int NetIfGetConfigByName(PNETIFINFO pInfo)
498{
499 NOREF(pInfo);
500 return VERR_NOT_IMPLEMENTED;
501}
502
503/**
504 * Retrieve the physical link speed in megabits per second. If the interface is
505 * not up or otherwise unavailable the zero speed is returned.
506 *
507 * @returns VBox status code.
508 *
509 * @param pcszIfName Interface name.
510 * @param puMbits Where to store the link speed.
511 */
512int NetIfGetLinkSpeed(const char *pcszIfName, uint32_t *puMbits)
513{
514 *puMbits = kstatGet(pcszIfName);
515 return VINF_SUCCESS;
516}
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