NetlinkPipe.cc 4.41 KB
Newer Older
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
// NetlinkPipe.cc

#ifdef LINUX

#include "lib.hh"
#include "NetlinkPipe.hh"

extern "C"
{
#include <sys/types.h>
#include <sys/socket.h>

#include <netlink/list.h>
#include <netlink/object.h>
#include <netlink/route/rtnl.h>
#include <netlink/route/route.h>
#include <netlink/route/link.h>
#include <netlink/route/class.h>
#include <netlink/route/qdisc.h>
#include <netlink/route/sch/plr.h>
#include <netlink/route/sch/delay.h>
#include <netlink/route/sch/htb.h>
}

using namespace std;

NetlinkPipe::NetlinkPipe(std::string const & iface, std::string const & pipeno)
{
	interfaceName = iface;
	pipeNumber = pipeno;
	nl_handle = NULL;
	class_cache = NULL;
	qdisc_cache = NULL;
	
	init();
	/* test(); */
}

void NetlinkPipe::test(void)
{
	Parameter testParam(Parameter::LOSS, 50000);

	updateParameter(testParam);
}

int NetlinkPipe::init(void)
{
	struct nl_cache *link_cache;
	int handle;
	string str;

	link_cache = NULL;

	cerr << "Got pipe number " << pipeNumber << endl;
	handle = hexStringToInt(pipeNumber);
	cerr << "handle: " << handle << endl;

	nl_handle = nl_handle_alloc();
	if (nl_handle == NULL) {
		cerr << "Unable to allocate nl_handle" << endl;
		return -1;
	}

	if (nl_connect(nl_handle, NETLINK_ROUTE) < 0) {
		cerr << "Unable to allocate Netlink socket" << endl;
		return -1;
	}
	link_cache = rtnl_link_alloc_cache(nl_handle);
	if (link_cache == NULL) {
		cerr << "Unable to allocate link cache" << endl;
		return -1;
	}

	ifindex = rtnl_link_name2i(link_cache, interfaceName.c_str());
	if (ifindex == RTNL_LINK_NOT_FOUND) {
		cerr << "Unable to translate link name to ifindex" << endl;
		return -1;
	}
	nl_cache_free(link_cache);

	qdisc_cache = rtnl_qdisc_alloc_cache(nl_handle);
	if (qdisc_cache == NULL) {
		cerr << "Unable to allocate qdisc cache" << endl;
		return -1;
	}

	plrHandle = handle << 16;
	delayHandle = (handle + 0x10) << 16;
	htbHandle = (handle + 0x20) << 16;

	class_cache = rtnl_class_alloc_cache(nl_handle, ifindex);
	if (class_cache == NULL) {
		cerr << "Unable to allocate class cache" << endl;
		return -1;
	}

	htbClassHandle = htbHandle + 1;


	return 0;
}

NetlinkPipe::~NetlinkPipe()
{
	if (qdisc_cache)
		nl_cache_free(qdisc_cache);

	if (class_cache)
		nl_cache_free(class_cache);

	if (nl_handle)
		nl_close(nl_handle);
}

void NetlinkPipe::reset(void)
{
	map<Parameter::ParameterType, Parameter>::iterator pos = g::defaultParameters.begin();
	map<Parameter::ParameterType, Parameter>::iterator limit = g::defaultParameters.end();

	for (; pos != limit; ++pos)
	{
		updateParameter(pos->second);
	}
}

void NetlinkPipe::resetParameter(Parameter const & newParameter)
{
	updateParameter(newParameter);
}

void NetlinkPipe::updateParameter(Parameter const & newParameter)
{
	struct rtnl_qdisc *qdisc;
	struct rtnl_class *htbClass;

	qdisc = NULL;

	switch(newParameter.getType()) {
		case Parameter::BANDWIDTH:
				htbClass = rtnl_class_get(class_cache, htbClassHandle);
				if (htbClass == NULL) {
					cerr << "Couldn't find htb class " << htbClassHandle << endl;
					return;
				}
145
146
				rtnl_htb_set_rate(htbClass, newParameter.getValue() * 1000);
				rtnl_htb_set_ceil(htbClass, newParameter.getValue() * 1000);
147
148
149
150
151
152
153
154
155
				rtnl_class_change(nl_handle, htbClass, NULL);
				rtnl_class_put(htbClass);
                                break;
		case Parameter::DELAY:
				qdisc = rtnl_qdisc_get(qdisc_cache, ifindex, delayHandle);
				if (qdisc == NULL) {
					cerr << "Couldn't find delay qdisc " << delayHandle << endl;
					return;
				}
156
				rtnl_delay_set_delay(qdisc, newParameter.getValue() * 1000);
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
				rtnl_qdisc_change(nl_handle, qdisc, NULL);
				rtnl_qdisc_put(qdisc);
                                break;
		case Parameter::LOSS:
				qdisc = rtnl_qdisc_get(qdisc_cache, ifindex, plrHandle);
				if (qdisc == NULL) {
					cerr << "Couldn't find plr qdisc " << plrHandle << endl;
					return;
				}
				rtnl_plr_set_plr(qdisc, newParameter.getValue());
				rtnl_qdisc_change(nl_handle, qdisc, NULL);
				rtnl_qdisc_put(qdisc);
                                break;
		case Parameter::LINK_UP:
				uint32_t value;

				if (newParameter.getValue()) {
Ryan Jackson's avatar
Ryan Jackson committed
174
					value = 0;
175
176
				}
				else {
Ryan Jackson's avatar
Ryan Jackson committed
177
					value = 0xffffffff;
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
				}
				qdisc = rtnl_qdisc_get(qdisc_cache, ifindex, plrHandle);
				if (qdisc == NULL) {
					cerr << "Couldn't find plr qdisc " << plrHandle << endl;
					return;
				}
				rtnl_plr_set_plr(qdisc, value);
				rtnl_qdisc_change(nl_handle, qdisc, NULL);
				rtnl_qdisc_put(qdisc);
                                break;
		default:
				break;
	}
}

#endif