xref: /onnv-gate/usr/src/lib/librstp/common/base.h (revision 10491:8893b747ecdf)
1*10491SRishi.Srivatsavai@Sun.COM /************************************************************************
2*10491SRishi.Srivatsavai@Sun.COM  * RSTP library - Rapid Spanning Tree (802.1t, 802.1w)
3*10491SRishi.Srivatsavai@Sun.COM  * Copyright (C) 2001-2003 Optical Access
4*10491SRishi.Srivatsavai@Sun.COM  * Author: Alex Rozin
5*10491SRishi.Srivatsavai@Sun.COM  *
6*10491SRishi.Srivatsavai@Sun.COM  * This file is part of RSTP library.
7*10491SRishi.Srivatsavai@Sun.COM  *
8*10491SRishi.Srivatsavai@Sun.COM  * RSTP library is free software; you can redistribute it and/or modify it
9*10491SRishi.Srivatsavai@Sun.COM  * under the terms of the GNU Lesser General Public License as published by the
10*10491SRishi.Srivatsavai@Sun.COM  * Free Software Foundation; version 2.1
11*10491SRishi.Srivatsavai@Sun.COM  *
12*10491SRishi.Srivatsavai@Sun.COM  * RSTP library is distributed in the hope that it will be useful, but
13*10491SRishi.Srivatsavai@Sun.COM  * WITHOUT ANY WARRANTY; without even the implied warranty of
14*10491SRishi.Srivatsavai@Sun.COM  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
15*10491SRishi.Srivatsavai@Sun.COM  * General Public License for more details.
16*10491SRishi.Srivatsavai@Sun.COM  *
17*10491SRishi.Srivatsavai@Sun.COM  * You should have received a copy of the GNU Lesser General Public License
18*10491SRishi.Srivatsavai@Sun.COM  * along with RSTP library; see the file COPYING.  If not, write to the Free
19*10491SRishi.Srivatsavai@Sun.COM  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20*10491SRishi.Srivatsavai@Sun.COM  * 02111-1307, USA.
21*10491SRishi.Srivatsavai@Sun.COM  **********************************************************************/
22*10491SRishi.Srivatsavai@Sun.COM 
23*10491SRishi.Srivatsavai@Sun.COM /* Mutual RSTP definitions */
24*10491SRishi.Srivatsavai@Sun.COM 
25*10491SRishi.Srivatsavai@Sun.COM #ifndef _STP_BASE_H__
26*10491SRishi.Srivatsavai@Sun.COM #define _STP_BASE_H__
27*10491SRishi.Srivatsavai@Sun.COM 
28*10491SRishi.Srivatsavai@Sun.COM #include <stdlib.h>
29*10491SRishi.Srivatsavai@Sun.COM #include <string.h>
30*10491SRishi.Srivatsavai@Sun.COM 
31*10491SRishi.Srivatsavai@Sun.COM #define STP_DBG 1
32*10491SRishi.Srivatsavai@Sun.COM 
33*10491SRishi.Srivatsavai@Sun.COM #if defined(__LINUX__) || defined(__SUN__)
34*10491SRishi.Srivatsavai@Sun.COM #  include <stddef.h>
35*10491SRishi.Srivatsavai@Sun.COM #  include <stdio.h>
36*10491SRishi.Srivatsavai@Sun.COM #  include <netinet/in.h>
37*10491SRishi.Srivatsavai@Sun.COM #  include "uid_stp.h"
38*10491SRishi.Srivatsavai@Sun.COM #else
39*10491SRishi.Srivatsavai@Sun.COM #  include <psos.h>
40*10491SRishi.Srivatsavai@Sun.COM #  include "comdef.h"
41*10491SRishi.Srivatsavai@Sun.COM #  include "comdef.x"
42*10491SRishi.Srivatsavai@Sun.COM #  include "Bitmap/bitmap.h"
43*10491SRishi.Srivatsavai@Sun.COM #  include "Bitmap/bitmap.x"
44*10491SRishi.Srivatsavai@Sun.COM #  include "Ui/uid_stp.h"
45*10491SRishi.Srivatsavai@Sun.COM #endif
46*10491SRishi.Srivatsavai@Sun.COM 
47*10491SRishi.Srivatsavai@Sun.COM #ifndef INOUT
48*10491SRishi.Srivatsavai@Sun.COM #  define IN      /* consider as comments near 'input' parameters */
49*10491SRishi.Srivatsavai@Sun.COM #  define OUT     /* consider as comments near 'output' parameters */
50*10491SRishi.Srivatsavai@Sun.COM #  define INOUT   /* consider as comments near 'input/output' parameters */
51*10491SRishi.Srivatsavai@Sun.COM #endif
52*10491SRishi.Srivatsavai@Sun.COM 
53*10491SRishi.Srivatsavai@Sun.COM #ifndef Zero
54*10491SRishi.Srivatsavai@Sun.COM #  define Zero        0
55*10491SRishi.Srivatsavai@Sun.COM #  define One         1
56*10491SRishi.Srivatsavai@Sun.COM #endif
57*10491SRishi.Srivatsavai@Sun.COM 
58*10491SRishi.Srivatsavai@Sun.COM #ifndef Bool
59*10491SRishi.Srivatsavai@Sun.COM #  define Bool        int
60*10491SRishi.Srivatsavai@Sun.COM #  define False       0
61*10491SRishi.Srivatsavai@Sun.COM #  define True        1
62*10491SRishi.Srivatsavai@Sun.COM #endif
63*10491SRishi.Srivatsavai@Sun.COM 
64*10491SRishi.Srivatsavai@Sun.COM #include "stp_bpdu.h"
65*10491SRishi.Srivatsavai@Sun.COM #include "vector.h"
66*10491SRishi.Srivatsavai@Sun.COM #include "times.h"
67*10491SRishi.Srivatsavai@Sun.COM 
68*10491SRishi.Srivatsavai@Sun.COM #define RSTP_ERRORS { \
69*10491SRishi.Srivatsavai@Sun.COM   CHOOSE(STP_OK),                                       \
70*10491SRishi.Srivatsavai@Sun.COM   CHOOSE(STP_Cannot_Find_Vlan),         \
71*10491SRishi.Srivatsavai@Sun.COM   CHOOSE(STP_Implicit_Instance_Create_Failed),          \
72*10491SRishi.Srivatsavai@Sun.COM   CHOOSE(STP_Small_Bridge_Priority),                    \
73*10491SRishi.Srivatsavai@Sun.COM   CHOOSE(STP_Large_Bridge_Priority),                    \
74*10491SRishi.Srivatsavai@Sun.COM   CHOOSE(STP_Small_Hello_Time),                         \
75*10491SRishi.Srivatsavai@Sun.COM   CHOOSE(STP_Large_Hello_Time),                         \
76*10491SRishi.Srivatsavai@Sun.COM   CHOOSE(STP_Small_Max_Age),                            \
77*10491SRishi.Srivatsavai@Sun.COM   CHOOSE(STP_Large_Max_Age),                            \
78*10491SRishi.Srivatsavai@Sun.COM   CHOOSE(STP_Small_Forward_Delay),                      \
79*10491SRishi.Srivatsavai@Sun.COM   CHOOSE(STP_Large_Forward_Delay),                      \
80*10491SRishi.Srivatsavai@Sun.COM   CHOOSE(STP_Forward_Delay_And_Max_Age_Are_Inconsistent),\
81*10491SRishi.Srivatsavai@Sun.COM   CHOOSE(STP_Hello_Time_And_Max_Age_Are_Inconsistent),  \
82*10491SRishi.Srivatsavai@Sun.COM   CHOOSE(STP_Vlan_Had_Not_Yet_Been_Created),            \
83*10491SRishi.Srivatsavai@Sun.COM   CHOOSE(STP_Port_Is_Absent_In_The_Vlan),               \
84*10491SRishi.Srivatsavai@Sun.COM   CHOOSE(STP_Big_len8023_Format),                       \
85*10491SRishi.Srivatsavai@Sun.COM   CHOOSE(STP_Small_len8023_Format),                     \
86*10491SRishi.Srivatsavai@Sun.COM   CHOOSE(STP_len8023_Format_Gt_Len),                    \
87*10491SRishi.Srivatsavai@Sun.COM   CHOOSE(STP_Not_Proper_802_3_Packet),                  \
88*10491SRishi.Srivatsavai@Sun.COM   CHOOSE(STP_Invalid_Protocol),                         \
89*10491SRishi.Srivatsavai@Sun.COM   CHOOSE(STP_Invalid_Version),                          \
90*10491SRishi.Srivatsavai@Sun.COM   CHOOSE(STP_Had_Not_Yet_Been_Enabled_On_The_Vlan),     \
91*10491SRishi.Srivatsavai@Sun.COM   CHOOSE(STP_Cannot_Create_Instance_For_Vlan),          \
92*10491SRishi.Srivatsavai@Sun.COM   CHOOSE(STP_Cannot_Create_Instance_For_Port),          \
93*10491SRishi.Srivatsavai@Sun.COM   CHOOSE(STP_Invalid_Bridge_Priority),                  \
94*10491SRishi.Srivatsavai@Sun.COM   CHOOSE(STP_There_Are_No_Ports),                       \
95*10491SRishi.Srivatsavai@Sun.COM   CHOOSE(STP_Cannot_Compute_Bridge_Prio),               \
96*10491SRishi.Srivatsavai@Sun.COM   CHOOSE(STP_Another_Error),                            \
97*10491SRishi.Srivatsavai@Sun.COM   CHOOSE(STP_Nothing_To_Do),                            \
98*10491SRishi.Srivatsavai@Sun.COM   CHOOSE(STP_No_Such_State_Machine),                    \
99*10491SRishi.Srivatsavai@Sun.COM   CHOOSE(STP_LAST_DUMMY)                                \
100*10491SRishi.Srivatsavai@Sun.COM }
101*10491SRishi.Srivatsavai@Sun.COM 
102*10491SRishi.Srivatsavai@Sun.COM #define CHOOSE(a) a
103*10491SRishi.Srivatsavai@Sun.COM typedef enum RSTP_ERRORS RSTP_ERRORS_T;
104*10491SRishi.Srivatsavai@Sun.COM #undef CHOOSE
105*10491SRishi.Srivatsavai@Sun.COM 
106*10491SRishi.Srivatsavai@Sun.COM #if !defined(__LINUX__) && !defined(__SUN__)
107*10491SRishi.Srivatsavai@Sun.COM extern char* strdup (const char *s);
108*10491SRishi.Srivatsavai@Sun.COM 
109*10491SRishi.Srivatsavai@Sun.COM extern USHORT Ntohs (USHORT n);
110*10491SRishi.Srivatsavai@Sun.COM extern ULONG Htonl (ULONG h);
111*10491SRishi.Srivatsavai@Sun.COM extern USHORT Htons (USHORT h);
112*10491SRishi.Srivatsavai@Sun.COM extern ULONG Ntohl (ULONG n);
113*10491SRishi.Srivatsavai@Sun.COM 
114*10491SRishi.Srivatsavai@Sun.COM #define htonl Htonl
115*10491SRishi.Srivatsavai@Sun.COM #define htons Htons
116*10491SRishi.Srivatsavai@Sun.COM #define ntohl Ntohl
117*10491SRishi.Srivatsavai@Sun.COM #define ntohs Ntohs
118*10491SRishi.Srivatsavai@Sun.COM 
119*10491SRishi.Srivatsavai@Sun.COM #endif
120*10491SRishi.Srivatsavai@Sun.COM 
121*10491SRishi.Srivatsavai@Sun.COM #if defined(__LINUX__) || defined(__SUN__)
122*10491SRishi.Srivatsavai@Sun.COM #ifdef STP_DBG
123*10491SRishi.Srivatsavai@Sun.COM #define STP_FATAL(TXT, MSG, EXCOD)                      \
124*10491SRishi.Srivatsavai@Sun.COM       {stp_trace ("FATAL:%s failed: %s:%d", TXT, MSG, EXCOD);  \
125*10491SRishi.Srivatsavai@Sun.COM       exit (EXCOD);}
126*10491SRishi.Srivatsavai@Sun.COM #else
127*10491SRishi.Srivatsavai@Sun.COM #define STP_FATAL(TXT, MSG, EXCOD)                      \
128*10491SRishi.Srivatsavai@Sun.COM       abort();
129*10491SRishi.Srivatsavai@Sun.COM #endif
130*10491SRishi.Srivatsavai@Sun.COM #else
131*10491SRishi.Srivatsavai@Sun.COM #define STP_FATAL(TXT, MSG, EXCOD)                      \
132*10491SRishi.Srivatsavai@Sun.COM       printf("FATAL: %s code %s:%d\n", TXT, MSG, EXCOD)
133*10491SRishi.Srivatsavai@Sun.COM #endif
134*10491SRishi.Srivatsavai@Sun.COM 
135*10491SRishi.Srivatsavai@Sun.COM #define STP_MALLOC(PTR, TYPE, MSG)              \
136*10491SRishi.Srivatsavai@Sun.COM   {                                             \
137*10491SRishi.Srivatsavai@Sun.COM     PTR = (TYPE*) calloc (1, sizeof (TYPE));    \
138*10491SRishi.Srivatsavai@Sun.COM     if (! PTR) {                                \
139*10491SRishi.Srivatsavai@Sun.COM       STP_FATAL("malloc", MSG, -6);             \
140*10491SRishi.Srivatsavai@Sun.COM     }                                           \
141*10491SRishi.Srivatsavai@Sun.COM   }
142*10491SRishi.Srivatsavai@Sun.COM 
143*10491SRishi.Srivatsavai@Sun.COM #define STP_FREE(PTR, MSG)              \
144*10491SRishi.Srivatsavai@Sun.COM   {                                     \
145*10491SRishi.Srivatsavai@Sun.COM     if (! PTR) {                        \
146*10491SRishi.Srivatsavai@Sun.COM       STP_FATAL("free", MSG, -66);      \
147*10491SRishi.Srivatsavai@Sun.COM     }                                   \
148*10491SRishi.Srivatsavai@Sun.COM     free (PTR);                         \
149*10491SRishi.Srivatsavai@Sun.COM     PTR = NULL;                         \
150*10491SRishi.Srivatsavai@Sun.COM   }
151*10491SRishi.Srivatsavai@Sun.COM 
152*10491SRishi.Srivatsavai@Sun.COM #define STP_STRDUP(PTR, SRC, MSG)       \
153*10491SRishi.Srivatsavai@Sun.COM   {                                     \
154*10491SRishi.Srivatsavai@Sun.COM     PTR = strdup (SRC);                 \
155*10491SRishi.Srivatsavai@Sun.COM     if (! PTR) {                        \
156*10491SRishi.Srivatsavai@Sun.COM       STP_FATAL("strdup", MSG, -7);     \
157*10491SRishi.Srivatsavai@Sun.COM     }                                   \
158*10491SRishi.Srivatsavai@Sun.COM   }
159*10491SRishi.Srivatsavai@Sun.COM 
160*10491SRishi.Srivatsavai@Sun.COM #define STP_NEW_IN_LIST(WHAT, TYPE, LIST, MSG)  \
161*10491SRishi.Srivatsavai@Sun.COM   {                                             \
162*10491SRishi.Srivatsavai@Sun.COM     STP_MALLOC(WHAT, TYPE, MSG);                \
163*10491SRishi.Srivatsavai@Sun.COM     WHAT->next = LIST;                          \
164*10491SRishi.Srivatsavai@Sun.COM     LIST = WHAT;                                \
165*10491SRishi.Srivatsavai@Sun.COM   }
166*10491SRishi.Srivatsavai@Sun.COM 
167*10491SRishi.Srivatsavai@Sun.COM /* for debug trace messages */
168*10491SRishi.Srivatsavai@Sun.COM 
169*10491SRishi.Srivatsavai@Sun.COM #ifdef STP_DBG
170*10491SRishi.Srivatsavai@Sun.COM #if defined(__LINUX__)
171*10491SRishi.Srivatsavai@Sun.COM extern char* sprint_time_stump (void);
172*10491SRishi.Srivatsavai@Sun.COM #define stp_trace(F, B...) printf("%s:" F "\n", sprint_time_stump(), ##B)
173*10491SRishi.Srivatsavai@Sun.COM #elif defined(__SUN__)
174*10491SRishi.Srivatsavai@Sun.COM #define	stp_trace	(*stp_vectors->trace)
175*10491SRishi.Srivatsavai@Sun.COM #else
176*10491SRishi.Srivatsavai@Sun.COM extern ULONG stp_trace (const char* fmt, ...);
177*10491SRishi.Srivatsavai@Sun.COM #endif
178*10491SRishi.Srivatsavai@Sun.COM #else /* !STP_DBG */
179*10491SRishi.Srivatsavai@Sun.COM #define stp_trace(F, B...) ((void)0)
180*10491SRishi.Srivatsavai@Sun.COM #endif /* STP_DBG */
181*10491SRishi.Srivatsavai@Sun.COM 
182*10491SRishi.Srivatsavai@Sun.COM 
183*10491SRishi.Srivatsavai@Sun.COM /* Inner usage definitions & functions */
184*10491SRishi.Srivatsavai@Sun.COM 
185*10491SRishi.Srivatsavai@Sun.COM #if defined(__LINUX__) || defined(__SUN__)
186*10491SRishi.Srivatsavai@Sun.COM #  define RSTP_INIT_CRITICAL_PATH_PROTECTIO
187*10491SRishi.Srivatsavai@Sun.COM #  define RSTP_CRITICAL_PATH_START
188*10491SRishi.Srivatsavai@Sun.COM #  define RSTP_CRITICAL_PATH_END
189*10491SRishi.Srivatsavai@Sun.COM #else
190*10491SRishi.Srivatsavai@Sun.COM #  define RSTP_INIT_CRITICAL_PATH_PROTECTIO STP_OUT_psos_init_semaphore ()
191*10491SRishi.Srivatsavai@Sun.COM #  define RSTP_CRITICAL_PATH_START          STP_OUT_psos_close_semaphore ()
192*10491SRishi.Srivatsavai@Sun.COM #  define RSTP_CRITICAL_PATH_END            STP_OUT_psos_open_semaphore ()
193*10491SRishi.Srivatsavai@Sun.COM    extern void STP_OUT_psos_init_semaphore (void);
194*10491SRishi.Srivatsavai@Sun.COM    extern void STP_OUT_psos_close_semaphore (void);
195*10491SRishi.Srivatsavai@Sun.COM    extern void STP_OUT_psos_open_semaphore (void);
196*10491SRishi.Srivatsavai@Sun.COM #endif
197*10491SRishi.Srivatsavai@Sun.COM 
198*10491SRishi.Srivatsavai@Sun.COM #endif /*  _STP_BASE_H__ */
199