Print this page
Commit IPMP changes

Split Close
Expand all
Collapse all
          --- old/usr/src/lib/libipadm/common/ipadm_persist.c
          +++ new/usr/src/lib/libipadm/common/ipadm_persist.c
↓ open down ↓ 12 lines elided ↑ open up ↑
  13   13   * When distributing Covered Code, include this CDDL HEADER in each
  14   14   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  /*
  22   22   * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
       23 + * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
  23   24   */
  24   25  
  25   26  /*
  26   27   * This file contains routines to read/write formatted entries from/to
  27   28   * libipadm data store /etc/ipadm/ipadm.conf. Each entry in the DB is a
  28   29   * series of IPADM_NVPAIR_SEP separated (name, value) pairs, as shown
  29   30   * below:
  30   31   *              name=value[;...]
  31   32   *
  32   33   * The 'name' determines how to interpret 'value'. The supported names are:
↓ open down ↓ 11 lines elided ↑ open up ↑
  44   45   *                      interface_id: DATA_TYPE_UINT8_ARRAY
  45   46   *                      prefixlen: DATA_TYPE_UINT32
  46   47   *                      stateless: DATA_TYPE_STRING
  47   48   *                      stateful: DATA_TYPE_STRING
  48   49   *
  49   50   *  IPADM_NVP_DHCP - value holds wait time and primary info and when converted
  50   51   *             to nvlist, will contain following nvpairs
  51   52   *                      wait:   DATA_TYPE_INT32
  52   53   *                      primary: DATA_TYPE_BOOLEAN
  53   54   *
       55 + *      IPADM_NVP_FAMILIES - value holds interface families and when converted
       56 + *              to nvlist, will be a DATA_TYPE_UINT16_ARRAY
       57 + *
       58 + *  IPADM_NVP_MIFNAMES - value holds IPMP group members and when converted
       59 + *          to nvlist, will be a DATA_TYPE_STRING_ARRAY
       60 + *
  54   61   *  default  - value is a single entity and when converted to nvlist, will
  55   62   *             contain nvpair of type DATA_TYPE_STRING. nvpairs private to
  56   63   *             ipadm are of this type. Further the property name and property
  57   64   *             values are stored as nvpairs of this type.
  58   65   *
  59   66   * The syntax for each line is described above the respective functions below.
  60   67   */
  61   68  
  62   69  #include <stdlib.h>
  63   70  #include <strings.h>
↓ open down ↓ 27 lines elided ↑ open up ↑
  91   98  typedef size_t  ipadm_wfunc_t(nvpair_t *, char *, size_t);
  92   99  
  93  100  /*
  94  101   * ipadm_rfunc_t takes (`name', `value') and adds the appropriately typed
  95  102   * nvpair to the nvlist.
  96  103   */
  97  104  typedef void  ipadm_rfunc_t(nvlist_t *, char *name, char *value);
  98  105  
  99  106  static ipadm_rfunc_t    i_ipadm_str_dbline2nvl, i_ipadm_ip4_dbline2nvl,
 100  107                          i_ipadm_ip6_dbline2nvl, i_ipadm_intfid_dbline2nvl,
 101      -                        i_ipadm_dhcp_dbline2nvl;
      108 +                        i_ipadm_dhcp_dbline2nvl, i_ipadm_families_dbline2nvl,
      109 +                        i_ipadm_groupmembers_dbline2nvl;
 102  110  
 103  111  static ipadm_wfunc_t    i_ipadm_str_nvp2dbline, i_ipadm_ip4_nvp2dbline,
 104  112                          i_ipadm_ip6_nvp2dbline, i_ipadm_intfid_nvp2dbline,
 105      -                        i_ipadm_dhcp_nvp2dbline;
      113 +                        i_ipadm_dhcp_nvp2dbline, i_ipadm_families_nvp2dbline,
      114 +                        i_ipadm_groupmembers_nvp2dbline;
 106  115  
 107  116  /*
 108  117   * table of function pointers to read/write formatted entries from/to
 109  118   * ipadm.conf.
 110  119   */
 111  120  typedef struct ipadm_conf_ent_s {
 112  121          const char              *ipent_type_name;
 113  122          ipadm_wfunc_t           *ipent_wfunc;
 114  123          ipadm_rfunc_t           *ipent_rfunc;
 115  124  } ipadm_conf_ent_t;
 116  125  
 117  126  static ipadm_conf_ent_t ipadm_conf_ent[] = {
 118  127          { IPADM_NVP_IPV6ADDR, i_ipadm_ip6_nvp2dbline, i_ipadm_ip6_dbline2nvl },
 119  128          { IPADM_NVP_IPV4ADDR, i_ipadm_ip4_nvp2dbline, i_ipadm_ip4_dbline2nvl },
 120  129          { IPADM_NVP_INTFID, i_ipadm_intfid_nvp2dbline,
 121  130              i_ipadm_intfid_dbline2nvl },
 122  131          { IPADM_NVP_DHCP, i_ipadm_dhcp_nvp2dbline, i_ipadm_dhcp_dbline2nvl },
      132 +        { IPADM_NVP_FAMILIES, i_ipadm_families_nvp2dbline,
      133 +            i_ipadm_families_dbline2nvl },
      134 +        { IPADM_NVP_MIFNAMES, i_ipadm_groupmembers_nvp2dbline,
      135 +            i_ipadm_groupmembers_dbline2nvl},
 123  136          { NULL, i_ipadm_str_nvp2dbline, i_ipadm_str_dbline2nvl }
 124  137  };
 125  138  
 126  139  static ipadm_conf_ent_t *
 127  140  i_ipadm_find_conf_type(const char *type)
 128  141  {
 129  142          int     i;
 130  143  
 131  144          for (i = 0; ipadm_conf_ent[i].ipent_type_name != NULL; i++)
 132  145                  if (strcmp(type, ipadm_conf_ent[i].ipent_type_name) == 0)
↓ open down ↓ 475 lines elided ↑ open up ↑
 608  621          assert(cp != NULL);
 609  622          *cp++ = '\0';
 610  623          errno = 0;
 611  624          wait_time = strtol(value, &endp, 10);
 612  625          if (*endp != '\0' || errno != 0)
 613  626                  return;
 614  627          primary = (strcmp(cp, "yes") == 0);
 615  628          (void) i_ipadm_add_dhcp2nvl(nvl, primary, (int32_t)wait_time);
 616  629  }
 617  630  
      631 +/*
      632 + * Input 'nvp': name = IPADM_NVP_FAMILIES and value = array of 'uint16_t'
      633 + *
      634 + *
      635 + */
      636 +static size_t
      637 +i_ipadm_families_nvp2dbline(nvpair_t *nvp, char *buf, size_t buflen)
      638 +{
      639 +        uint_t nelem = 0;
      640 +        uint16_t *elem;
      641 +
      642 +        assert(nvpair_type(nvp) == DATA_TYPE_UINT16_ARRAY);
      643 +
      644 +        if (nvpair_value_uint16_array(nvp,
      645 +            &elem, &nelem) != 0) {
      646 +                buf[0] = '\0';
      647 +                return (0);
      648 +        }
      649 +
      650 +        assert(nelem != 0 || nelem > 2);
      651 +
      652 +        if (nelem == 1) {
      653 +                return (snprintf(buf, buflen, "%s=%d",
      654 +                    nvpair_name(nvp), elem[0]));
      655 +        } else {
      656 +                return (snprintf(buf, buflen, "%s=%d,%d",
      657 +                    nvpair_name(nvp), elem[0], elem[1]));
      658 +        }
      659 +}
      660 +
      661 +/*
      662 + * name = IPADM_NVP_FAMILIES and value = <FAMILY>[,FAMILY]
      663 + *
      664 + * output nvp: name = IPADM_NVP_FAMILIES and value = array of 'uint16_t'
      665 + *
      666 + */
      667 +static void
      668 +i_ipadm_families_dbline2nvl(nvlist_t *nvl, char *name, char *value)
      669 +{
      670 +        uint16_t        families[2];
      671 +        uint_t  nelem = 0;
      672 +        char    *val, *lasts;
      673 +
      674 +        if ((val = strtok_r(value,
      675 +            ",", &lasts)) != NULL) {
      676 +                families[0] = atoi(val);
      677 +                nelem++;
      678 +                if ((val = strtok_r(NULL,
      679 +                    ",", &lasts)) != NULL) {
      680 +                        families[1] = atoi(val);
      681 +                        nelem++;
      682 +                }
      683 +                nvlist_add_uint16_array(nvl,
      684 +                    IPADM_NVP_FAMILIES, families, nelem);
      685 +        }
      686 +}
      687 +
      688 +/*
      689 + * input nvp: name = IPADM_NVP_MIFNAMES and value = array of 'char *'
      690 + *
      691 + *
      692 + */
      693 +static size_t
      694 +i_ipadm_groupmembers_nvp2dbline(nvpair_t *nvp, char *buf, size_t buflen)
      695 +{
      696 +        uint_t nelem = 0;
      697 +        char **elem;
      698 +        size_t n;
      699 +
      700 +        assert(nvpair_type(nvp) == DATA_TYPE_STRING_ARRAY);
      701 +
      702 +        if (nvpair_value_string_array(nvp,
      703 +            &elem, &nelem) != 0) {
      704 +                buf[0] = '\0';
      705 +                return (0);
      706 +        }
      707 +
      708 +        assert(nelem != 0);
      709 +
      710 +        n = snprintf(buf, buflen, "%s=", IPADM_NVP_MIFNAMES);
      711 +        if (n >= buflen)
      712 +                return (n);
      713 +
      714 +        while (nelem--) {
      715 +                n = strlcat(buf, elem[nelem], buflen);
      716 +                if (nelem > 0)
      717 +                        n = strlcat(buf, ",", buflen);
      718 +
      719 +                if (n > buflen)
      720 +                        return (n);
      721 +        }
      722 +
      723 +        return (n);
      724 +}
      725 +
      726 +/*
      727 + * name = IPADM_NVP_MIFNAMES and value = <if_name>[,if_name]
      728 + *
      729 + * output nvp: name = IPADM_NVP_MIFNAMES and value = array of 'char *'
      730 + */
      731 +static void
      732 +i_ipadm_groupmembers_dbline2nvl(nvlist_t *nvl, char *name, char *value)
      733 +{
      734 +        char    *members[256];
      735 +        char    *member;
      736 +        char    *val, *lasts;
      737 +        uint_t  m_cnt = 0;
      738 +
      739 +        assert(strcmp(name, IPADM_NVP_MIFNAMES) == 0 && value != NULL);
      740 +
      741 +        if ((val = strtok_r(value, ",", &lasts)) != NULL) {
      742 +                if ((member = calloc(1, LIFNAMSIZ)) == NULL)
      743 +                        return;
      744 +
      745 +                strlcpy(member, val, LIFNAMSIZ);
      746 +                members[m_cnt++] = member;
      747 +
      748 +                while ((val = strtok_r(NULL, ",", &lasts)) != NULL) {
      749 +                        if ((member = calloc(1, LIFNAMSIZ)) == NULL)
      750 +                                goto fail;
      751 +
      752 +                        strlcpy(member, val, LIFNAMSIZ);
      753 +                        members[m_cnt++] = member;
      754 +                }
      755 +
      756 +                nvlist_add_string_array(nvl, IPADM_NVP_MIFNAMES,
      757 +                    members, m_cnt);
      758 +        }
      759 +
      760 +fail:
      761 +        while (m_cnt--) {
      762 +                free(members[m_cnt]);
      763 +        }
      764 +}
      765 +
 618  766  /*
 619  767   * Parses the buffer, for name-value pairs and creates nvlist. The value
 620  768   * is always considered to be a string.
 621  769   */
 622  770  int
 623  771  ipadm_str2nvlist(const char *inbuf, nvlist_t **ipnvl, uint_t flags)
 624  772  {
 625  773          char    *nv, *name, *val, *buf, *cp, *sep;
 626  774          int     err;
 627  775  
↓ open down ↓ 206 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX