1 /*
   2  * This file and its contents are supplied under the terms of the
   3  * Common Development and Distribution License ("CDDL"), version 1.0.
   4  * You may only use this file in accordance with the terms of version
   5  * 1.0 of the CDDL.
   6  *
   7  * A full copy of the text of the CDDL should have accompanied this
   8  * source.  A copy of the CDDL is also available via the Internet at
   9  * http://www.illumos.org/license/CDDL.
  10  */
  11 
  12 /*
  13  * Copyright 2012 Nexenta Systems, Inc.  All rights reserved.
  14  */
  15 
  16 #include <sys/aoe.h>
  17 #include <sys/list.h>
  18 #include <sys/param.h>
  19 #include <sys/stat.h>
  20 #include <sys/types.h>
  21 
  22 #include <assert.h>
  23 #include <ctype.h>
  24 #include <errno.h>
  25 #include <fcntl.h>
  26 #include <inttypes.h>
  27 #include <libaoe.h>
  28 #include <libdllink.h>
  29 #include <libintl.h>
  30 #include <libscf.h>
  31 #include <locale.h>
  32 #include <locale.h>
  33 #include <stddef.h>
  34 #include <stdlib.h>
  35 #include <string.h>
  36 #include <strings.h>
  37 #include <syslog.h>
  38 #include <wchar.h>
  39 #include <libipadm.h>
  40 
  41 int
  42 main(void)
  43 {
  44         AOE_STATUS              status;
  45         PAOE_SMF_PORT_LIST      portlist = NULL;
  46         PAOE_SMF_PORT_INSTANCE  port = NULL;
  47         int                     i;
  48         ipadm_handle_t          iph;
  49         ipadm_status_t          rc;
  50 
  51         (void) setlocale(LC_ALL, "");
  52 
  53         /*
  54          * Loading configuration from the Service Management Facility.
  55          * Note: Loading the configuration from SMF is done in libaoe.
  56          *      Please see the aoe_load_config routine in libaoe.c
  57          *      for the procedure of doing this.
  58          */
  59         status = aoe_load_config(AOE_PORTTYPE_INITIATOR, &portlist);
  60         if (status != AOE_STATUS_OK) {
  61                 syslog(LOG_ERR, "Failed loading AoE configuration "
  62                     "from SMF. status=%d", status);
  63                 return (1);
  64         }
  65 
  66         if (portlist == NULL)
  67                 return (0);
  68 
  69         if (ipadm_open(&iph, IPH_IPMGMTD) != IPADM_SUCCESS) {
  70                 syslog(LOG_ERR, "Could not open ipadm handle.");
  71                 return (1);
  72         }
  73 
  74         for (i = 0; i < portlist->port_num; i++) {
  75                 char    buf[MAXLINKNAMELEN];
  76                 char    *linkname = NULL;
  77 
  78                 port = &portlist->ports[i];
  79                 (void) strlcpy(buf, port->linknames, MAXLINKNAMELEN);
  80                 linkname = strtok(buf, ",");
  81                 while (linkname != NULL) {
  82                         /* Unplumb the interfaces */
  83                         rc = ipadm_delete_if(iph, linkname,
  84                             AF_INET, IPADM_OPT_ACTIVE);
  85                         if (rc != IPADM_SUCCESS)
  86                                 syslog(LOG_ERR,
  87                                     "Failed unplumbing %s, rc=%d",
  88                                     linkname, rc);
  89                         linkname = strtok(NULL, ",");
  90                 }
  91                 if (port->type == AOE_PORTTYPE_INITIATOR) {
  92                         status = aoe_create_port(port->id, port->linknames,
  93                             port->promisc, AOE_PORTTYPE_INITIATOR,
  94                             port->policy, port->module);
  95                         if (status != AOE_STATUS_OK) {
  96                                 syslog(LOG_ERR, "Failed creating AoE port. "
  97                                     "port_id=%d, status=%d", port->id, status);
  98                         }
  99                 }
 100         }
 101 
 102         if (portlist != NULL)
 103                 free(portlist);
 104 
 105         ipadm_close(iph);
 106         return (0);
 107 }