-
Notifications
You must be signed in to change notification settings - Fork 81
/
todevice.hh
148 lines (131 loc) · 4.06 KB
/
todevice.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
#ifndef CLICK_TODEVICE_USERLEVEL_HH
#define CLICK_TODEVICE_USERLEVEL_HH
#include <click/batchelement.hh>
#include <click/string.hh>
#include <click/task.hh>
#include <click/timer.hh>
#include <click/notifier.hh>
#include "elements/userlevel/fromdevice.hh"
CLICK_DECLS
/*
* =title ToDevice.u
* =c
* ToDevice(DEVNAME [, I<keywords>])
* =s netdevices
* sends packets to network device (user-level)
* =d
*
* This manual page describes the user-level version of the ToDevice element.
* For the Linux kernel module element, read the ToDevice(n) manual page.
*
* Pulls packets and sends them out the named device using
* Berkeley Packet Filters (or Linux equivalent).
*
* Keyword arguments are:
*
* =over 8
*
* =item BURST
*
* Integer. Maximum number of packets to pull per scheduling. Defaults to 1.
*
* =item METHOD
*
* Word. Defines the method ToDevice will use to write packets to the
* device. Linux targets generally support PCAP and LINUX; other targets
* support PCAP or, occasionally, other methods. Defaults to the method
* specified for a matching L<FromDevice(n)>, or the first supported
* method among PCAP, DEVBPF, LINUX and PCAPFD otherwise.
*
* =item DEBUG
*
* Boolean. If true, print out debug messages.
*
* =back
*
* This element is only available at user level.
*
* =n
*
* Packets sent via ToDevice should already have a link-level
* header prepended. This means that ARP processing,
* for example, must already have been done.
*
* The L<FromDevice(n)> element's OUTBOUND keyword argument determines whether
* FromDevice receives packets sent by a ToDevice element for the same
* device.
*
* Packets that are written successfully are sent on output 0, if it exists.
* Packets that fail to be written are pushed out output 1, if it exists.
* KernelTun lets you send IP packets to the host kernel's IP processing code,
* sort of like the kernel module's ToHost element.
*
* =a
* FromDevice.u, FromDump, ToDump, KernelTun, ToDevice(n) */
#if defined(__linux__)
# define TODEVICE_ALLOW_LINUX 1
#endif
#if HAVE_PCAP && (HAVE_PCAP_INJECT || HAVE_PCAP_SENDPACKET)
extern "C" {
# include <pcap.h>
}
# define TODEVICE_ALLOW_PCAP 1
#endif
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__APPLE__) || defined(__NetBSD__)
# define TODEVICE_ALLOW_DEVBPF 1
#elif defined(__sun)
# define TODEVICE_ALLOW_PCAPFD 1
#endif
class ToDevice : public BatchElement { public:
ToDevice() CLICK_COLD;
~ToDevice() CLICK_COLD;
const char *class_name() const override { return "ToDevice"; }
const char *port_count() const override { return "1/0-2"; }
const char *processing() const override { return "l/h"; }
const char *flags() const { return "S2"; }
int configure_phase() const override { return KernelFilter::CONFIGURE_PHASE_TODEVICE; }
int configure(Vector<String> &, ErrorHandler *) CLICK_COLD;
int initialize(ErrorHandler *) CLICK_COLD;
void cleanup(CleanupStage) CLICK_COLD;
void add_handlers() CLICK_COLD;
String ifname() const { return _ifname; }
int fd() const { return _fd; }
bool run_task(Task *);
void selected(int fd, int mask);
protected:
Task _task;
Timer _timer;
String _ifname;
uint64_t _tot_count;
#if TODEVICE_ALLOW_PCAP
pcap_t *_pcap;
#endif
#if TODEVICE_ALLOW_LINUX || TODEVICE_ALLOW_DEVBPF || TODEVICE_ALLOW_PCAPFD
int _fd;
#endif
enum { method_default, method_linux, method_pcap, method_devbpf, method_pcapfd };
int _method;
NotifierSignal _signal;
#if HAVE_BATCH
PacketBatch *_q;
#else
Packet *_q;
#endif
int _burst;
bool _debug;
#if TODEVICE_ALLOW_PCAP
bool _my_pcap;
#endif
#if TODEVICE_ALLOW_LINUX || TODEVICE_ALLOW_DEVBPF || TODEVICE_ALLOW_PCAPFD
bool _my_fd;
#endif
int _backoff;
int _pulls;
enum { h_debug, h_signal, h_pulls, h_q, h_count };
FromDevice *find_fromdevice() const;
int send_packet(Packet *p);
static int write_param(const String &in_s, Element *e, void *vparam, ErrorHandler *errh) CLICK_COLD;
static String read_param(Element *e, void *thunk) CLICK_COLD;
};
CLICK_ENDDECLS
#endif