"Fossies" - the Fresh Open Source Software Archive

Member "open-fcoe-3.19/fcoe-utils/include/fcoemon_utils.h" (15 Apr 2015, 11653 Bytes) of package /linux/misc/open-fcoe-3.19.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "fcoemon_utils.h" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright(c) 2009 Intel Corporation. All rights reserved.
    3  *
    4  * This program is free software; you can redistribute it and/or modify it
    5  * under the terms and conditions of the GNU General Public License,
    6  * version 2, as published by the Free Software Foundation.
    7  *
    8  * This program is distributed in the hope it will be useful, but WITHOUT
    9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   11  * more details.
   12  *
   13  * You should have received a copy of the GNU General Public License along with
   14  * this program; if not, write to the Free Software Foundation, Inc.,
   15  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
   16  *
   17  * Maintained at www.Open-FCoE.org
   18  */
   19 
   20 #ifndef _FCOEMON_UTILS_H_
   21 #define _FCOEMON_UTILS_H_
   22 
   23 #include <sys/types.h>
   24 #include <sys/time.h>
   25 #include <sys/socket.h>
   26 #include <sys/queue.h>
   27 #include <malloc.h>
   28 #include <stdlib.h>
   29 #include <stdio.h>
   30 #include <string.h>
   31 #include <time.h>
   32 #include <stdarg.h>
   33 #include <ctype.h>
   34 #include <time.h>
   35 #include <unistd.h>
   36 #include <signal.h>
   37 #include <errno.h>
   38 #include <syslog.h>
   39 #include <dirent.h>
   40 
   41 #include "fc_types.h"
   42 
   43 #define ARRAY_SIZE(a)   (sizeof(a) / sizeof((a)[0]))
   44 
   45 __attribute__((__format__(__printf__, 1, 2)))
   46 void sa_log(const char *format, ...);
   47 __attribute__((__format__(__printf__, 1, 2)))
   48 void sa_log_debug(const char *format, ...);
   49 __attribute__((__format__(__printf__, 3, 4)))
   50 void sa_log_err(int, const char *func, const char *format, ...);
   51 
   52 /*
   53  * These functions can be provided outside of libsa for those environments
   54  * that want to redirect them.
   55  */
   56 void sa_log_output(const char *);   /* log message */
   57 void sa_log_abort(const char *);    /* log message and abort */
   58 
   59 #define __SA_STRING(x)  #x
   60 
   61 /*
   62  * Logging options.
   63  */
   64 #define SA_LOGF_TIME    0x0001      /* include timestamp in message */
   65 #define SA_LOGF_DELTA   0x0002      /* include time since last message */
   66 
   67 extern u_int sa_log_flags;          /* timestamp and other option flags */
   68 extern int sa_log_time_delta_min;   /* minimum diff to print in millisec */
   69 extern char *sa_log_prefix;         /* string to print before any message */
   70 
   71 __attribute__((__format__(__printf__, 1, 2)))
   72 extern void assert_failed(const char *s, ...);
   73 
   74 #ifndef UNLIKELY
   75 #define UNLIKELY(_x) (_x)
   76 #endif /* UNLIKELY */
   77 
   78 /*
   79  * ASSERT macros
   80  *
   81  * ASSERT(expr) - this calls assert_failed() if expr is false.  This variant
   82  * is not present in production code or if DEBUG_ASSERTS is not defined.
   83  * Be careful not to rely on expr being evaluated.
   84  */
   85 #if defined(DEBUG_ASSERTS)
   86 #define ASSERT(_x) do {                         \
   87         if (UNLIKELY(!(_x))) {                  \
   88             assert_failed("ASSERT FAILED (%s) @ %s:%d\n",   \
   89                 "" #_x, __FILE__, __LINE__);        \
   90         }                           \
   91     } while (0)
   92 #else
   93 #define ASSERT(_x)
   94 #endif /* DEBUG_ASSERTS */
   95 
   96 /*
   97  * ASSERT_NOTIMPL(expr) - this calls assert_failed() if expr is false.
   98  * The implication is that the condition is not handled by the current
   99  * implementation, and work should be done eventually to handle this.
  100  */
  101 #define ASSERT_NOTIMPL(_x) do {                     \
  102         if (UNLIKELY(!(_x))) {                  \
  103             assert_failed("ASSERT (NOT IMPL) "      \
  104                 "(%s) @ %s:%d\n",           \
  105                 "" #_x, __FILE__, __LINE__);        \
  106         }                           \
  107     } while (0)
  108 
  109 /*
  110  * ASSERT_NOTREACHED - this is the same as ASSERT_NOTIMPL(0).
  111  */
  112 #define ASSERT_NOTREACHED do {                      \
  113         assert_failed("ASSERT (NOT REACHED) @ %s:%d\n",     \
  114             __FILE__, __LINE__);                \
  115     } while (0)
  116 
  117 /*
  118  * ASSERT_BUG(bugno, expr).  This variant is used when a bug number has
  119  * been assigned to any one of the other assertion failures.  It is always
  120  * present in code.  It gives the bug number which helps locate
  121  * documentation and helps prevent duplicate bug filings.
  122  */
  123 #define ASSERT_BUG(_bugNr, _x) do {                 \
  124         if (UNLIKELY(!(_x))) {                  \
  125             assert_failed("ASSERT (BUG %d) (%s) @ %s:%d\n", \
  126                 (_bugNr), #_x, __FILE__, __LINE__); \
  127         }                                                       \
  128     } while (0)
  129 
  130 #ifndef LIBSA_USE_DANGEROUS_ROUTINES
  131 #define strcpy DONT_USE_strcpy
  132 #define strcat DONT_USE_strcat
  133 #define gets   DONT_USE_gets
  134 #endif /* LIBSA_USE_DANGEROUS_ROUTINES */
  135 
  136 char *sa_strncpy_safe(char *dest, size_t len, const char *src, size_t src_len);
  137 char *sa_hex_format(char *buf, size_t buflen,
  138             const unsigned char *data, size_t data_len,
  139             unsigned int group_len, char *inter_group_sep);
  140 
  141 /*
  142  * Structure for tables encoding and decoding name-value pairs such as enums.
  143  */
  144 struct sa_nameval {
  145     char      *nv_name;
  146     u_int32_t nv_val;
  147 };
  148 
  149 const char *sa_enum_decode(char *, size_t, const struct sa_nameval *, u_int);
  150 int sa_enum_encode(const struct sa_nameval *tp, const char *, u_int *);
  151 const char *sa_flags_decode(char *, size_t, const struct sa_nameval *, u_int);
  152 
  153 /*
  154  * Timer facility.
  155  */
  156 
  157 struct sa_timer {
  158     struct sa_timer *tm_next;
  159     u_int64_t   tm_nsec;    /* relative time to event (nSec) */
  160     void        (*tm_handler)(void *arg);
  161     void        *tm_handler_arg;
  162     struct sa_timer **timer_head;
  163 };
  164 
  165 
  166 #define SA_TIMER_UNITS  (1000 * 1000UL) /* number of timer ticks per second */
  167 
  168 /*
  169  * Initialize a pre-allocated timer structure.
  170  */
  171 void sa_timer_init(struct sa_timer *, void (*handler)(void *), void *arg);
  172 
  173 /*
  174  * Test whether the timer is active.
  175  */
  176 static inline int sa_timer_active(struct sa_timer *tm)
  177 {
  178     return tm->tm_nsec != 0;
  179 }
  180 
  181 /*
  182  * Allocate a timer structure.  Set handler.
  183  */
  184 struct sa_timer *sa_timer_alloc(void (*)(void *arg), void *arg);
  185 
  186 /*
  187  * Set timer to fire.   Delta is in microseconds from now.
  188  */
  189 void sa_timer_set(struct sa_timer *, u_long delta);
  190 
  191 /*
  192  * Cancel timer.
  193  */
  194 void sa_timer_cancel(struct sa_timer *);
  195 
  196 /*
  197  * Free (and cancel) timer.
  198  */
  199 void sa_timer_free(struct sa_timer *);
  200 
  201 
  202 /*
  203  * Handle timer checks.  Called from select loop or other periodic function.
  204  *
  205  * The struct timeval passed in indicates how much time has passed since
  206  * the last call, and is set before returning to the maximum amount of time
  207  * that should elapse before the next call.
  208  *
  209  * Returns 1 if any timer handlers were invoked, 0 otherwise.
  210  */
  211 int sa_timer_check(struct timeval *);
  212 
  213 /*
  214  * Get time in nanoseconds since some arbitrary time.
  215  */
  216 u_int64_t sa_timer_get(void);
  217 
  218 /*
  219  * Get time in seconds since some arbitrary time.
  220  */
  221 u_int sa_timer_get_secs(void);
  222 
  223 /*
  224  * sa_select - Server Array select facility.
  225  *
  226  * This is a thin layer to poll files with a select loop.
  227  */
  228 
  229 /*
  230  * Enter the polling loop which never exits.
  231  */
  232 int sa_select_loop(void);
  233 
  234 /*
  235  * Set callback for every time through the select loop.
  236  */
  237 void sa_select_set_callback(void (*)(void));
  238 
  239 /*
  240  * Add a callback to handle files which are ready for receive, transmit,
  241  * or to handle exceptions.
  242  */
  243 void sa_select_add_fd(int fd, void (*rx_handler)(void *),
  244             void (*tx_handler)(void *),
  245             void (*ex_handler)(void *), void *arg);
  246 
  247 /*
  248  * Change a single callback for a descriptor that's already been added.
  249  */
  250 void sa_select_set_rx(int fd, void (*handler)(void *));
  251 void sa_select_set_tx(int fd, void (*handler)(void *));
  252 void sa_select_set_ex(int fd, void (*handler)(void *));
  253 
  254 /*
  255  * Remove all callbacks for a file descriptor.
  256  */
  257 void sa_select_rem_fd(int fd);
  258 
  259 /*
  260  * Cause select loop to return.
  261  */
  262 void sa_select_exit(int sig);
  263 
  264 /*
  265  * Convert 48-bit IEEE MAC address to 64-bit FC WWN.
  266  */
  267 extern fc_wwn_t
  268 fc_wwn_from_mac(u_int64_t, u_int32_t scheme, u_int32_t port);
  269 
  270 extern int hex2int(char *b);
  271 
  272 extern int use_syslog;
  273 void enable_syslog(int);
  274 void enable_debug_log(int);
  275 
  276 /*
  277  * Table and sysfs helpers
  278  */
  279 
  280 /*
  281  * Structure for integer-indexed tables that can grow.
  282  */
  283 struct sa_table {
  284     u_int32_t   st_size;        /* number of entries in table */
  285     u_int32_t   st_limit;       /* end of valid entries in table (public) */
  286     void        **st_table;     /* re-allocatable array of pointers */
  287 };
  288 
  289 /*
  290  * Function prototypes
  291  */
  292 extern int sa_sys_read_line(const char *, const char *, char *, size_t);
  293 extern int sa_sys_write_line(const char *, const char *, const char *);
  294 extern int sa_sys_read_int(const char *, const char *, int *);
  295 extern int sa_sys_read_u32(const char *, const char *, u_int32_t *);
  296 extern int sa_sys_read_u64(const char *, const char *, u_int64_t *);
  297 extern int sa_dir_read(char *, int (*)(struct dirent *, void *), void *);
  298 extern char *sa_strncpy_safe(char *dest, size_t len,
  299                  const char *src, size_t src_len);
  300 extern const char *sa_enum_decode(char *, size_t,
  301                   const struct sa_nameval *, u_int32_t);
  302 extern int sa_enum_encode(const struct sa_nameval *tp,
  303             const char *, u_int32_t *);
  304 extern const char *sa_flags_decode(char *, size_t,
  305                    const struct sa_nameval *, u_int32_t);
  306 extern int sa_table_grow(struct sa_table *, u_int32_t index);
  307 extern void sa_table_destroy_all(struct sa_table *);
  308 extern void sa_table_destroy(struct sa_table *);
  309 extern void sa_table_iterate(struct sa_table *tp,
  310             void (*handler)(void *ep, void *arg), void *arg);
  311 extern void *sa_table_search(struct sa_table *tp,
  312             void *(*match)(void *ep, void *arg), void *arg);
  313 
  314 /** sa_table_init(tp) - initialize a table.
  315  * @param tp table pointer.
  316  *
  317  * This just clears a table structure that was allocated by the caller.
  318  */
  319 static inline void sa_table_init(struct sa_table *tp)
  320 {
  321     memset(tp, 0, sizeof(*tp));
  322 }
  323 
  324 /** sa_table_lookup(tp, index) - lookup an entry in the table.
  325  * @param tp table pointer.
  326  * @param index the index in the table to access
  327  * @returns the entry, or NULL if the index wasn't valid.
  328  */
  329 static inline void *sa_table_lookup(const struct sa_table *tp, u_int32_t index)
  330 {
  331     void *ep = NULL;
  332 
  333     if (index < tp->st_limit)
  334         ep = tp->st_table[index];
  335     return ep;
  336 }
  337 
  338 /** sa_table_lookup_n(tp, n) - find Nth non-empty entry in a table.
  339  * @param tp table pointer.
  340  * @param n is the entry number, the first non-empty entry is 0.
  341  * @returns the entry, or NULL if the end of the table reached first.
  342  */
  343 static inline void *sa_table_lookup_n(const struct sa_table *tp, u_int32_t n)
  344 {
  345     void *ep = NULL;
  346     u_int32_t   i;
  347 
  348     for (i = 0; i < tp->st_limit; i++) {
  349         ep = tp->st_table[i];
  350         if (ep != NULL && n-- == 0)
  351             return ep;
  352     }
  353     return NULL;
  354 }
  355 
  356 /** sa_table_insert(tp, index, ep) - Replace or insert an entry in the table.
  357  * @param tp table pointer.
  358  * @param index the index for the new entry.
  359  * @param ep entry pointer.
  360  * @returns index on success, or -1 if the insert failed.
  361  *
  362  * Note: if the table has never been used, and is still all zero, this works.
  363  *
  364  * Note: perhaps not safe for multithreading.  Caller can lock the table
  365  * externally, but reallocation can take a while, during which time the
  366  * caller may not wish to hold the lock.
  367  */
  368 static inline int sa_table_insert(struct sa_table *tp,
  369                   u_int32_t index, void *ep)
  370 {
  371     if (index >= tp->st_limit && sa_table_grow(tp, index) < 0)
  372         return -1;
  373     tp->st_table[index] = ep;
  374     return index;
  375 }
  376 
  377 /** sa_table_append(tp, ep) - add entry to table and return index.
  378  *
  379  * @param tp pointer to sa_table structure.
  380  * @param ep pointer to new entry, to be added at the end of the table.
  381  * @returns new index, or -1 if table couldn't be grown.
  382  *
  383  * See notes on sa_table_insert().
  384  */
  385 static inline int
  386 sa_table_append(struct sa_table *tp, void *ep)
  387 {
  388     return sa_table_insert(tp, tp->st_limit, ep);
  389 }
  390 
  391 /** sa_table_sort(tp, compare) - sort table in place
  392  *
  393  * @param tp pointer to sa_table structure.
  394  * @param compare function to compare two entries.  It is called with pointers
  395  * to the pointers to the entries to be compared.  See qsort(3).
  396  */
  397 static inline void
  398 sa_table_sort(struct sa_table *tp, int (*compare)(const void **, const void **))
  399 {
  400     qsort(tp->st_table, tp->st_limit, sizeof(void *),
  401         (int (*)(const void *, const void *)) compare);
  402 }
  403 
  404 #endif /* _FCOEMON_UTILS_H_ */