xref: /onnv-gate/usr/src/lib/librstp/common/vector.c (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 /* STP priority vectors API : 17.4.2 */
24*10491SRishi.Srivatsavai@Sun.COM 
25*10491SRishi.Srivatsavai@Sun.COM #include "base.h"
26*10491SRishi.Srivatsavai@Sun.COM #include "stp_bpdu.h"
27*10491SRishi.Srivatsavai@Sun.COM #include "vector.h"
28*10491SRishi.Srivatsavai@Sun.COM #include "stp_vectors.h"
29*10491SRishi.Srivatsavai@Sun.COM 
30*10491SRishi.Srivatsavai@Sun.COM int
STP_VECT_compare_bridge_id(BRIDGE_ID * b1,BRIDGE_ID * b2)31*10491SRishi.Srivatsavai@Sun.COM STP_VECT_compare_bridge_id (BRIDGE_ID* b1, BRIDGE_ID* b2)
32*10491SRishi.Srivatsavai@Sun.COM {
33*10491SRishi.Srivatsavai@Sun.COM   if (b1->prio < b2->prio)
34*10491SRishi.Srivatsavai@Sun.COM     return -1;
35*10491SRishi.Srivatsavai@Sun.COM 
36*10491SRishi.Srivatsavai@Sun.COM   if (b1->prio > b2->prio)
37*10491SRishi.Srivatsavai@Sun.COM     return 1;
38*10491SRishi.Srivatsavai@Sun.COM   return memcmp (b1->addr, b2->addr, 6);
39*10491SRishi.Srivatsavai@Sun.COM }
40*10491SRishi.Srivatsavai@Sun.COM 
41*10491SRishi.Srivatsavai@Sun.COM void
STP_VECT_copy(OUT PRIO_VECTOR_T * t,IN PRIO_VECTOR_T * f)42*10491SRishi.Srivatsavai@Sun.COM STP_VECT_copy (OUT PRIO_VECTOR_T* t, IN PRIO_VECTOR_T* f)
43*10491SRishi.Srivatsavai@Sun.COM {
44*10491SRishi.Srivatsavai@Sun.COM   (void) memcpy (t, f, sizeof (PRIO_VECTOR_T));
45*10491SRishi.Srivatsavai@Sun.COM }
46*10491SRishi.Srivatsavai@Sun.COM 
47*10491SRishi.Srivatsavai@Sun.COM void
STP_VECT_create(OUT PRIO_VECTOR_T * t,IN BRIDGE_ID * root_br,IN unsigned long root_path_cost,IN BRIDGE_ID * design_bridge,IN PORT_ID design_port,IN PORT_ID bridge_port)48*10491SRishi.Srivatsavai@Sun.COM STP_VECT_create (OUT PRIO_VECTOR_T* t,
49*10491SRishi.Srivatsavai@Sun.COM                  IN BRIDGE_ID* root_br,
50*10491SRishi.Srivatsavai@Sun.COM                  IN unsigned long root_path_cost,
51*10491SRishi.Srivatsavai@Sun.COM                  IN BRIDGE_ID* design_bridge,
52*10491SRishi.Srivatsavai@Sun.COM                  IN PORT_ID design_port,
53*10491SRishi.Srivatsavai@Sun.COM                  IN PORT_ID bridge_port)
54*10491SRishi.Srivatsavai@Sun.COM {
55*10491SRishi.Srivatsavai@Sun.COM   (void) memcpy (&t->root_bridge, root_br, sizeof (BRIDGE_ID));
56*10491SRishi.Srivatsavai@Sun.COM   t->root_path_cost = root_path_cost;
57*10491SRishi.Srivatsavai@Sun.COM   (void) memcpy (&t->design_bridge, design_bridge, sizeof (BRIDGE_ID));
58*10491SRishi.Srivatsavai@Sun.COM   t->design_port = design_port;
59*10491SRishi.Srivatsavai@Sun.COM   t->bridge_port = bridge_port;
60*10491SRishi.Srivatsavai@Sun.COM }
61*10491SRishi.Srivatsavai@Sun.COM 
62*10491SRishi.Srivatsavai@Sun.COM int
STP_VECT_compare_vector(PRIO_VECTOR_T * v1,PRIO_VECTOR_T * v2)63*10491SRishi.Srivatsavai@Sun.COM STP_VECT_compare_vector (PRIO_VECTOR_T* v1, PRIO_VECTOR_T* v2)
64*10491SRishi.Srivatsavai@Sun.COM {
65*10491SRishi.Srivatsavai@Sun.COM   int bridcmp;
66*10491SRishi.Srivatsavai@Sun.COM 
67*10491SRishi.Srivatsavai@Sun.COM   bridcmp = STP_VECT_compare_bridge_id (&v1->root_bridge, &v2->root_bridge);
68*10491SRishi.Srivatsavai@Sun.COM   if (bridcmp < 0) return bridcmp;
69*10491SRishi.Srivatsavai@Sun.COM 
70*10491SRishi.Srivatsavai@Sun.COM   if (! bridcmp) {
71*10491SRishi.Srivatsavai@Sun.COM     bridcmp = v1->root_path_cost - v2->root_path_cost;
72*10491SRishi.Srivatsavai@Sun.COM     if (bridcmp < 0) return bridcmp;
73*10491SRishi.Srivatsavai@Sun.COM     if (! bridcmp) {
74*10491SRishi.Srivatsavai@Sun.COM       bridcmp = STP_VECT_compare_bridge_id (&v1->design_bridge, &v2->design_bridge);
75*10491SRishi.Srivatsavai@Sun.COM       if (bridcmp < 0) return bridcmp;
76*10491SRishi.Srivatsavai@Sun.COM       if (! bridcmp) {
77*10491SRishi.Srivatsavai@Sun.COM         bridcmp = v1->design_port - v2->design_port;
78*10491SRishi.Srivatsavai@Sun.COM         if (bridcmp < 0) return bridcmp;
79*10491SRishi.Srivatsavai@Sun.COM         if (! bridcmp)
80*10491SRishi.Srivatsavai@Sun.COM           return v1->bridge_port - v2->bridge_port;
81*10491SRishi.Srivatsavai@Sun.COM       }
82*10491SRishi.Srivatsavai@Sun.COM     }
83*10491SRishi.Srivatsavai@Sun.COM   }
84*10491SRishi.Srivatsavai@Sun.COM 
85*10491SRishi.Srivatsavai@Sun.COM   return bridcmp;
86*10491SRishi.Srivatsavai@Sun.COM }
87*10491SRishi.Srivatsavai@Sun.COM 
88*10491SRishi.Srivatsavai@Sun.COM static unsigned short
stp_vect_get_short(IN unsigned char * f)89*10491SRishi.Srivatsavai@Sun.COM stp_vect_get_short (IN unsigned char* f)
90*10491SRishi.Srivatsavai@Sun.COM {
91*10491SRishi.Srivatsavai@Sun.COM   /* LINTED: alignment */
92*10491SRishi.Srivatsavai@Sun.COM   return ntohs (*(unsigned short *)f);
93*10491SRishi.Srivatsavai@Sun.COM }
94*10491SRishi.Srivatsavai@Sun.COM 
95*10491SRishi.Srivatsavai@Sun.COM static void
stp_vect_set_short(IN unsigned short f,OUT unsigned char * t)96*10491SRishi.Srivatsavai@Sun.COM stp_vect_set_short (IN unsigned short f, OUT unsigned char* t)
97*10491SRishi.Srivatsavai@Sun.COM {
98*10491SRishi.Srivatsavai@Sun.COM   /* LINTED: alignment */
99*10491SRishi.Srivatsavai@Sun.COM   *(unsigned short *)t = htons (f);
100*10491SRishi.Srivatsavai@Sun.COM }
101*10491SRishi.Srivatsavai@Sun.COM 
102*10491SRishi.Srivatsavai@Sun.COM static void
stp_vect_get_bridge_id(IN unsigned char * c_br,OUT BRIDGE_ID * bridge_id)103*10491SRishi.Srivatsavai@Sun.COM stp_vect_get_bridge_id (IN unsigned char* c_br, OUT BRIDGE_ID* bridge_id)
104*10491SRishi.Srivatsavai@Sun.COM {
105*10491SRishi.Srivatsavai@Sun.COM   bridge_id->prio = stp_vect_get_short (c_br);
106*10491SRishi.Srivatsavai@Sun.COM   (void) memcpy (bridge_id->addr, c_br + 2, 6);
107*10491SRishi.Srivatsavai@Sun.COM }
108*10491SRishi.Srivatsavai@Sun.COM 
109*10491SRishi.Srivatsavai@Sun.COM static void
stp_vect_set_bridge_id(IN BRIDGE_ID * bridge_id,OUT unsigned char * c_br)110*10491SRishi.Srivatsavai@Sun.COM stp_vect_set_bridge_id (IN BRIDGE_ID* bridge_id, OUT unsigned char* c_br)
111*10491SRishi.Srivatsavai@Sun.COM {
112*10491SRishi.Srivatsavai@Sun.COM   stp_vect_set_short (bridge_id->prio, c_br);
113*10491SRishi.Srivatsavai@Sun.COM   (void) memcpy (c_br + 2, bridge_id->addr, 6);
114*10491SRishi.Srivatsavai@Sun.COM }
115*10491SRishi.Srivatsavai@Sun.COM 
116*10491SRishi.Srivatsavai@Sun.COM void
STP_VECT_get_vector(IN BPDU_BODY_T * b,OUT PRIO_VECTOR_T * v)117*10491SRishi.Srivatsavai@Sun.COM STP_VECT_get_vector (IN BPDU_BODY_T* b, OUT PRIO_VECTOR_T* v)
118*10491SRishi.Srivatsavai@Sun.COM {
119*10491SRishi.Srivatsavai@Sun.COM   stp_vect_get_bridge_id (b->root_id, &v->root_bridge);
120*10491SRishi.Srivatsavai@Sun.COM 
121*10491SRishi.Srivatsavai@Sun.COM   /* LINTED: alignment */
122*10491SRishi.Srivatsavai@Sun.COM   v->root_path_cost = ntohl (*((long*) b->root_path_cost));
123*10491SRishi.Srivatsavai@Sun.COM 
124*10491SRishi.Srivatsavai@Sun.COM   stp_vect_get_bridge_id (b->bridge_id, &v->design_bridge);
125*10491SRishi.Srivatsavai@Sun.COM 
126*10491SRishi.Srivatsavai@Sun.COM   v->design_port = stp_vect_get_short (b->port_id);
127*10491SRishi.Srivatsavai@Sun.COM }
128*10491SRishi.Srivatsavai@Sun.COM 
129*10491SRishi.Srivatsavai@Sun.COM void
STP_VECT_set_vector(IN PRIO_VECTOR_T * v,OUT BPDU_BODY_T * b)130*10491SRishi.Srivatsavai@Sun.COM STP_VECT_set_vector (IN PRIO_VECTOR_T* v, OUT BPDU_BODY_T* b)
131*10491SRishi.Srivatsavai@Sun.COM {
132*10491SRishi.Srivatsavai@Sun.COM   unsigned long root_path_cost;
133*10491SRishi.Srivatsavai@Sun.COM 
134*10491SRishi.Srivatsavai@Sun.COM   stp_vect_set_bridge_id (&v->root_bridge, b->root_id);
135*10491SRishi.Srivatsavai@Sun.COM 
136*10491SRishi.Srivatsavai@Sun.COM   root_path_cost = htonl (v->root_path_cost);
137*10491SRishi.Srivatsavai@Sun.COM   (void) memcpy (b->root_path_cost, &root_path_cost, 4);
138*10491SRishi.Srivatsavai@Sun.COM 
139*10491SRishi.Srivatsavai@Sun.COM   stp_vect_set_bridge_id (&v->design_bridge, b->bridge_id);
140*10491SRishi.Srivatsavai@Sun.COM 
141*10491SRishi.Srivatsavai@Sun.COM   stp_vect_set_short (v->design_port, b->port_id);
142*10491SRishi.Srivatsavai@Sun.COM }
143*10491SRishi.Srivatsavai@Sun.COM 
144*10491SRishi.Srivatsavai@Sun.COM #ifdef STP_DBG
145*10491SRishi.Srivatsavai@Sun.COM 
146*10491SRishi.Srivatsavai@Sun.COM /*ARGSUSED*/
147*10491SRishi.Srivatsavai@Sun.COM void
STP_VECT_br_id_print(IN char * title,IN BRIDGE_ID * br_id,IN Bool cr)148*10491SRishi.Srivatsavai@Sun.COM STP_VECT_br_id_print (IN char *title, IN BRIDGE_ID* br_id, IN Bool cr)
149*10491SRishi.Srivatsavai@Sun.COM {
150*10491SRishi.Srivatsavai@Sun.COM   stp_trace ("%s=%04lX-%02x%02x%02x%02x%02x%02x",
151*10491SRishi.Srivatsavai@Sun.COM             title,
152*10491SRishi.Srivatsavai@Sun.COM           (unsigned long) br_id->prio,
153*10491SRishi.Srivatsavai@Sun.COM           (unsigned char) br_id->addr[0],
154*10491SRishi.Srivatsavai@Sun.COM           (unsigned char) br_id->addr[1],
155*10491SRishi.Srivatsavai@Sun.COM           (unsigned char) br_id->addr[2],
156*10491SRishi.Srivatsavai@Sun.COM           (unsigned char) br_id->addr[3],
157*10491SRishi.Srivatsavai@Sun.COM           (unsigned char) br_id->addr[4],
158*10491SRishi.Srivatsavai@Sun.COM           (unsigned char) br_id->addr[5]);
159*10491SRishi.Srivatsavai@Sun.COM #ifndef __SUN__
160*10491SRishi.Srivatsavai@Sun.COM   stp_trace (cr ? "\n" : " ");
161*10491SRishi.Srivatsavai@Sun.COM #endif
162*10491SRishi.Srivatsavai@Sun.COM }
163*10491SRishi.Srivatsavai@Sun.COM 
164*10491SRishi.Srivatsavai@Sun.COM void
STP_VECT_print(IN char * title,IN PRIO_VECTOR_T * v)165*10491SRishi.Srivatsavai@Sun.COM STP_VECT_print (IN char *title, IN PRIO_VECTOR_T *v)
166*10491SRishi.Srivatsavai@Sun.COM {
167*10491SRishi.Srivatsavai@Sun.COM   stp_trace ("%s:", title);
168*10491SRishi.Srivatsavai@Sun.COM   STP_VECT_br_id_print ("rootBr", &v->root_bridge, False);
169*10491SRishi.Srivatsavai@Sun.COM 
170*10491SRishi.Srivatsavai@Sun.COM /****
171*10491SRishi.Srivatsavai@Sun.COM   stp_trace (" rpc=%ld ", (long) v->root_path_cost);
172*10491SRishi.Srivatsavai@Sun.COM ****/
173*10491SRishi.Srivatsavai@Sun.COM 
174*10491SRishi.Srivatsavai@Sun.COM   STP_VECT_br_id_print ("designBr", &v->design_bridge, False);
175*10491SRishi.Srivatsavai@Sun.COM 
176*10491SRishi.Srivatsavai@Sun.COM /****/
177*10491SRishi.Srivatsavai@Sun.COM   stp_trace (" dp=%lx bp=%lx ",
178*10491SRishi.Srivatsavai@Sun.COM           (unsigned long) v->design_port,
179*10491SRishi.Srivatsavai@Sun.COM           (unsigned long) v->bridge_port);
180*10491SRishi.Srivatsavai@Sun.COM /***********/
181*10491SRishi.Srivatsavai@Sun.COM   stp_trace ("\n");
182*10491SRishi.Srivatsavai@Sun.COM }
183*10491SRishi.Srivatsavai@Sun.COM #endif
184