File: us_mfa.h

package info (click to toggle)
mrd6 0.9.5-release-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,308 kB
  • ctags: 3,956
  • sloc: cpp: 25,728; perl: 462; makefile: 281; ansic: 142; sh: 67
file content (291 lines) | stat: -rw-r--r-- 7,055 bytes parent folder | download
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 * Multicast Routing Daemon (MRD)
 *   us_mfa.h
 *
 * Copyright (C) 2004 Univ. Aveiro, Instituto Telecomunicacoes - Polo Aveiro
 *
 * This program 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 of the License, or
 * (at your option) any later version.
 *
 * This program 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; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Authors:	Hugo Santos, <hsantos@av.it.pt>
 */

#ifndef _mrd_us_mfa_h_
#define _mrd_us_mfa_h_

#include <mrd/mrd.h>
#include <mrd/mfa.h>
#include <mrd/address.h>
#include <mrd/interface.h>
#include <mrd/timers.h>

#include <stdint.h>

#include <list>
#include <map>

class interface;
class router;

struct ip6_hdr;

class us_mfa_group_source;
class us_mfa_group;
class us_mfa;

class us_mfa_group_source : public mfa_group_source {
public:
	us_mfa_group_source(us_mfa_group *, const in6_addr &, uint32_t, action *);

	const in6_addr &address() const { return m_addr; }

	void route(int, ip6_hdr *, uint16_t);

	bool is_iif(int intf) const { return m_iif && m_iif->index() == intf; }
	bool has_oif(interface *) const;

	void change_flags(uint32_t, action);

	void set_iif(interface *);
	void release_iif(interface *);

	void add_oif(interface *);
	void release_oif(interface *);

	void update_stats();

	void output_info(base_stream &, bool, bool) const;

	void clear_interface_references(const inet6_addr &, interface *);

	void get_input_counter(uint64_t &bytes) const;
	void get_forwarding_counter(uint64_t &bytes) const;

private:
	typedef std::vector<interface *> oifs;

	us_mfa_group *m_owner;
	in6_addr m_addr;

	interface *m_iif;
	oifs m_oifs;

	uint32_t m_flags;

	uint32_t m_interest_flags;

	enum {
		stat_forwarded = 0,
		stat_input,
		stat_forwarded_size,
		stat_wrong_iif,
		stat_count
	};

	mutable uint64_t m_stats[stat_count];

	/* enough for 10Gbps+ */
	mutable uint32_t m_fw_bag, m_fw_pkt_bag;

	mutable uint64_t stat_octet_count60s, stat_packet_count60s;
};

class us_mfa_group : public mfa_group {
public:
	us_mfa_group(router *, const inet6_addr &);

	void activate(bool);

	void route(int, ip6_hdr *, uint16_t);

	mfa_group_source *create_source_state(const in6_addr &, void *);
	mfa_group_source *get_source_state(const in6_addr &) const;
	void release_source_state(mfa_group_source *);

	void change_default_flags(uint32_t, mfa_group_source::action);

	void update_stats();

	void output_info(base_stream &, bool, bool) const;

	void clear_interface_references(const inet6_addr &, interface *);

private:
	us_mfa_group_source *match_source(const in6_addr &addr) const;

	typedef std::map<in6_addr, us_mfa_group_source *> sources;

	sources m_sources;

#define _SOURCE_CACHE_LEN	32
	mutable us_mfa_group_source *m_source_cache[_SOURCE_CACHE_LEN];
	bool m_useful_cache;

	void invalidate_source_cache();

	enum state {
		running,
		pending,
		denied
	};

	state m_state;

	uint32_t m_flags;
	mfa_group_source::action m_actions[mfa_group_source::event_count];

	mutable uint32_t m_fw_bag, m_fw_pkt_bag;
	mutable uint64_t stat_octet_count60s, stat_packet_count60s;
	friend class us_mfa_group_source;
};

#define _SOURCE_CACHE_HASH(x) \
		(((x).s6_addr32[2] ^ (x).s6_addr32[3]) & (_SOURCE_CACHE_LEN-1))

inline us_mfa_group_source *us_mfa_group::match_source(const in6_addr &addr) const {
	if (m_useful_cache) {
		register us_mfa_group_source *possible = m_source_cache[_SOURCE_CACHE_HASH(addr)];
		if (possible && possible->address() == addr)
			return possible;
	}

	sources::const_iterator k = m_sources.find(addr);
	if (k != m_sources.end()) {
		if (m_useful_cache) {
			m_source_cache[_SOURCE_CACHE_HASH(addr)] = k->second;
		}

		return k->second;
	}

	return 0;
}

/*!
 * \brief Implements the User Space Linux Multicast forwarding agent (MFA).
 *
 * This MFA implements does multicast forwarding completly in user-space
 * without any special kernel requirements besides IPv6 support. Internally
 * the forwarding is done using Raw sockets of type PF_PACKET.
 */
class us_mfa : public mfa_core {
public:
	us_mfa();

	bool pre_startup();
	bool check_startup();
	void shutdown();

	bool supports_stats() const { return true; }

	void send_icmpv6_toobig(interface *intf, ip6_hdr *, uint16_t) const;

	void added_interface(interface *);
	void removed_interface(interface *);

	bool output_info(base_stream &, bool counters, bool noempty) const;
	bool output_info(base_stream &, const std::vector<std::string> &) const;

	void discovered_source(int, const inet6_addr &, const inet6_addr &);

	mfa_group *create_group(router *, const inet6_addr &, void *);
	mfa_group *get_group(const inet6_addr &) const;
	void release_group(mfa_group *);

	void route(interface *, ip6_hdr *, uint16_t);

	void change_group_default_flags(uint32_t, mfa_group_source::action);

	void forward(interface *, ip6_hdr *, uint16_t) const;

	uint32_t m_grpflags;
	mfa_group_source::action m_grpactions[mfa_group_source::event_count];

	void clear_interface_references(interface *);

private:
	void data_available(uint32_t);
	void handle_ipv6(int, uint8_t *, uint16_t);

	void log_failed_packet(const interface *, int) const;

	socket0<us_mfa> m_rawsock;

	data_plane_source_discovery m_sourcedisc;

#ifndef LINUX_NO_MMAP
	void *m_mmaped;
	uint32_t m_mmapedlen;
	uint32_t m_framesize;
	uint8_t *m_mmapbuf;
#endif
	int m_icmpsock;

	void update_stats();

	timer<us_mfa> m_stat_timer;

	us_mfa_group *match_group(const in6_addr &) const;

	typedef std::map<inet6_addr, us_mfa_group *> groups;

	groups m_groups;

	typedef std::map<in6_addr, us_mfa_group *> singles;
	mutable singles m_singles;

#define _GROUP_CACHE_LEN 128
	struct grp_cache_entry {
		in6_addr addr;
		us_mfa_group *entry;
	};
	mutable grp_cache_entry m_grp_cache[_GROUP_CACHE_LEN];

	void invalidate_group_cache();
	void invalidate_group_cache(const in6_addr &);
};

#define _GROUP_CACHE_HASH(x) \
		(((x).s6_addr32[2] ^ (x).s6_addr32[3]) & (_GROUP_CACHE_LEN-1))

inline us_mfa_group *us_mfa::match_group(const in6_addr &addr) const {
	grp_cache_entry &ent = m_grp_cache[_GROUP_CACHE_HASH(addr)];
	if (ent.addr == addr && ent.entry) {
		return ent.entry;
	}

	singles::const_iterator i = m_singles.find(addr);
	if (i != m_singles.end()) {
		ent.addr = addr;
		ent.entry = i->second;
		return i->second;
	}

	for (groups::const_iterator k = m_groups.begin();
				k != m_groups.end(); ++k) {
		if (k->first.matches(addr)) {
			m_singles[addr] = k->second;
			return k->second;
		}
	}

	return 0;
}

inline bool us_mfa_group_source::has_oif(interface *oif) const {
	return std::find(m_oifs.begin(), m_oifs.end(), oif) != m_oifs.end();
}

#endif