-
Notifications
You must be signed in to change notification settings - Fork 80
/
fromhost.hh
199 lines (139 loc) · 5.29 KB
/
fromhost.hh
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
// -*- mode: c++; c-basic-offset: 4 -*-
#ifndef CLICK_FROMHOST_HH
#define CLICK_FROMHOST_HH
#include <click/element.hh>
#include <click/etheraddress.hh>
#include <click/timer.hh>
#include <click/notifier.hh>
/*
=c
FromHost(DEVNAME [, PREFIX, I<KEYWORDS>])
=s comm
reads packets from Linux
=d
Captures packets orginating from the Linux kernel and pushes them on output 0.
The type of packet depends on the TYPE keyword argument. For TYPE ETHER,
output packets have Ethernet headers; only the protocol field is interesting.
For TYPE IP, output packets are IP packets. TYPE ETHER is the default,
although TYPE IP is probably more useful.
Installs a fake interface called DEVNAME. If PREFIX is given, changes the
routing table so that every packet destined for PREFIX = ADDR/MASK is sent
through that interface. The packet then leaves on output 0. The device's
native address is ADDR.
After the fake device is created, the effect of bringing up the interface
and changing the routing table is analogous to:
% /sbin/ifconfig DEVNAME up
% /sbin/route add -net ADDR netmask MASK DEVNAME
This element is only available in the Linux kernel module.
Keyword arguments are:
=over 8
=item TYPE
Specifies the device type. Valid options are C<ETHER> and C<IP>. Currently
defaults to C<ETHER> with a warning.
=item PREFIX
Specifies the fake device's IP address and netmask.
=item ETHER
Ethernet address. Specifies the fake device's Ethernet address. Default is
00-01-02-03-04-05.
=item MTU
Integer. The maximum transmission unit reported to Linux for the device.
Defaults to 1500.
=item CAPACITY
Unsigned. Maximum length of the internal queue that collects packets from
Linux. Defaults to 100. If this queue overflows, packets will be silently
dropped.
=item CLEAR_ANNO
Boolean. Sets whether or not to clear the user annotation area on packets
received from Linux. This consists of the user annotations.
If false, this area is left as it was received from Linux.
(Note that the packet type, device, and header annotations are left as is.)
Defaults to true.
=item BURST
Integer. Sets the BURST parameter affecting how many packets are
emitted per task execution. BURST is 8 by default.
=back
=n
If TYPE is IP, FromHost will set the packet's IP header and destination IP
address annotations. Packets with bad IP version or header length are dropped
or emitted on output 1 if it exists. Note that FromHost doesn't check IP
checksums or full packet lengths.
If TYPE is ETHER, Linux will send ARP queries to the fake device. You must
respond to these queries in order to receive any IP packets, but you can
obviously respond with any Ethernet address you'd like. Here is one common
idiom:
FromHost(fake0, 192.0.0.1/8, TYPE ETHER)
-> fromhost_cl :: Classifier(12/0806, 12/0800);
fromhost_cl[0] -> ARPResponder(0.0.0.0/0 1-1-1-1-1-1) -> ToHost;
fromhost_cl[1] -> ... // IP packets
=e
FromHost(fake0, 192.0.0.1/8) -> ...;
=h length r
Return the current length of the internal queue.
=h capacity r
Return the maximum length of the internal queue (the CAPACITY argument).
=h drops r
Return the number of packets dropped off the internal queue so far.
=a ToHost, FromDevice, PollDevice, ToDevice */
#include <click/cxxprotect.h>
CLICK_CXX_PROTECT
#include <linux/netdevice.h>
#include <linux/route.h>
CLICK_CXX_UNPROTECT
#include <click/cxxunprotect.h>
#include <click/standard/storage.hh>
#include "elements/linuxmodule/anydevice.hh"
class EtherAddress;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 32)
typedef int netdev_tx_t;
#endif
class FromHost : public AnyDevice, public Storage { public:
FromHost() CLICK_COLD;
~FromHost() CLICK_COLD;
static void static_initialize();
const char *class_name() const override { return "FromHost"; }
const char *port_count() const override { return "0/1-2"; }
const char *processing() const override { return PUSH; }
void *cast(const char *name);
int configure_phase() const { return CONFIGURE_PHASE_FROMHOST; }
int configure(Vector<String> &, ErrorHandler *) CLICK_COLD;
int initialize(ErrorHandler *) CLICK_COLD;
void add_handlers() CLICK_COLD;
void cleanup(CleanupStage) CLICK_COLD;
int set_device_addresses(ErrorHandler *);
bool run_task(Task *);
private:
EtherAddress _macaddr;
IPAddress _destaddr;
IPAddress _destmask;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 32)
net_device_stats _stats;
#endif
int _burst;
Task _task;
Timer _wakeup_timer;
enum { smq_size = 4 };
union {
Packet * volatile smq[smq_size + 1];
Packet * volatile *lgq;
} _q;
NotifierSignal _nonfull_signal;
int _mtu;
unsigned _drops;
unsigned _ninvalid;
static FromHost *configuring;
net_device *new_device(const char *);
static netdev_tx_t fl_tx(struct sk_buff *, net_device *);
inline Packet * volatile *queue() {
return _capacity <= smq_size ? _q.smq : _q.lgq;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 32)
net_device_stats *stats() { return &_dev->stats; }
#else
net_device_stats *stats() { return &_stats; }
static net_device_stats *fl_stats(net_device *dev);
#endif
enum { h_length, h_burst, h_ether };
static String read_handler(Element *e, void *thunk) CLICK_COLD;
static int write_handler(const String &, Element *, void *, ErrorHandler *) CLICK_COLD;
};
#endif