[netcf-devel] [PATCH 2/2] Implement MAC lookup using normalize_if

Jonas Eriksson jonas.j.eriksson at ericsson.com
Wed Aug 5 07:28:53 UTC 2009


data/xml/augeas.rng: replace /forest/tree/@path with @name and add
  additional @mac in order to do mac lookup when the tree is read.

data/xml/initscripts-get.xsl: create /forest/tree/@mac instead of a
node with @label='HWADDR' containing the mac address

data/xml/initscripts-put.xsl: handle @mac instead of
  node[@label='HWADDR']/@value when outputing interface/mac

src/drv_initscripts.c:
  - aug_get_xml: replace tree/@path with @name, insert tree/@mac
  - aug_put_xml: use tree/@name and @mac, use normalize_if to look up
    the correct interface, generate the DEVICE-line from the lookup
    instead of using node[@label='DEVICE'] from augeas xml
---
 data/xml/augeas.rng          |   11 +++++++-
 data/xml/initscripts-get.xsl |    8 +++---
 data/xml/initscripts-put.xsl |    8 +++---
 src/drv_initscripts.c        |   55 ++++++++++++++++++++++++++++++++++-------
 4 files changed, 63 insertions(+), 19 deletions(-)

diff --git a/data/xml/augeas.rng b/data/xml/augeas.rng
index d60eae1..aefb9b5 100644
--- a/data/xml/augeas.rng
+++ b/data/xml/augeas.rng
@@ -14,7 +14,10 @@
     <element name="forest">
       <oneOrMore>
         <element name="tree">
-          <attribute name="path"><ref name="path-pattern"/></attribute>
+          <attribute name="name"><ref name="label-pattern"/></attribute>
+	  <optional>
+            <attribute name="mac"><ref name="mac-pattern"/></attribute>
+	  </optional>
           <zeroOrMore>
             <element name="node">
               <attribute name="label"><ref name="label-pattern"/></attribute>
@@ -43,4 +46,10 @@
     </data>
   </define>
 
+  <define name='mac-pattern'>
+    <data type='string'>
+      <param name="pattern">[a-fA-F0-9:]+</param>
+    </data>
+  </define>
+
 </grammar>
diff --git a/data/xml/initscripts-get.xsl b/data/xml/initscripts-get.xsl
index 46e39b1..1e70735 100644
--- a/data/xml/initscripts-get.xsl
+++ b/data/xml/initscripts-get.xsl
@@ -47,7 +47,7 @@
   <xsl:template name="vlan-interface-common">
     <xsl:variable name="iface" select="concat(vlan/interface/@name, '.', vlan/@tag)"/>
 
-    <xsl:attribute name="path">/files/etc/sysconfig/network-scripts/ifcfg-<xsl:value-of select="$iface"/></xsl:attribute>
+    <xsl:attribute name="name"><xsl:value-of select="$iface"/></xsl:attribute>
     <node label="DEVICE" value="{$iface}"/>
     <node label="VLAN" value="yes"/>
   </xsl:template>
@@ -117,7 +117,7 @@
        Named templates, following the Relax NG syntax
   -->
   <xsl:template name="name-attr">
-    <xsl:attribute name="path">/files/etc/sysconfig/network-scripts/ifcfg-<xsl:value-of select="@name"/></xsl:attribute>
+    <xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute>
     <node label="DEVICE" value="{@name}"/>
   </xsl:template>
 
@@ -128,10 +128,10 @@
   </xsl:template>
 
   <xsl:template name="bare-ethernet-interface">
-    <xsl:call-template name="name-attr"/>
     <xsl:if test="mac">
-      <node label="HWADDR" value="{mac/@address}"/>
+      <xsl:attribute name="mac"><xsl:value-of select="mac/@address"/></xsl:attribute>
     </xsl:if>
+    <xsl:call-template name="name-attr"/>
     <xsl:call-template name="startmode"/>
     <xsl:call-template name="mtu"/>
   </xsl:template>
diff --git a/data/xml/initscripts-put.xsl b/data/xml/initscripts-put.xsl
index 886a265..9632289 100644
--- a/data/xml/initscripts-put.xsl
+++ b/data/xml/initscripts-put.xsl
@@ -23,8 +23,8 @@
     <interface type="ethernet">
       <xsl:call-template name="name-attr"/>
       <xsl:call-template name="startmode"/>
-      <xsl:if test="node[@label = 'HWADDR']">
-        <mac address="{node[@label = 'HWADDR']/@value}"/>
+      <xsl:if test="@mac">
+        <mac address="{@mac}"/>
       </xsl:if>
       <xsl:call-template name="mtu"/>
       <xsl:call-template name="interface-addressing"/>
@@ -187,8 +187,8 @@
   <xsl:template name="bare-ethernet-interface">
     <interface type="ethernet">
       <xsl:call-template name="name-attr"/>
-      <xsl:if test="node[@label = 'HWADDR']">
-        <mac address="{node[@label = 'HWADDR']/@value}"/>
+      <xsl:if test="@mac">
+        <mac address="{@mac}"/>
       </xsl:if>
     </interface>
   </xsl:template>
diff --git a/src/drv_initscripts.c b/src/drv_initscripts.c
index 550df65..2593a70 100644
--- a/src/drv_initscripts.c
+++ b/src/drv_initscripts.c
@@ -397,6 +397,9 @@ static xmlDocPtr aug_get_xml(struct netcf *ncf, int nint, char **intf) {
     xmlDocPtr result = NULL;
     xmlNodePtr root = NULL, tree = NULL;
     char **matches = NULL;
+    char *name;
+    const char *mac;
+
     int nmatches, r;
 
     aug = get_augeas(ncf);
@@ -408,7 +411,11 @@ static xmlDocPtr aug_get_xml(struct netcf *ncf, int nint, char **intf) {
 
     for (int i=0; i < nint; i++) {
         tree = xmlNewChild(root, NULL, BAD_CAST "tree", NULL);
-        xmlNewProp(tree, BAD_CAST "path", BAD_CAST intf[i]);
+        name = &intf[i][strlen(ifcfg_path) + 5];
+        xmlNewProp(tree, BAD_CAST "name", BAD_CAST name);
+        r = aug_get_mac(ncf, name, &mac);
+        if (r >= 0 && mac != NULL)
+            xmlNewProp(tree, BAD_CAST "mac", BAD_CAST mac);
         nmatches = aug_fmt_match(ncf, &matches, "%s/%s", intf[i], "*");
         ERR_COND_BAIL(nint < 0, ncf, EOTHER);
         for (int j = 0; j < nmatches; j++) {
@@ -435,6 +442,7 @@ static xmlDocPtr aug_get_xml(struct netcf *ncf, int nint, char **intf) {
 static int aug_put_xml(struct netcf *ncf, xmlDocPtr xml) {
     xmlNodePtr forest;
     char *path = NULL, *lpath = NULL, *label = NULL, *value = NULL;
+    char *name = NULL, *nname = NULL, *mac = NULL;
     struct augeas *aug = NULL;
     int result = -1;
     int r;
@@ -451,11 +459,29 @@ static int aug_put_xml(struct netcf *ncf, xmlDocPtr xml) {
         ERR_THROW(! xmlStrEqual(tree->name, BAD_CAST "tree"), ncf,
                   EINTERNAL, "expected node labeled 'tree', not '%s'",
                   tree->name);
-        path = xml_prop(tree, "path");
+        name = xml_prop(tree, "name");
+        mac = xml_prop(tree, "mac");
+        nname = normalize_if(ncf, name, mac);
+        ERR_BAIL(ncf);
+
+        r = xasprintf(&path,
+                "/files/etc/sysconfig/network-scripts/ifcfg-%s", nname);
+        ERR_THROW(r < 0, ncf, ENOMEM, NULL);
+
         int toplevel = 1;
         /* This is a little drastic, since it clears out the file entirely */
         r = aug_rm(aug, path);
         ERR_THROW(r < 0, ncf, EINTERNAL, "aug_rm of '%s' failed", path);
+
+        r = xasprintf(&lpath,
+                "%s/DEVICE", path);
+        ERR_THROW(r < 0, ncf, ENOMEM, NULL);
+
+        r = aug_set(aug, lpath, nname);
+        ERR_THROW(r < 0, ncf, EOTHER,
+                  "aug_set of '%s' failed", lpath);
+        FREE(lpath);
+
         list_for_each(node, tree->children) {
             label = xml_prop(node, "label");
             value = xml_prop(node, "value");
@@ -463,26 +489,35 @@ static int aug_put_xml(struct netcf *ncf, xmlDocPtr xml) {
             if (STREQ(label, "BRIDGE") || STREQ(label, "MASTER")) {
                 toplevel = 0;
             }
-            r = xasprintf(&lpath, "%s/%s", path, label);
-            ERR_THROW(r < 0, ncf, ENOMEM, NULL);
+            if (STRNEQ(label, "DEVICE")) {
+                r = xasprintf(&lpath, "%s/%s", path, label);
+                ERR_THROW(r < 0, ncf, ENOMEM, NULL);
 
-            r = aug_set(aug, lpath, value);
-            ERR_THROW(r < 0, ncf, EOTHER,
-                      "aug_set of '%s' failed", lpath);
+                r = aug_set(aug, lpath, value);
+                ERR_THROW(r < 0, ncf, EOTHER,
+                          "aug_set of '%s' failed", lpath);
+            }
             FREE(lpath);
+            FREE(nname);
             xmlFree(label);
             xmlFree(value);
             label = value = NULL;
         }
-        xmlFree(path);
-        path = NULL;
+        FREE(path);
+        xmlFree(name);
+        if (mac != NULL)
+            xmlFree(mac);
+        name = mac = NULL;
     }
     result = 0;
  error:
     xmlFree(label);
     xmlFree(value);
-    xmlFree(path);
+    xmlFree(name);
+    xmlFree(mac);
     FREE(lpath);
+    FREE(path);
+    FREE(nname);
     return result;
 }
 
-- 
1.6.2



More information about the netcf-devel mailing list