7096 vioif should not log to the console on boot, or ever
[unleashed.git] / usr / src / uts / common / io / strplumb.c
blobd57bc150f58f997535a1ebc58f6767414c95abd5
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/param.h>
27 #include <sys/types.h>
28 #include <sys/user.h>
29 #include <sys/vfs.h>
30 #include <sys/vnode.h>
31 #include <sys/file.h>
32 #include <sys/stream.h>
33 #include <sys/stropts.h>
34 #include <sys/strsubr.h>
35 #include <sys/dlpi.h>
36 #include <sys/vnode.h>
37 #include <sys/socket.h>
38 #include <sys/sockio.h>
39 #include <net/if.h>
41 #include <sys/cred.h>
42 #include <sys/sysmacros.h>
44 #include <sys/sad.h>
45 #include <sys/kstr.h>
46 #include <sys/bootconf.h>
47 #include <sys/bootprops.h>
49 #include <sys/errno.h>
50 #include <sys/modctl.h>
51 #include <sys/sunddi.h>
52 #include <sys/sunldi.h>
53 #include <sys/esunddi.h>
54 #include <sys/promif.h>
56 #include <sys/strlog.h>
57 #include <sys/log.h>
58 #include <sys/ethernet.h>
59 #include <sys/ddi_implfuncs.h>
61 #include <sys/dld.h>
62 #include <sys/mac_client.h>
65 * Debug Macros
67 int strplumbdebug = 0;
69 extern ib_boot_prop_t *iscsiboot_prop;
71 #define DBG0(_f) \
72 if (strplumbdebug != 0) \
73 printf("strplumb: " _f)
75 #define DBG1(_f, _a) \
76 if (strplumbdebug != 0) \
77 printf("strplumb: " _f, (_a))
79 #define DBG2(_f, _a, _b) \
80 if (strplumbdebug != 0) \
81 printf("strplumb: " _f, (_a), (_b))
83 #define DBG3(_f, _a, _b, _c) \
84 if (strplumbdebug != 0) \
85 printf("strplumb: " _f, (_a), (_b), (_c))
88 * Module linkage information for the kernel.
90 #define STRPLUMB_IDENT "STREAMS Plumbing Module"
92 static struct modlmisc modlmisc = {
93 &mod_miscops,
94 STRPLUMB_IDENT
97 static struct modlinkage modlinkage = {
98 MODREV_1,
99 &modlmisc,
100 NULL
104 _init(void)
106 return (mod_install(&modlinkage));
110 _fini(void)
112 return (mod_remove(&modlinkage));
116 _info(struct modinfo *modinfop)
118 return (mod_info(&modlinkage, modinfop));
121 #define ARP "arp"
122 #define TCP "tcp"
123 #define TCP6 "tcp6"
124 #define UDP "udp"
125 #define UDP6 "udp6"
126 #define ICMP "icmp"
127 #define ICMP6 "icmp6"
128 #define IP "ip"
129 #define IP6 "ip6"
130 #define TIMOD "timod"
132 #define UDPDEV "/devices/pseudo/udp@0:udp"
133 #define TCP6DEV "/devices/pseudo/tcp6@0:tcp6"
134 #define UDP6DEV "/devices/pseudo/udp6@0:udp6"
135 #define IP6DEV "/devices/pseudo/ip6@0:ip6"
137 typedef struct strplumb_modspec {
138 char *sm_type;
139 char *sm_name;
140 } strplumb_modspec_t;
142 static strplumb_modspec_t strplumb_modlist[] = {
143 { "drv", DLD_DRIVER_NAME },
144 { "drv", IP },
145 { "drv", IP6 },
146 { "drv", TCP },
147 { "drv", TCP6 },
148 { "drv", UDP },
149 { "drv", UDP6 },
150 { "drv", ICMP },
151 { "drv", ICMP6 },
152 { "drv", ARP },
153 { "strmod", TIMOD }
157 * Called from swapgeneric.c:loadrootmodules() in the network boot case.
160 strplumb_load(void)
162 uint_t i;
163 strplumb_modspec_t *p;
165 DBG0("loading modules\n");
167 for (i = 0, p = strplumb_modlist;
168 i < sizeof (strplumb_modlist) / sizeof (strplumb_modlist[0]);
169 i++, p++) {
170 if (modloadonly(p->sm_type, p->sm_name) < 0) {
171 printf("strplumb: failed to load %s/%s\n",
172 p->sm_type, p->sm_name);
173 return (EFAULT);
177 return (0);
180 static int
181 strplumb_init(void)
183 uint_t i;
184 strplumb_modspec_t *p;
185 int err;
187 DBG0("initializing modules\n");
189 for (i = 0, p = strplumb_modlist;
190 i < sizeof (strplumb_modlist) / sizeof (strplumb_modlist[0]);
191 i++, p++) {
192 if (strcmp(p->sm_type, "drv") == 0)
193 err = (i_ddi_attach_pseudo_node(p->sm_name) != NULL) ?
194 0 : EFAULT;
195 else
196 err = (modload(p->sm_type, p->sm_name) < 0) ?
197 EFAULT : 0;
199 if (err != 0) {
200 printf("strplumb: failed to initialize %s/%s\n",
201 p->sm_type, p->sm_name);
202 return (err);
206 return (0);
210 * Can be set in /etc/system in the case of local booting. See comment below.
212 char *ndev_name = 0;
213 int ndev_unit = 0;
216 * If we booted diskless then strplumb() will have been called from
217 * either:
218 * in case of x86 NEWBOOT: vfs.c:rootconf()
219 * in case of nfs root, the rootfs.bo_name is reset from /ramdisk:a
220 * to empty string and we will copy netdev_path there.
221 * or
222 * in case of sparc: swapgeneric.c:rootconf().
223 * All we can do in that case is plumb the network device that we booted from.
225 * If we booted from a local disk, we will have been called from main(),
226 * and normally we defer the plumbing of interfaces until network/physical.
227 * This can be overridden by setting "ndev_name" in /etc/system.
229 static int
230 resolve_boot_path(void)
232 char *devpath;
233 dev_info_t *dip;
234 const char *driver;
235 int instance;
236 #ifdef _OBP
237 char stripped_path[OBP_MAXPATHLEN];
238 #endif
240 if (strncmp(rootfs.bo_fstype, "nfs", 3) == 0 &&
241 rootfs.bo_name[0] != '\0') {
242 devpath = rootfs.bo_name;
243 } else {
244 devpath = strplumb_get_netdev_path();
245 netdev_path = devpath;
246 if (netdev_path != NULL) {
247 (void) strncpy(rootfs.bo_name, netdev_path,
248 BO_MAXOBJNAME);
252 if (devpath != NULL) {
253 DBG1("resolving boot-path: %s\n", devpath);
254 #ifdef _OBP
256 * OBP passes options e.g, "net:dhcp"
257 * remove them here
259 prom_strip_options(devpath, stripped_path);
260 devpath = stripped_path;
261 #endif
263 * Hold the devi since this is the root device.
265 if ((dip = e_ddi_hold_devi_by_path(devpath, 0)) == NULL) {
266 printf("strplumb: unable to hold root device: %s\n",
267 devpath);
268 return (ENXIO);
271 driver = ddi_driver_name(dip);
272 instance = ddi_get_instance(dip);
273 } else {
274 if (ndev_name == NULL)
275 return (ENODEV);
277 DBG2("using ndev_name (%s) ndev_unit (%d)\n", ndev_name,
278 ndev_unit);
280 if (i_ddi_attach_hw_nodes(ndev_name) != DDI_SUCCESS) {
281 printf("strplumb: cannot load ndev_name '%s'\n",
282 ndev_name);
283 return (ENXIO);
286 driver = ndev_name;
287 instance = ndev_unit;
290 (void) snprintf(rootfs.bo_devname, BO_MAXOBJNAME,
291 "/devices/pseudo/clone@0:%s", driver);
292 (void) snprintf(rootfs.bo_ifname, BO_MAXOBJNAME, "%s%d",
293 driver, instance);
294 rootfs.bo_ppa = instance;
295 return (0);
298 static int
299 getifflags(ldi_handle_t lh, struct lifreq *lifrp)
301 struct strioctl iocb;
302 int rval;
304 iocb.ic_cmd = SIOCGLIFFLAGS;
305 iocb.ic_timout = 15;
306 iocb.ic_len = sizeof (struct lifreq);
307 iocb.ic_dp = (char *)lifrp;
309 return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, CRED(), &rval));
313 static int
314 setifname(ldi_handle_t lh, struct lifreq *lifrp)
316 struct strioctl iocb;
317 int rval;
319 iocb.ic_cmd = SIOCSLIFNAME;
320 iocb.ic_timout = 15;
321 iocb.ic_len = sizeof (struct lifreq);
322 iocb.ic_dp = (char *)lifrp;
324 return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, CRED(), &rval));
327 static int
328 strplumb_dev(ldi_ident_t li)
330 ldi_handle_t lh = NULL;
331 ldi_handle_t mux_lh = NULL;
332 int err;
333 struct lifreq lifr;
334 struct ifreq ifr;
335 int rval;
336 int af = 0;
337 char *name = NULL;
339 bzero(&lifr, sizeof (struct lifreq));
340 bzero(&ifr, sizeof (ifr));
342 if (iscsiboot_prop != NULL) {
343 af = iscsiboot_prop->boot_nic.sin_family;
347 * Now set up the links. Ultimately, we should have two streams
348 * permanently linked under UDP. One stream consists of the
349 * ARP-[ifname] combination, while the other consists of IP-[ifname].
351 * We pin underneath UDP here to match what is done in ifconfig(1m);
352 * otherwise, ifconfig will be unable to unplumb the stream (the major
353 * number and mux id must both match for a successful I_PUNLINK).
355 * There are subtleties in the plumbing which make it essential to
356 * follow the logic used in ifconfig(1m) very closely.
360 * Plumb UDP-IP-<dev>
363 if ((err = ldi_open_by_name(rootfs.bo_devname, FREAD|FWRITE, CRED(),
364 &lh, li)) != 0) {
365 printf("strplumb: open %s failed: %d\n", rootfs.bo_devname,
366 err);
367 goto done;
371 if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)IP, FKIOCTL, CRED(),
372 &rval)) != 0) {
373 printf("strplumb: push IP failed: %d\n", err);
374 goto done;
377 if ((err = getifflags(lh, &lifr)) != 0)
378 goto done;
380 if (af == 0 || af == AF_INET) {
381 lifr.lifr_flags |= IFF_IPV4;
382 lifr.lifr_flags &= ~IFF_IPV6;
383 name = UDPDEV;
384 } else {
386 * iscsi boot is used with ipv6 enabled
388 lifr.lifr_flags |= IFF_IPV6;
389 lifr.lifr_flags &= ~IFF_IPV4;
390 name = UDP6DEV;
392 (void) strlcpy(lifr.lifr_name, rootfs.bo_ifname,
393 sizeof (lifr.lifr_name));
394 lifr.lifr_ppa = rootfs.bo_ppa;
396 if ((err = setifname(lh, &lifr)) != 0)
397 goto done;
399 /* get the flags and check if ARP is needed */
400 if ((err = getifflags(lh, &lifr)) != 0) {
401 printf("strplumb: getifflags %s IP failed, error %d\n",
402 lifr.lifr_name, err);
403 goto done;
405 if ((err = ldi_open_by_name(name, FREAD|FWRITE, CRED(), &mux_lh,
406 li)) != 0) {
407 printf("strplumb: open of %s failed: %d\n", name, err);
408 goto done;
410 if ((err = ldi_ioctl(mux_lh, I_PLINK, (intptr_t)lh,
411 FREAD|FWRITE|FNOCTTY|FKIOCTL, CRED(),
412 &(ifr.ifr_ip_muxid))) != 0) {
413 printf("strplumb: plink UDP-ARP-IP-%s failed: %d\n",
414 rootfs.bo_ifname, err);
415 goto done;
418 /* if ARP is not needed, we are done */
419 if (lifr.lifr_flags & (IFF_NOARP | IFF_IPV6))
420 goto done;
422 DBG2("UDP-ARP-IP-%s muxid: %d\n", rootfs.bo_ifname, ifr.ifr_ip_muxid);
424 (void) ldi_close(lh, FREAD|FWRITE, CRED());
425 lh = NULL;
428 * Plumb UDP-ARP-<dev>
431 if ((err = ldi_open_by_name(rootfs.bo_devname, FREAD|FWRITE, CRED(),
432 &lh, li)) != 0) {
433 printf("strplumb: open %s failed: %d\n", rootfs.bo_devname,
434 err);
435 goto done;
438 if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)ARP, FKIOCTL, CRED(),
439 &rval)) != 0) {
440 printf("strplumb: push ARP failed: %d\n", err);
441 goto done;
444 if ((err = setifname(lh, &lifr)) != 0)
445 goto done;
447 if ((err = ldi_ioctl(mux_lh, I_PLINK, (intptr_t)lh,
448 FREAD|FWRITE|FNOCTTY|FKIOCTL, CRED(),
449 &(ifr.ifr_arp_muxid))) != 0) {
450 printf("strplumb: plink UDP-ARP-%s failed: %d\n",
451 rootfs.bo_ifname, err);
452 goto done;
455 DBG2("UDP-ARP-%s muxid: %d\n", rootfs.bo_ifname, ifr.ifr_arp_muxid);
458 * Cache the mux ids.
460 (void) strlcpy(ifr.ifr_name, rootfs.bo_ifname, sizeof (ifr.ifr_name));
462 if ((err = ldi_ioctl(mux_lh, SIOCSIFMUXID, (intptr_t)&ifr, FKIOCTL,
463 CRED(), &rval)) != 0) {
464 printf("strplumb: SIOCSIFMUXID failed: %d\n", err);
465 goto done;
468 done:
469 if (lh != NULL)
470 (void) ldi_close(lh, FREAD|FWRITE, CRED());
472 if (mux_lh != NULL)
473 (void) ldi_close(mux_lh, FREAD|FWRITE, CRED());
475 return (err);
479 * Do streams plumbing for internet protocols.
482 strplumb(void)
484 ldi_ident_t li;
485 int err;
487 if ((err = strplumb_init()) != 0)
488 return (err);
490 if ((err = ldi_ident_from_mod(&modlinkage, &li)) != 0)
491 return (err);
493 if ((err = resolve_boot_path()) != 0)
494 goto done;
496 DBG1("rootfs.bo_devname: %s\n", rootfs.bo_devname);
497 DBG1("rootfs.bo_ifname: %s\n", rootfs.bo_ifname);
498 DBG1("rootfs.bo_ppa: %d\n", rootfs.bo_ppa);
500 if ((err = strplumb_dev(li)) != 0)
501 goto done;
503 done:
504 ldi_ident_release(li);
506 return (err);
509 /* multiboot: diskless boot interface discovery */
511 #ifndef _OBP
513 static uchar_t boot_macaddr[16];
514 static int boot_maclen;
515 static uchar_t *getmacaddr(dev_info_t *dip, size_t *maclenp);
516 static int matchmac(dev_info_t *dip, void *arg);
518 #endif /* !_OBP */
520 char *
521 strplumb_get_netdev_path(void)
523 #ifdef _OBP
524 char fstype[OBP_MAXPROPNAME];
525 static char iscsi_network_path[BO_MAXOBJNAME] = {0};
526 int proplen;
527 char *p = NULL;
529 if (bop_getprop("fstype", fstype) == -1)
530 return (NULL);
532 if (strncmp(fstype, "nfs", 3) == 0)
533 return (prom_bootpath());
534 else if (iscsiboot_prop != NULL) {
535 proplen = BOP_GETPROPLEN(bootops,
536 BP_ISCSI_NETWORK_BOOTPATH);
537 if (proplen > 0) {
538 if (BOP_GETPROP(bootops,
539 BP_ISCSI_NETWORK_BOOTPATH,
540 iscsi_network_path) > 0) {
541 p = strchr(iscsi_network_path, ':');
542 if (p != NULL) {
543 *p = '\0';
545 return (iscsi_network_path);
549 return (NULL);
550 #else
552 char *macstr, *devpath = NULL;
553 uchar_t *bootp;
554 uint_t bootp_len;
556 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
557 DDI_PROP_DONTPASS, BP_BOOT_MAC, &macstr) == DDI_SUCCESS) {
559 * hard coded ether mac len for booting floppy on
560 * machines with old cards
562 boot_maclen = ether_aton(macstr, boot_macaddr);
563 if (boot_maclen != 6) {
564 cmn_err(CE_WARN,
565 "malformed boot_mac property, %d bytes",
566 boot_maclen);
568 ddi_prop_free(macstr);
569 } else if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, ddi_root_node(),
570 DDI_PROP_DONTPASS, BP_BOOTP_RESPONSE, &bootp, &bootp_len)
571 == DDI_SUCCESS) {
574 * These offsets are defined by dhcp standard
575 * Should use structure offsets
577 boot_maclen = *(bootp + 2);
578 ASSERT(boot_maclen <= 16);
579 bcopy(bootp + 28, boot_macaddr, boot_maclen);
581 dhcack = kmem_alloc(bootp_len, KM_SLEEP);
582 bcopy(bootp, dhcack, bootp_len);
583 dhcacklen = bootp_len;
585 ddi_prop_free(bootp);
586 } else if (iscsiboot_prop != NULL) {
587 bcopy(iscsiboot_prop->boot_nic.nic_mac,
588 boot_macaddr, IB_BOOT_MACLEN);
589 boot_maclen = IB_BOOT_MACLEN;
590 } else {
591 return (NULL);
594 ddi_walk_devs(ddi_root_node(), matchmac, (void *)&devpath);
595 return (devpath);
597 #endif /* _OBP */
600 #ifndef _OBP
603 * Get boot path from the boot_mac address
605 /*ARGSUSED*/
606 static int
607 matchmac(dev_info_t *dip, void *arg)
609 char **devpathp = (char **)arg;
610 char *model_str;
611 uchar_t *macaddr;
612 size_t maclen;
614 /* XXX Should use "device-type" per IEEE 1275 */
615 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, 0,
616 "model", &model_str) != DDI_SUCCESS)
617 return (DDI_WALK_CONTINUE);
619 if (strcmp(model_str, "Ethernet controller") != 0) {
620 ddi_prop_free(model_str);
621 return (DDI_WALK_CONTINUE);
623 ddi_prop_free(model_str);
625 /* We have a network device now */
626 if (i_ddi_attach_node_hierarchy(dip) != DDI_SUCCESS) {
627 return (DDI_WALK_CONTINUE);
630 ASSERT(boot_maclen != 0);
631 macaddr = getmacaddr(dip, &maclen);
632 if (macaddr == NULL)
633 return (DDI_WALK_CONTINUE);
635 if (maclen != boot_maclen ||
636 bcmp(macaddr, boot_macaddr, maclen) != 0) {
637 kmem_free(macaddr, maclen);
638 return (DDI_WALK_CONTINUE);
641 /* found hardware with the mac address */
642 (void) localetheraddr((struct ether_addr *)macaddr, NULL);
643 kmem_free(macaddr, maclen);
645 *devpathp = kmem_alloc(MAXPATHLEN, KM_SLEEP);
646 (void) ddi_pathname(dip, *devpathp);
648 /* fill in dhcifname */
649 if (dhcack) {
650 (void) snprintf(dhcifname, IFNAMSIZ, "%s%d",
651 ddi_driver_name(dip), i_ddi_devi_get_ppa(dip));
653 return (DDI_WALK_TERMINATE);
656 static uchar_t *
657 getmacaddr(dev_info_t *dip, size_t *maclenp)
659 int rc, ppa;
660 ldi_ident_t li;
661 ldi_handle_t lh;
662 const char *drv_name = ddi_driver_name(dip);
663 char *clonepath;
664 uchar_t *macaddr = NULL;
666 if (rc = ldi_ident_from_mod(&modlinkage, &li)) {
667 cmn_err(CE_WARN,
668 "getmacaddr: ldi_ident_from_mod failed: %d\n", rc);
669 return (NULL);
672 clonepath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
673 (void) snprintf(clonepath, MAXPATHLEN,
674 "/devices/pseudo/clone@0:%s", drv_name);
676 rc = ldi_open_by_name(clonepath, FREAD|FWRITE, CRED(), &lh, li);
677 ldi_ident_release(li);
678 if (rc) {
679 cmn_err(CE_WARN,
680 "getmacaddr: ldi_open_by_name(%s) failed: %d\n",
681 clonepath, rc);
682 kmem_free(clonepath, MAXPATHLEN);
683 return (NULL);
685 kmem_free(clonepath, MAXPATHLEN);
687 ppa = i_ddi_devi_get_ppa(dip);
688 if ((dl_attach(lh, ppa, NULL) != 0) ||
689 (dl_bind(lh, ETHERTYPE_IP, NULL) != 0)) {
690 (void) ldi_close(lh, FREAD|FWRITE, CRED());
691 cmn_err(CE_WARN,
692 "getmacaddr: dl_attach/bind(%s%d) failed: %d\n",
693 drv_name, ppa, rc);
694 return (NULL);
697 *maclenp = ETHERADDRL;
698 macaddr = kmem_alloc(ETHERADDRL, KM_SLEEP);
699 if (dl_phys_addr(lh, macaddr, maclenp, NULL) != 0 ||
700 *maclenp != ETHERADDRL) {
701 kmem_free(macaddr, ETHERADDRL);
702 macaddr = NULL;
703 *maclenp = 0;
704 cmn_err(CE_WARN,
705 "getmacaddr: dl_phys_addr(%s%d) failed: %d\n",
706 drv_name, ppa, rc);
708 (void) ldi_close(lh, FREAD|FWRITE, CRED());
709 return (macaddr);
711 #endif /* !_OBP */