Create library files for fcoe util socket interfaces.
Create new files fcoe_clif.h/c as a library used by fcoemon and fcoeadm.
Clif stands for client interface. Change Makefile.am for new files
fcoe_clif.h/c.

Signed-off-by: Lucy Liu <[email protected]>

---

 Makefile.am |    4 +-
 fcoe_clif.c |  146 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 fcoe_clif.h |   55 ++++++++++++++++++++++
 3 files changed, 203 insertions(+), 2 deletions(-)
 create mode 100644 fcoe_clif.c
 create mode 100644 fcoe_clif.h

diff --git a/Makefile.am b/Makefile.am
index 0c28462..59d72cb 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -13,7 +13,7 @@ AM_CFLAGS = -DSYSCONFDIR="\"${sysconfdir}\""
 ## rules for building fcoeadm
 ## only listed sources get packaged, so must list all headers too
 fcoeadm_SOURCES = fcoeadm_display.c fcoeadm.c fcoeadm.h \
-include/fc_scsi.h include/fc_types.h include/net_types.h
+include/fc_scsi.h include/fc_types.h include/net_types.h fcoe_clif.c 
fcoe_clif.h
 
 ## fcoeadm uses HBAAPI, so get the right flags for compiling and linking
 fcoeadm_CFLAGS = $(HBAAPI_CFLAGS)
@@ -30,7 +30,7 @@ fcping_LDFLAGS = $(HBAAPI_LIBS) -lrt
 ## rules for building fcoemon
 ## only listed sources get packaged, so must list all headers too
 fcoemon_SOURCES = fcoemon_utils.c fcoemon.c fcoemon.h fcoemon_utils.h \
-include/fc_scsi.h include/fc_types.h include/net_types.h
+include/fc_scsi.h include/fc_types.h include/net_types.h fcoe_clif.c 
fcoe_clif.h
 
 ## fcoemon needs headers from dcbd, get the right include path for them
 fcoemon_CFLAGS = $(DCBD_CFLAGS)
diff --git a/fcoe_clif.c b/fcoe_clif.c
new file mode 100644
index 0000000..3f33ba8
--- /dev/null
+++ b/fcoe_clif.c
@@ -0,0 +1,146 @@
+/*
+ * Copyright(c) 2009 Intel Corporation. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Maintained at www.Open-FCoE.org
+ */
+
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <libgen.h>
+#include <dirent.h>
+#include <net/if.h>
+#include <errno.h>
+#include "fcoe_clif.h"
+
+static char *fcoeclif_read(const char *path)
+{
+       FILE *fp;
+       char *buf;
+       int size = 512;
+
+       buf = malloc(size);
+       if (!buf)
+               return NULL;
+       memset(buf, 0, size);
+
+       fp = fopen(path, "r");
+       if (fp) {
+               if (fgets(buf, size, fp)) {
+                       fclose(fp);
+                       return buf;
+               }
+       }
+       fclose(fp);
+       free(buf);
+       return NULL;
+}
+
+static int fcoeclif_check_fchost(const char *ifname, const char *dname)
+{
+       char *buf;
+       char path[512];
+
+       if (dname[0] == '.')
+               return -EINVAL;
+
+       sprintf(path, "%s/%s/symbolic_name", SYSFS_FCHOST, dname);
+       buf = fcoeclif_read(path);
+       if (!buf)
+               return -EINVAL;
+
+       if (!strstr(buf, ifname)) {
+               free(buf);
+               return -EINVAL;
+       }
+       free(buf);
+       return 0;
+}
+
+static int fcoeclif_find_fchost(char *ifname, char *fchost, int len)
+{
+       int n, dname_len;
+       int found = 0;
+       struct dirent **namelist;
+
+       memset(fchost, 0, len);
+       n = scandir(SYSFS_FCHOST, &namelist, 0, alphasort);
+       if (n > 0) {
+               while (n--) {
+                       /* check symbolic name */
+                       if (!fcoeclif_check_fchost(ifname,
+                                                 namelist[n]->d_name)) {
+                               dname_len = strnlen(namelist[n]->d_name, len);
+                               if (dname_len != len) {
+                                       /*
+                                        * This assumes that d_name is always
+                                        * NULL terminated.
+                                        */
+                                       strncpy(fchost, namelist[n]->d_name,
+                                               dname_len + 1);
+                                       found = 1;
+                               } else {
+                                       fprintf(stderr, "scsi_host (%s) is "
+                                               "too large for a buffer that "
+                                               "is only %d bytes large\n",
+                                               namelist[n]->d_name, dname_len);
+                                       free(namelist[n]);
+                               }
+                       }
+                       free(namelist[n]);
+               }
+               free(namelist);
+       }
+
+       return found;
+}
+
+/*
+ * Validate an existing instance for an FC interface
+ */
+int fcoeclif_validate_interface(char *ifname, char *fchost, int len)
+{
+       if ((!ifname) || (!fchost) || (len <= 0))
+               return -EINVAL;
+
+       if (!fcoeclif_find_fchost(ifname, fchost, len)) {
+               fprintf(stderr, "No fc_host found for %s\n", ifname);
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+
+/*
+ * Open and close to check if directory exists
+ */
+int fcoeclif_checkdir(char *dir)
+{
+       DIR *d = NULL;
+
+       if (!dir)
+               return -EINVAL;
+       /* check if we have sysfs */
+       d = opendir(dir);
+       if (!d)
+               return -EINVAL;
+       closedir(d);
+       return 0;
+}
diff --git a/fcoe_clif.h b/fcoe_clif.h
new file mode 100644
index 0000000..4ff8afe
--- /dev/null
+++ b/fcoe_clif.h
@@ -0,0 +1,55 @@
+/*
+ * Copyright(c) 2009 Intel Corporation. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Maintained at www.Open-FCoE.org
+ */
+
+#ifndef _FCOE_CLIF_H_
+#define _FCOE_CLIF_H_
+
+#define SYSFS_MOUNT    "/sys"
+#define SYSFS_NET      SYSFS_MOUNT "/class/net"
+#define SYSFS_FCHOST   SYSFS_MOUNT "/class/fc_host"
+#define SYSFS_FCOE     SYSFS_MOUNT "/module/fcoe/parameters"
+#define FCM_SRV_DIR "/var/run/fcm"
+#define CLIF_IFNAME "fcm_clif"
+#define FCHOSTBUFLEN           64
+#define MAX_MSGBUF 512
+#define CLIF_PID_FILE           _PATH_VARRUN "fcoemon.pid"
+
+enum clif_status {
+       CLI_SUCCESS = 0,
+       CLI_FAIL,
+       CLI_NO_ACTION
+};
+
+enum {
+       FCOE_CREATE_CMD = 1,
+       FCOE_DESTROY_CMD,
+       FCOE_RESET_CMD,
+};
+
+/*
+ * Description of fcoemon and fcoeadm socket data structure interface
+ */
+struct clif_data {
+       int cmd;
+       char ifname[IFNAMSIZ];
+};
+
+int fcoeclif_validate_interface(char *ifname, char *fchost, int len);
+int fcoeclif_checkdir(char *dir);
+#endif /* _FCOE_CLIF_H_ */

-- 
Signature: Lucy Liu <[email protected]>
_______________________________________________
devel mailing list
[email protected]
http://www.open-fcoe.org/mailman/listinfo/devel

Reply via email to