VirtualBox

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

Last change on this file since 29853 was 28962, checked in by vboxsync, 15 years ago

Main/Solaris: Added description for igb NIC.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.1 KB
Line 
1/* $Id: NetIf-solaris.cpp 28962 2010-05-03 08:26:14Z vboxsync $ */
2/** @file
3 * Main - NetIfList, Solaris implementation.
4 */
5
6/*
7 * Copyright (C) 2008 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 <list>
28
29#include "Logging.h"
30#include "HostNetworkInterfaceImpl.h"
31#include "netif.h"
32
33#ifdef VBOX_WITH_HOSTNETIF_API
34
35#include <map>
36#include <string>
37#include <fcntl.h>
38#include <unistd.h>
39#include <stropts.h>
40#include <errno.h>
41#include <limits.h>
42#include <stdio.h>
43#ifdef VBOX_SOLARIS_NSL_RESOLVED
44# include <libdevinfo.h>
45#endif
46#include <net/if.h>
47#include <sys/socket.h>
48#include <sys/sockio.h>
49#include <net/if_arp.h>
50#include <net/if.h>
51#include <sys/types.h>
52
53#include "DynLoadLibSolaris.h"
54
55static void vboxSolarisAddHostIface(char *pszIface, int Instance, void *pvHostNetworkInterfaceList)
56{
57 std::list<ComObjPtr<HostNetworkInterface> > *pList = (std::list<ComObjPtr<HostNetworkInterface> > *)pvHostNetworkInterfaceList;
58 Assert(pList);
59
60 typedef std::map <std::string, std::string> NICMap;
61 typedef std::pair <std::string, std::string> NICPair;
62 static NICMap SolarisNICMap;
63 if (SolarisNICMap.empty())
64 {
65 SolarisNICMap.insert(NICPair("afe", "ADMtek Centaur/Comet Fast Ethernet"));
66 SolarisNICMap.insert(NICPair("aggr", "Link Aggregation Interface"));
67 SolarisNICMap.insert(NICPair("bge", "Broadcom BCM57xx Gigabit Ethernet"));
68 SolarisNICMap.insert(NICPair("ce", "Cassini Gigabit Ethernet"));
69 SolarisNICMap.insert(NICPair("chxge", "Chelsio Ethernet"));
70 SolarisNICMap.insert(NICPair("dmfe", "Davicom Fast Ethernet"));
71 SolarisNICMap.insert(NICPair("dnet", "DEC 21040/41 21140 Ethernet"));
72 SolarisNICMap.insert(NICPair("e1000", "Intel PRO/1000 Gigabit Ethernet"));
73 SolarisNICMap.insert(NICPair("e1000g", "Intel PRO/1000 Gigabit Ethernet"));
74 SolarisNICMap.insert(NICPair("elx", "3COM EtherLink III Ethernet"));
75 SolarisNICMap.insert(NICPair("elxl", "3COM Ethernet"));
76 SolarisNICMap.insert(NICPair("eri", "eri Fast Ethernet"));
77 SolarisNICMap.insert(NICPair("ge", "GEM Gigabit Ethernet"));
78 SolarisNICMap.insert(NICPair("hme", "SUNW,hme Fast-Ethernet"));
79 SolarisNICMap.insert(NICPair("hxge", "Sun Blade 10 Gigabit Ethernet"));
80 SolarisNICMap.insert(NICPair("igb", "Intel 82575 PCI-E Gigabit Ethernet"));
81 SolarisNICMap.insert(NICPair("ipge", "PCI-E Gigabit Ethernet"));
82 SolarisNICMap.insert(NICPair("iprb", "Intel 82557/58/59 Ethernet"));
83 SolarisNICMap.insert(NICPair("mxfe", "Macronix 98715 Fast Ethernet"));
84 SolarisNICMap.insert(NICPair("nfo", "Nvidia Gigabit Ethernet"));
85 SolarisNICMap.insert(NICPair("nge", "Nvidia Gigabit Ethernet"));
86 SolarisNICMap.insert(NICPair("pcelx", "3COM EtherLink III PCMCIA Ethernet"));
87 SolarisNICMap.insert(NICPair("pcn", "AMD PCnet Ethernet"));
88 SolarisNICMap.insert(NICPair("qfe", "SUNW,qfe Quad Fast-Ethernet"));
89 SolarisNICMap.insert(NICPair("rge", "Realtek Gigabit Ethernet"));
90 SolarisNICMap.insert(NICPair("rtls", "Realtek 8139 Fast Ethernet"));
91 SolarisNICMap.insert(NICPair("skge", "SksKonnect Gigabit Ethernet"));
92 SolarisNICMap.insert(NICPair("spwr", "SMC EtherPower II 10/100 (9432) Ethernet"));
93 SolarisNICMap.insert(NICPair("vboxnet", "VirtualBox Host Ethernet"));
94 SolarisNICMap.insert(NICPair("vlan", "Virtual LAN Ethernet"));
95 SolarisNICMap.insert(NICPair("vnic", "Virtual Network Interface Ethernet"));
96 SolarisNICMap.insert(NICPair("xge", "Neterior Xframe 10Gigabit Ethernet"));
97 }
98
99 /*
100 * Try picking up description from our NIC map.
101 */
102 char szNICInstance[128];
103 RTStrPrintf(szNICInstance, sizeof(szNICInstance), "%s%d", pszIface, Instance);
104 char szNICDesc[256];
105 std::string Description = SolarisNICMap[pszIface];
106 if (Description != "VirtualBox Host Ethernet")
107 {
108 if (Description != "")
109 RTStrPrintf(szNICDesc, sizeof(szNICDesc), "%s - %s", szNICInstance, Description.c_str());
110 else
111 RTStrPrintf(szNICDesc, sizeof(szNICDesc), "%s - Ethernet", szNICInstance);
112 }
113 else
114 RTStrPrintf(szNICDesc, sizeof(szNICDesc), "%s", szNICInstance);
115
116 /*
117 * Try to get IP V4 address and netmask as well as Ethernet address.
118 */
119 NETIFINFO Info;
120 memset(&Info, 0, sizeof(Info));
121 int Sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
122 if (Sock > 0)
123 {
124 struct lifreq IfReq;
125 strcpy(IfReq.lifr_name, szNICInstance);
126 if (ioctl(Sock, SIOCGLIFADDR, &IfReq) >= 0)
127 {
128 memcpy(Info.IPAddress.au8, &((struct sockaddr_in *)&IfReq.lifr_addr)->sin_addr.s_addr,
129 sizeof(Info.IPAddress.au8));
130 struct arpreq ArpReq;
131 memcpy(&ArpReq.arp_pa, &IfReq.lifr_addr, sizeof(struct sockaddr_in));
132
133 /*
134 * We might fail if the interface has not been assigned an IP address.
135 * That doesn't matter; as long as it's plumbed we can pick it up.
136 * But, if it has not acquired an IP address we cannot obtain it's MAC
137 * address this way, so we just use all zeros there.
138 */
139 if (ioctl(Sock, SIOCGARP, &ArpReq) >= 0)
140 {
141 memcpy(&Info.MACAddress, ArpReq.arp_ha.sa_data, sizeof(Info.MACAddress));
142 }
143
144 }
145
146 if (ioctl(Sock, SIOCGLIFNETMASK, &IfReq) >= 0)
147 {
148 memcpy(Info.IPNetMask.au8, &((struct sockaddr_in *)&IfReq.lifr_addr)->sin_addr.s_addr,
149 sizeof(Info.IPNetMask.au8));
150 }
151 if (ioctl(Sock, SIOCGLIFFLAGS, &IfReq) >= 0)
152 {
153 Info.enmStatus = IfReq.lifr_flags & IFF_UP ? NETIF_S_UP : NETIF_S_DOWN;
154 }
155 close(Sock);
156 }
157 /*
158 * Try to get IP V6 address and netmask.
159 */
160 Sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
161 if (Sock > 0)
162 {
163 struct lifreq IfReq;
164 strcpy(IfReq.lifr_name, szNICInstance);
165 if (ioctl(Sock, SIOCGLIFADDR, &IfReq) >= 0)
166 {
167 memcpy(Info.IPv6Address.au8, ((struct sockaddr_in6 *)&IfReq.lifr_addr)->sin6_addr.s6_addr,
168 sizeof(Info.IPv6Address.au8));
169 }
170 if (ioctl(Sock, SIOCGLIFNETMASK, &IfReq) >= 0)
171 {
172 memcpy(Info.IPv6NetMask.au8, ((struct sockaddr_in6 *)&IfReq.lifr_addr)->sin6_addr.s6_addr,
173 sizeof(Info.IPv6NetMask.au8));
174 }
175 close(Sock);
176 }
177
178 /*
179 * Construct UUID with interface name and the MAC address if available.
180 */
181 RTUUID Uuid;
182 RTUuidClear(&Uuid);
183 memcpy(&Uuid, szNICInstance, RT_MIN(strlen(szNICInstance), sizeof(Uuid)));
184 Uuid.Gen.u8ClockSeqHiAndReserved = (Uuid.Gen.u8ClockSeqHiAndReserved & 0x3f) | 0x80;
185 Uuid.Gen.u16TimeHiAndVersion = (Uuid.Gen.u16TimeHiAndVersion & 0x0fff) | 0x4000;
186 Uuid.Gen.au8Node[0] = Info.MACAddress.au8[0];
187 Uuid.Gen.au8Node[1] = Info.MACAddress.au8[1];
188 Uuid.Gen.au8Node[2] = Info.MACAddress.au8[2];
189 Uuid.Gen.au8Node[3] = Info.MACAddress.au8[3];
190 Uuid.Gen.au8Node[4] = Info.MACAddress.au8[4];
191 Uuid.Gen.au8Node[5] = Info.MACAddress.au8[5];
192 Info.Uuid = Uuid;
193 Info.enmMediumType = NETIF_T_ETHERNET;
194
195 HostNetworkInterfaceType_T enmType;
196 if (strncmp("vboxnet", szNICInstance, 7))
197 enmType = HostNetworkInterfaceType_Bridged;
198 else
199 enmType = HostNetworkInterfaceType_HostOnly;
200 ComObjPtr<HostNetworkInterface> IfObj;
201 IfObj.createObject();
202 if (SUCCEEDED(IfObj->init(Bstr(szNICDesc), enmType, &Info)))
203 pList->push_back(IfObj);
204}
205
206static boolean_t vboxSolarisAddLinkHostIface(const char *pszIface, void *pvHostNetworkInterfaceList)
207{
208 /*
209 * Skip IPSEC interfaces. It's at IP level.
210 */
211 if (!strncmp(pszIface, "ip.tun", 6))
212 return _B_FALSE;
213
214 /*
215 * Clip off the zone instance number from the interface name (if any).
216 */
217 char szIfaceName[128];
218 strcpy(szIfaceName, pszIface);
219 char *pszColon = (char *)memchr(szIfaceName, ':', sizeof(szIfaceName));
220 if (pszColon)
221 *pszColon = '\0';
222
223 /*
224 * Get the instance number from the interface name, then clip it off.
225 */
226 int cbInstance = 0;
227 int cbIface = strlen(szIfaceName);
228 const char *pszEnd = pszIface + cbIface - 1;
229 for (int i = 0; i < cbIface - 1; i++)
230 {
231 if (!RT_C_IS_DIGIT(*pszEnd))
232 break;
233 cbInstance++;
234 pszEnd--;
235 }
236
237 int Instance = atoi(pszEnd + 1);
238 strncpy(szIfaceName, pszIface, cbIface - cbInstance);
239 szIfaceName[cbIface - cbInstance] = '\0';
240
241 /*
242 * Add the interface.
243 */
244 vboxSolarisAddHostIface(szIfaceName, Instance, pvHostNetworkInterfaceList);
245
246 /*
247 * Continue walking...
248 */
249 return _B_FALSE;
250}
251
252static bool vboxSolarisSortNICList(const ComObjPtr<HostNetworkInterface> Iface1, const ComObjPtr<HostNetworkInterface> Iface2)
253{
254 Bstr Iface1Str;
255 (*Iface1).COMGETTER(Name) (Iface1Str.asOutParam());
256
257 Bstr Iface2Str;
258 (*Iface2).COMGETTER(Name) (Iface2Str.asOutParam());
259
260 return Iface1Str < Iface2Str;
261}
262
263static bool vboxSolarisSameNIC(const ComObjPtr<HostNetworkInterface> Iface1, const ComObjPtr<HostNetworkInterface> Iface2)
264{
265 Bstr Iface1Str;
266 (*Iface1).COMGETTER(Name) (Iface1Str.asOutParam());
267
268 Bstr Iface2Str;
269 (*Iface2).COMGETTER(Name) (Iface2Str.asOutParam());
270
271 return (Iface1Str == Iface2Str);
272}
273
274# ifdef VBOX_SOLARIS_NSL_RESOLVED
275static int vboxSolarisAddPhysHostIface(di_node_t Node, di_minor_t Minor, void *pvHostNetworkInterfaceList)
276{
277 NOREF(Minor);
278
279 /*
280 * Skip aggregations.
281 */
282 if (!strcmp(di_driver_name(Node), "aggr"))
283 return DI_WALK_CONTINUE;
284
285 /*
286 * Skip softmacs.
287 */
288 if (!strcmp(di_driver_name(Node), "softmac"))
289 return DI_WALK_CONTINUE;
290
291 vboxSolarisAddHostIface(di_driver_name(Node), di_instance(Node), pvHostNetworkInterfaceList);
292 return DI_WALK_CONTINUE;
293}
294# endif /* VBOX_SOLARIS_NSL_RESOLVED */
295
296int NetIfList(std::list <ComObjPtr<HostNetworkInterface> > &list)
297{
298
299# ifdef VBOX_SOLARIS_NSL_RESOLVED
300
301 /*
302 * Use libdevinfo for determining all physical interfaces.
303 */
304 di_node_t Root;
305 Root = di_init("/", DINFOCACHE);
306 if (Root != DI_NODE_NIL)
307 {
308 di_walk_minor(Root, DDI_NT_NET, 0, &list, vboxSolarisAddPhysHostIface);
309 di_fini(Root);
310 }
311
312 /*
313 * Use libdlpi for determining all DLPI interfaces.
314 */
315 if (VBoxSolarisLibDlpiFound())
316 g_pfnLibDlpiWalk(vboxSolarisAddLinkHostIface, &list, 0);
317
318# endif /* VBOX_SOLARIS_NSL_RESOLVED */
319
320 /*
321 * This gets only the list of all plumbed logical interfaces.
322 * This is needed for zones which cannot access the device tree
323 * and in this case we just let them use the list of plumbed interfaces
324 * on the zone.
325 */
326 int Sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
327 if (Sock > 0)
328 {
329 struct lifnum IfNum;
330 memset(&IfNum, 0, sizeof(IfNum));
331 IfNum.lifn_family = AF_INET;
332 int rc = ioctl(Sock, SIOCGLIFNUM, &IfNum);
333 if (!rc)
334 {
335 struct lifreq Ifaces[24];
336 struct lifconf IfConfig;
337 memset(&IfConfig, 0, sizeof(IfConfig));
338 IfConfig.lifc_family = AF_INET;
339 IfConfig.lifc_len = sizeof(Ifaces);
340 IfConfig.lifc_buf = (caddr_t)&(Ifaces[0]);
341 rc = ioctl(Sock, SIOCGLIFCONF, &IfConfig);
342 if (!rc)
343 {
344 for (int i = 0; i < IfNum.lifn_count; i++)
345 {
346 /*
347 * Skip loopback interfaces.
348 */
349 if (!strncmp(Ifaces[i].lifr_name, "lo", 2))
350 continue;
351
352#if 0
353 rc = ioctl(Sock, SIOCGLIFADDR, &(Ifaces[i]));
354 if (rc >= 0)
355 {
356 memcpy(Info.IPAddress.au8, ((struct sockaddr *)&Ifaces[i].lifr_addr)->sa_data,
357 sizeof(Info.IPAddress.au8));
358 // SIOCGLIFNETMASK
359 struct arpreq ArpReq;
360 memcpy(&ArpReq.arp_pa, &Ifaces[i].lifr_addr, sizeof(struct sockaddr_in));
361
362 /*
363 * We might fail if the interface has not been assigned an IP address.
364 * That doesn't matter; as long as it's plumbed we can pick it up.
365 * But, if it has not acquired an IP address we cannot obtain it's MAC
366 * address this way, so we just use all zeros there.
367 */
368 rc = ioctl(Sock, SIOCGARP, &ArpReq);
369 if (rc >= 0)
370 memcpy(&Info.MACAddress, ArpReq.arp_ha.sa_data, sizeof(Info.MACAddress));
371
372 char szNICDesc[LIFNAMSIZ + 256];
373 char *pszIface = Ifaces[i].lifr_name;
374 strcpy(szNICDesc, pszIface);
375
376 vboxSolarisAddLinkHostIface(pszIface, &list);
377 }
378#endif
379
380 char *pszIface = Ifaces[i].lifr_name;
381 vboxSolarisAddLinkHostIface(pszIface, &list);
382 }
383 }
384 }
385 close(Sock);
386 }
387
388 /*
389 * Weed out duplicates caused by dlpi_walk inconsistencies across Nevadas.
390 */
391 list.sort(vboxSolarisSortNICList);
392 list.unique(vboxSolarisSameNIC);
393
394 return VINF_SUCCESS;
395}
396
397#else
398int NetIfList(std::list <ComObjPtr<HostNetworkInterface> > &list)
399{
400 return VERR_NOT_IMPLEMENTED;
401}
402#endif
403
404int NetIfGetConfigByName(PNETIFINFO pInfo)
405{
406 NOREF(pInfo);
407 return VERR_NOT_IMPLEMENTED;
408}
409
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