summaryrefslogtreecommitdiffstats
path: root/zebra/zebra_ns.c
blob: ac724a3299bc715787ec6dd98be854b03976f6fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* zebra NS Routines
 * Copyright (C) 2016 Cumulus Networks, Inc.
 *                    Donald Sharp
 *
 * This file is part of Quagga.
 *
 * Quagga is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * Quagga is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; see the file COPYING; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */
#include "zebra.h"

#include "lib/ns.h"
#include "lib/vrf.h"
#include "lib/prefix.h"
#include "lib/memory.h"

#include "rtadv.h"
#include "zebra_ns.h"
#include "zebra_vrf.h"
#include "zebra_memory.h"
#include "rt.h"
#include "zebra_vxlan.h"

DEFINE_MTYPE(ZEBRA, ZEBRA_NS, "Zebra Name Space")

static __inline int
zebra_ns_table_entry_compare(const struct zebra_ns_table *e1,
			     const struct zebra_ns_table *e2);

RB_GENERATE(zebra_ns_table_head, zebra_ns_table, zebra_ns_table_entry,
	    zebra_ns_table_entry_compare);

static struct zebra_ns *dzns;

static __inline int
zebra_ns_table_entry_compare(const struct zebra_ns_table *e1,
			     const struct zebra_ns_table *e2)
{
	if (e1->tableid == e2->tableid)
		return (e1->afi - e2->afi);

	return e1->tableid - e2->tableid;
}

struct zebra_ns *zebra_ns_lookup(ns_id_t ns_id)
{
	return dzns;
}

/* Do global enable actions - open sockets, read kernel config etc. */
int zebra_ns_enable(ns_id_t ns_id, void **info)
{
	struct zebra_ns *zns = (struct zebra_ns *)(*info);

#if defined(HAVE_RTADV)
	rtadv_init(zns);
#endif

	kernel_init(zns);
	interface_list(zns);
	route_read(zns);

	return 0;
}

struct route_table *zebra_ns_find_table(struct zebra_ns *zns,
					uint32_t tableid, afi_t afi)
{
	struct zebra_ns_tables finder;
	struct zebra_ns_tables *znst;

	memset(&finder, 0, sizeof(finder));
	finder.afi = afi;
	finder.tableid = tableid;
	znst = RB_FIND(zebra_ns_tables_head, &zns->ns_tables, &finder);

	if (znst)
		return znst->table;
	else
		return NULL;
}

struct route_table *zebra_ns_get_table(struct zebra_ns *zns,
				       struct zebra_vrf *zvrf, uint32_t tableid,
				       afi_t afi)
{
	struct zebra_ns_table finder;
	struct zebra_ns_table *znst;
	rib_table_info_t *info;

	memset(&finder, 0, sizeof(finder));
	finder.afi = afi;
	finder.tableid = tableid;
	znst = RB_FIND(zebra_ns_table_head, &zns->ns_tables, &finder);

	if (znst)
		return znst->table;

	znst = XCALLOC(MTYPE_ZEBRA_NS, sizeof(*znst));
	znst->tableid = tableid;
	znst->afi = afi;
	znst->table =
		(afi == AFI_IP6) ? srcdest_table_init() : route_table_init();

	info = XCALLOC(MTYPE_RIB_TABLE_INFO, sizeof(*info));
	info->zvrf = zvrf;
	info->afi = afi;
	info->safi = SAFI_UNICAST;
	znst->table->info = info;
	znst->table->cleanup = zebra_rtable_node_cleanup;

	RB_INSERT(zebra_ns_table_head, &zns->ns_tables, znst);
	return znst->table;
}

static struct zebra_ns_table *zebra_ns_free_table(struct zebra_ns_table *znst)
{
	void *table_info;
	rib_close_table(znst->table);

	table_info = znst->table->info;
	route_table_finish(znst->table);
	XFREE(MTYPE_RIB_TABLE_INFO, table_info);
	XFREE(MTYPE_ZEBRA_NS, znst);
	return NULL;
}

int zebra_ns_disable(ns_id_t ns_id, void **info)
{
	struct zebra_ns_table *znst;
	struct zebra_ns *zns = (struct zebra_ns *)(*info);

	while ((znst = RB_ROOT(zebra_ns_table_head, &zns->ns_tables))
	       != NULL) {
		RB_REMOVE(zebra_ns_table_head, &zns->ns_tables, znst);
		znst = zebra_ns_free_table(znst);
	}
	route_table_finish(zns->if_table);
	zebra_vxlan_ns_disable(zns);
#if defined(HAVE_RTADV)
	rtadv_terminate(zns);
#endif

	kernel_terminate(zns);

	return 0;
}


int zebra_ns_init(void)
{
	dzns = XCALLOC(MTYPE_ZEBRA_NS, sizeof(struct zebra_ns));

	ns_init();

	/* Do any needed per-NS data structure allocation. */
	dzns->if_table = route_table_init();
	zebra_vxlan_ns_init(dzns);

	/* Register zebra VRF callbacks, create and activate default VRF. */
	zebra_vrf_init();

	/* Default NS is activated */
	zebra_ns_enable(NS_DEFAULT, (void **)&dzns);

	return 0;
}