1 /*
2  *
3  * Copyright 2009-2016, LabN Consulting, L.L.C.
4  *
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; see the file COPYING; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 
22 #include "lib/zebra.h"
23 #include "lib/prefix.h"
24 #include "lib/agg_table.h"
25 #include "lib/memory.h"
26 #include "lib/vty.h"
27 
28 #include "bgpd/bgpd.h"
29 #include "bgpd/bgp_route.h"
30 
31 #include "bgpd/rfapi/vnc_export_table.h"
32 #include "bgpd/rfapi/rfapi_private.h"
33 #include "bgpd/rfapi/rfapi_import.h"
34 #include "bgpd/rfapi/vnc_debug.h"
35 
vnc_etn_get(struct bgp * bgp,vnc_export_type_t type,const struct prefix * p)36 struct agg_node *vnc_etn_get(struct bgp *bgp, vnc_export_type_t type,
37 			     const struct prefix *p)
38 {
39 	struct agg_table *t = NULL;
40 	struct agg_node *rn = NULL;
41 	afi_t afi;
42 
43 	if (!bgp || !bgp->rfapi)
44 		return NULL;
45 
46 	afi = family2afi(p->family);
47 	assert(afi == AFI_IP || afi == AFI_IP6);
48 
49 	switch (type) {
50 	case EXPORT_TYPE_BGP:
51 		if (!bgp->rfapi->rt_export_bgp[afi])
52 			bgp->rfapi->rt_export_bgp[afi] = agg_table_init();
53 		t = bgp->rfapi->rt_export_bgp[afi];
54 		break;
55 
56 	case EXPORT_TYPE_ZEBRA:
57 		if (!bgp->rfapi->rt_export_zebra[afi])
58 			bgp->rfapi->rt_export_zebra[afi] = agg_table_init();
59 		t = bgp->rfapi->rt_export_zebra[afi];
60 		break;
61 	}
62 
63 	if (t)
64 		rn = agg_node_get(t, p);
65 	return rn;
66 }
67 
vnc_etn_lookup(struct bgp * bgp,vnc_export_type_t type,const struct prefix * p)68 struct agg_node *vnc_etn_lookup(struct bgp *bgp, vnc_export_type_t type,
69 				const struct prefix *p)
70 {
71 	struct agg_table *t = NULL;
72 	struct agg_node *rn = NULL;
73 	afi_t afi;
74 
75 	if (!bgp || !bgp->rfapi)
76 		return NULL;
77 
78 	afi = family2afi(p->family);
79 	assert(afi == AFI_IP || afi == AFI_IP6);
80 
81 	switch (type) {
82 	case EXPORT_TYPE_BGP:
83 		if (!bgp->rfapi->rt_export_bgp[afi])
84 			bgp->rfapi->rt_export_bgp[afi] = agg_table_init();
85 		t = bgp->rfapi->rt_export_bgp[afi];
86 		break;
87 
88 	case EXPORT_TYPE_ZEBRA:
89 		if (!bgp->rfapi->rt_export_zebra[afi])
90 			bgp->rfapi->rt_export_zebra[afi] = agg_table_init();
91 		t = bgp->rfapi->rt_export_zebra[afi];
92 		break;
93 	}
94 
95 	if (t)
96 		rn = agg_node_lookup(t, p);
97 	return rn;
98 }
99 
vnc_eti_get(struct bgp * bgp,vnc_export_type_t etype,const struct prefix * p,struct peer * peer,uint8_t type,uint8_t subtype)100 struct vnc_export_info *vnc_eti_get(struct bgp *bgp, vnc_export_type_t etype,
101 				    const struct prefix *p, struct peer *peer,
102 				    uint8_t type, uint8_t subtype)
103 {
104 	struct agg_node *etn;
105 	struct vnc_export_info *eti;
106 
107 	etn = vnc_etn_get(bgp, etype, p);
108 	assert(etn);
109 
110 	for (eti = etn->info; eti; eti = eti->next) {
111 		if (peer == eti->peer && type == eti->type
112 		    && subtype == eti->subtype) {
113 
114 			break;
115 		}
116 	}
117 
118 	if (eti) {
119 		agg_unlock_node(etn);
120 	} else {
121 		eti = XCALLOC(MTYPE_RFAPI_ETI, sizeof(struct vnc_export_info));
122 		assert(eti);
123 		eti->node = etn;
124 		eti->peer = peer;
125 		peer_lock(peer);
126 		eti->type = type;
127 		eti->subtype = subtype;
128 		eti->next = etn->info;
129 		etn->info = eti;
130 	}
131 
132 	return eti;
133 }
134 
vnc_eti_delete(struct vnc_export_info * goner)135 void vnc_eti_delete(struct vnc_export_info *goner)
136 {
137 	struct agg_node *etn;
138 	struct vnc_export_info *eti;
139 	struct vnc_export_info *eti_prev = NULL;
140 
141 	etn = goner->node;
142 
143 	for (eti = etn->info; eti; eti_prev = eti, eti = eti->next) {
144 		if (eti == goner)
145 			break;
146 	}
147 
148 	if (!eti) {
149 		vnc_zlog_debug_verbose("%s: COULDN'T FIND ETI", __func__);
150 		return;
151 	}
152 
153 	if (eti_prev) {
154 		eti_prev->next = goner->next;
155 	} else {
156 		etn->info = goner->next;
157 	}
158 
159 	peer_unlock(eti->peer);
160 	goner->node = NULL;
161 	XFREE(MTYPE_RFAPI_ETI, goner);
162 
163 	agg_unlock_node(etn);
164 }
165 
vnc_eti_checktimer(struct bgp * bgp,vnc_export_type_t etype,const struct prefix * p,struct peer * peer,uint8_t type,uint8_t subtype)166 struct vnc_export_info *vnc_eti_checktimer(struct bgp *bgp,
167 					   vnc_export_type_t etype,
168 					   const struct prefix *p,
169 					   struct peer *peer, uint8_t type,
170 					   uint8_t subtype)
171 {
172 	struct agg_node *etn;
173 	struct vnc_export_info *eti;
174 
175 	etn = vnc_etn_lookup(bgp, etype, p);
176 	if (!etn)
177 		return NULL;
178 
179 	for (eti = etn->info; eti; eti = eti->next) {
180 		if (peer == eti->peer && type == eti->type
181 		    && subtype == eti->subtype) {
182 
183 			break;
184 		}
185 	}
186 
187 	agg_unlock_node(etn);
188 
189 	if (eti && eti->timer)
190 		return eti;
191 
192 	return NULL;
193 }
194