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