-
Notifications
You must be signed in to change notification settings - Fork 10
/
direct.c
161 lines (124 loc) · 3.57 KB
/
direct.c
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
#include <linux/netdevice.h>
#include <linux/skbuff.h>
#include <linux/rtnetlink.h>
#include "rl.h"
#include "tx.h"
#include "rx.h"
#ifndef DIRECT
#error "Compiling direct.c without -DDIRECT"
#endif
extern struct net_device *iso_netdev;
static netdev_tx_t (*old_ndo_start_xmit)(struct sk_buff *, struct net_device *);
netdev_tx_t iso_ndo_start_xmit(struct sk_buff *, struct net_device *);
rx_handler_result_t iso_rx_handler(struct sk_buff **);
int iso_tx_hook_init(struct iso_tx_context *);
void iso_tx_hook_exit(struct iso_tx_context *);
int iso_rx_hook_init(struct iso_rx_context *);
void iso_rx_hook_exit(struct iso_rx_context *);
enum iso_verdict iso_tx(struct sk_buff *skb, const struct net_device *out, struct iso_tx_context *);
enum iso_verdict iso_rx(struct sk_buff *skb, const struct net_device *in, struct iso_rx_context *);
/* Called with bh disabled */
inline void skb_xmit(struct sk_buff *skb) {
struct netdev_queue *txq;
int cpu;
int locked = 0;
if(likely(old_ndo_start_xmit != NULL)) {
cpu = smp_processor_id();
txq = netdev_get_tx_queue(iso_netdev, skb_get_queue_mapping(skb));
if(txq->xmit_lock_owner != cpu) {
HARD_TX_LOCK(iso_netdev, txq, cpu);
locked = 1;
}
/* XXX: will the else condition happen? */
if(!netif_tx_queue_stopped(txq)) {
old_ndo_start_xmit(skb, iso_netdev);
} else {
kfree_skb(skb);
}
if(locked) {
HARD_TX_UNLOCK(iso_netdev, txq);
}
}
}
int iso_tx_hook_init(struct iso_tx_context *txctx) {
struct net_device_ops *ops;
if(iso_netdev == NULL || iso_netdev->netdev_ops == NULL)
return 1;
ops = (struct net_device_ops *)iso_netdev->netdev_ops;
rtnl_lock();
old_ndo_start_xmit = txctx->xmit = ops->ndo_start_xmit;
ops->ndo_start_xmit = iso_ndo_start_xmit;
rtnl_unlock();
synchronize_net();
return 0;
}
void iso_tx_hook_exit(struct iso_tx_context *txctx) {
struct net_device_ops *ops = (struct net_device_ops *)iso_netdev->netdev_ops;
rtnl_lock();
ops->ndo_start_xmit = txctx->xmit;
rtnl_unlock();
synchronize_net();
}
int iso_rx_hook_init(struct iso_rx_context *rxctx) {
int ret = 0;
if(iso_netdev == NULL)
return 1;
rtnl_lock();
ret = netdev_rx_handler_register(iso_netdev, iso_rx_handler, NULL);
rtnl_unlock();
/* Wait till stack sees our new handler */
synchronize_net();
return ret;
}
void iso_rx_hook_exit(struct iso_rx_context *rxctx) {
rtnl_lock();
netdev_rx_handler_unregister(iso_netdev);
rtnl_unlock();
synchronize_net();
}
/* Called with bh disabled */
netdev_tx_t iso_ndo_start_xmit(struct sk_buff *skb, struct net_device *out) {
enum iso_verdict verdict;
struct netdev_queue *txq;
int cpu = smp_processor_id();
netdev_tx_t ret = NETDEV_TX_OK;
txq = netdev_get_tx_queue(iso_netdev, skb_get_queue_mapping(skb));
HARD_TX_UNLOCK(iso_netdev, txq);
skb_reset_mac_header(skb);
verdict = iso_tx(skb, out, &global_txcontext);
switch(verdict) {
case ISO_VERDICT_DROP:
ret = NETDEV_TX_BUSY;
break;
case ISO_VERDICT_PASS:
skb_xmit(skb);
break;
case ISO_VERDICT_SUCCESS:
case ISO_VERDICT_ERROR:
default:
break;
}
HARD_TX_LOCK(iso_netdev, txq, cpu);
return ret;
}
rx_handler_result_t iso_rx_handler(struct sk_buff **pskb) {
struct sk_buff *skb = *pskb;
enum iso_verdict verdict;
if(unlikely(skb->pkt_type == PACKET_LOOPBACK))
return RX_HANDLER_PASS;
verdict = iso_rx(skb, iso_netdev, &global_rxcontext);
switch(verdict) {
case ISO_VERDICT_DROP:
kfree_skb(skb);
return RX_HANDLER_CONSUMED;
case ISO_VERDICT_SUCCESS:
case ISO_VERDICT_PASS:
default:
return RX_HANDLER_PASS;
}
/* Unreachable */
return RX_HANDLER_PASS;
}
/* Local Variables: */
/* indent-tabs-mode:t */
/* End: */