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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
|
/******************************************************************************/
/* */
/* bypass library, Copyright (c) 2004-2007 Silicom, Ltd */
/* */
/* 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, located in the file LICENSE. */
/* */
/* */
/* bypass.c */
/* */
/******************************************************************************/
#if defined(CONFIG_SMP) && !defined(__SMP__)
#define __SMP__
#endif
#include <linux/module.h>
#include <linux/kernel.h>
#include <asm/unistd.h>
#include <linux/sched.h>
#include <linux/wait.h>
#include <linux/netdevice.h> /* struct device, and other headers */
#include <linux/kernel_stat.h>
#include <linux/pci.h>
#include <linux/rtnetlink.h>
#include <linux/ethtool.h>
#include <net/net_namespace.h>
#include "bplibk.h"
#define MOD_NAME "bypass"
#define VERSION "\n"MOD_NAME" version 9.0.4\n"
MODULE_AUTHOR("www.silicom.co.il");
MODULE_LICENSE("GPL");
static int do_cmd(struct net_device *dev, struct ifreq *ifr, int cmd, int *data)
{
int ret = -1;
struct if_bypass *bypass_cb;
bypass_cb = (struct if_bypass *)ifr;
bypass_cb->cmd = cmd;
bypass_cb->data = *data;
if (dev->netdev_ops && dev->netdev_ops->ndo_do_ioctl) {
ret = dev->netdev_ops->ndo_do_ioctl(dev, ifr, SIOCGIFBYPASS);
*data = bypass_cb->data;
}
return ret;
}
static int doit(int cmd, int if_index, int *data)
{
struct ifreq ifr;
int ret = -1;
struct net_device *dev;
struct net_device *n;
for_each_netdev_safe(&init_net, dev, n) {
if (dev->ifindex == if_index) {
ret = do_cmd(dev, &ifr, cmd, data);
if (ret < 0)
ret = -1;
}
}
return ret;
}
#define bp_symbol_get(fn_name) symbol_get(fn_name)
#define bp_symbol_put(fn_name) symbol_put(fn_name)
#define SET_BPLIB_INT_FN(fn_name, arg_type, arg, ret) \
({ int (*fn_ex)(arg_type) = NULL; \
fn_ex = bp_symbol_get(fn_name##_sd); \
if (fn_ex) { \
ret = fn_ex(arg); \
bp_symbol_put(fn_name##_sd); \
} else { \
ret = -1; \
} \
})
#define SET_BPLIB_INT_FN2(fn_name, arg_type, arg, arg_type1, arg1, ret)\
({ int (*fn_ex)(arg_type, arg_type1) = NULL; \
fn_ex = bp_symbol_get(fn_name##_sd); \
if (fn_ex) { \
ret = fn_ex(arg, arg1); \
bp_symbol_put(fn_name##_sd); \
} else { \
ret = -1; \
} \
})
#define SET_BPLIB_INT_FN3(fn_name, arg_type, arg, arg_type1, arg1, \
arg_type2, arg2, ret) \
({ int (*fn_ex)(arg_type, arg_type1, arg_type2) = NULL; \
fn_ex = bp_symbol_get(fn_name##_sd); \
if (fn_ex) { \
ret = fn_ex(arg, arg1, arg2); \
bp_symbol_put(fn_name##_sd); \
} else { \
ret = -1; \
} \
})
#define DO_BPLIB_GET_ARG_FN(fn_name, ioctl_val, if_index) \
({ int data, ret = 0; \
if (is_dev_sd(if_index)) { \
SET_BPLIB_INT_FN(fn_name, int, if_index, ret); \
return ret; \
} \
return doit(ioctl_val, if_index, &data); \
})
#define DO_BPLIB_SET_ARG_FN(fn_name, ioctl_val, if_index, arg) \
({ int data, ret = 0; \
if (is_dev_sd(if_index)) { \
SET_BPLIB_INT_FN2(fn_name, int, if_index, int, \
arg, ret); \
return ret; \
} \
data = arg; \
return doit(ioctl_val, if_index, &data); \
})
static int is_dev_sd(int if_index)
{
int ret = 0;
SET_BPLIB_INT_FN(is_bypass, int, if_index, ret);
return ret >= 0 ? 1 : 0;
}
static int is_bypass_dev(int if_index)
{
struct pci_dev *pdev = NULL;
struct net_device *dev = NULL;
struct ifreq ifr;
int ret = 0;
int data = 0;
while ((pdev = pci_get_class(PCI_CLASS_NETWORK_ETHERNET << 8, pdev))) {
dev = pci_get_drvdata(pdev);
if ((dev != NULL) && (dev->ifindex == if_index)) {
if ((pdev->vendor == SILICOM_VID) &&
(pdev->device >= SILICOM_BP_PID_MIN) &&
(pdev->device <= SILICOM_BP_PID_MAX)) {
goto send_cmd;
}
#if defined(BP_VENDOR_SUPPORT) && defined(ETHTOOL_GDRVINFO)
else {
struct ethtool_drvinfo info;
const struct ethtool_ops *ops =
dev->ethtool_ops;
int k = 0;
if (ops->get_drvinfo) {
memset(&info, 0, sizeof(info));
info.cmd = ETHTOOL_GDRVINFO;
ops->get_drvinfo(dev, &info);
for (; bp_desc_array[k]; k++)
if (!(strcmp(bp_desc_array[k],
info.driver)))
goto send_cmd;
}
}
#endif
return -1;
}
}
send_cmd:
ret = do_cmd(dev, &ifr, IS_BYPASS, &data);
return ret < 0 ? -1 : ret;
}
static int is_bypass(int if_index)
{
int ret = 0;
SET_BPLIB_INT_FN(is_bypass, int, if_index, ret);
if (ret < 0)
return is_bypass_dev(if_index);
return ret;
}
EXPORT_SYMBOL(is_bypass);
static int get_bypass_slave(int if_index)
{
DO_BPLIB_GET_ARG_FN(get_bypass_slave, GET_BYPASS_SLAVE, if_index);
}
EXPORT_SYMBOL(get_bypass_slave);
static int get_bypass_caps(int if_index)
{
DO_BPLIB_GET_ARG_FN(get_bypass_caps, GET_BYPASS_CAPS, if_index);
}
EXPORT_SYMBOL(get_bypass_caps);
static int get_wd_set_caps(int if_index)
{
DO_BPLIB_GET_ARG_FN(get_wd_set_caps, GET_WD_SET_CAPS, if_index);
}
EXPORT_SYMBOL(get_wd_set_caps);
static int set_bypass(int if_index, int bypass_mode)
{
DO_BPLIB_SET_ARG_FN(set_bypass, SET_BYPASS, if_index, bypass_mode);
}
EXPORT_SYMBOL(set_bypass);
static int get_bypass(int if_index)
{
DO_BPLIB_GET_ARG_FN(get_bypass, GET_BYPASS, if_index);
}
EXPORT_SYMBOL(get_bypass);
static int get_bypass_change(int if_index)
{
DO_BPLIB_GET_ARG_FN(get_bypass_change, GET_BYPASS_CHANGE, if_index);
}
EXPORT_SYMBOL(get_bypass_change);
static int set_dis_bypass(int if_index, int dis_bypass)
{
DO_BPLIB_SET_ARG_FN(set_dis_bypass, SET_DIS_BYPASS, if_index,
dis_bypass);
}
EXPORT_SYMBOL(set_dis_bypass);
static int get_dis_bypass(int if_index)
{
DO_BPLIB_GET_ARG_FN(get_dis_bypass, GET_DIS_BYPASS, if_index);
}
EXPORT_SYMBOL(get_dis_bypass);
static int set_bypass_pwoff(int if_index, int bypass_mode)
{
DO_BPLIB_SET_ARG_FN(set_bypass_pwoff, SET_BYPASS_PWOFF, if_index,
bypass_mode);
}
EXPORT_SYMBOL(set_bypass_pwoff);
static int get_bypass_pwoff(int if_index)
{
DO_BPLIB_GET_ARG_FN(get_bypass_pwoff, GET_BYPASS_PWOFF, if_index);
}
EXPORT_SYMBOL(get_bypass_pwoff);
static int set_bypass_pwup(int if_index, int bypass_mode)
{
DO_BPLIB_SET_ARG_FN(set_bypass_pwup, SET_BYPASS_PWUP, if_index,
bypass_mode);
}
EXPORT_SYMBOL(set_bypass_pwup);
static int get_bypass_pwup(int if_index)
{
DO_BPLIB_GET_ARG_FN(get_bypass_pwup, GET_BYPASS_PWUP, if_index);
}
EXPORT_SYMBOL(get_bypass_pwup);
static int set_bypass_wd(int if_index, int ms_timeout, int *ms_timeout_set)
{
int data = ms_timeout;
int ret = 0;
if (is_dev_sd(if_index)) {
SET_BPLIB_INT_FN3(set_bypass_wd, int, if_index, int, ms_timeout,
int *, ms_timeout_set, ret);
} else {
ret = doit(SET_BYPASS_WD, if_index, &data);
if (ret > 0) {
*ms_timeout_set = ret;
ret = 0;
}
}
return ret;
}
EXPORT_SYMBOL(set_bypass_wd);
static int get_bypass_wd(int if_index, int *ms_timeout_set)
{
int *data = ms_timeout_set;
int ret = 0;
if (is_dev_sd(if_index))
SET_BPLIB_INT_FN2(get_bypass_wd, int, if_index, int *,
ms_timeout_set, ret);
else
ret = doit(GET_BYPASS_WD, if_index, data);
return ret;
}
EXPORT_SYMBOL(get_bypass_wd);
static int get_wd_expire_time(int if_index, int *ms_time_left)
{
int *data = ms_time_left, ret = 0;
if (is_dev_sd(if_index)) {
SET_BPLIB_INT_FN2(get_wd_expire_time, int, if_index, int *,
ms_time_left, ret);
} else {
ret = doit(GET_WD_EXPIRE_TIME, if_index, data);
if ((ret == 0) && (*data != 0))
ret = 1;
}
return ret;
}
EXPORT_SYMBOL(get_wd_expire_time);
static int reset_bypass_wd_timer(int if_index)
{
DO_BPLIB_GET_ARG_FN(reset_bypass_wd_timer, RESET_BYPASS_WD_TIMER,
if_index);
}
EXPORT_SYMBOL(reset_bypass_wd_timer);
static int set_std_nic(int if_index, int bypass_mode)
{
DO_BPLIB_SET_ARG_FN(set_std_nic, SET_STD_NIC, if_index, bypass_mode);
}
EXPORT_SYMBOL(set_std_nic);
static int get_std_nic(int if_index)
{
DO_BPLIB_GET_ARG_FN(get_std_nic, GET_STD_NIC, if_index);
}
EXPORT_SYMBOL(get_std_nic);
static int set_tx(int if_index, int tx_state)
{
DO_BPLIB_SET_ARG_FN(set_tx, SET_TX, if_index, tx_state);
}
EXPORT_SYMBOL(set_tx);
static int get_tx(int if_index)
{
DO_BPLIB_GET_ARG_FN(get_tx, GET_TX, if_index);
}
EXPORT_SYMBOL(get_tx);
static int set_tap(int if_index, int tap_mode)
{
DO_BPLIB_SET_ARG_FN(set_tap, SET_TAP, if_index, tap_mode);
}
EXPORT_SYMBOL(set_tap);
static int get_tap(int if_index)
{
DO_BPLIB_GET_ARG_FN(get_tap, GET_TAP, if_index);
}
EXPORT_SYMBOL(get_tap);
static int get_tap_change(int if_index)
{
DO_BPLIB_GET_ARG_FN(get_tap_change, GET_TAP_CHANGE, if_index);
}
EXPORT_SYMBOL(get_tap_change);
static int set_dis_tap(int if_index, int dis_tap)
{
DO_BPLIB_SET_ARG_FN(set_dis_tap, SET_DIS_TAP, if_index, dis_tap);
}
EXPORT_SYMBOL(set_dis_tap);
static int get_dis_tap(int if_index)
{
DO_BPLIB_GET_ARG_FN(get_dis_tap, GET_DIS_TAP, if_index);
}
EXPORT_SYMBOL(get_dis_tap);
static int set_tap_pwup(int if_index, int tap_mode)
{
DO_BPLIB_SET_ARG_FN(set_tap_pwup, SET_TAP_PWUP, if_index, tap_mode);
}
EXPORT_SYMBOL(set_tap_pwup);
static int get_tap_pwup(int if_index)
{
DO_BPLIB_GET_ARG_FN(get_tap_pwup, GET_TAP_PWUP, if_index);
}
EXPORT_SYMBOL(get_tap_pwup);
static int set_bp_disc(int if_index, int disc_mode)
{
DO_BPLIB_SET_ARG_FN(set_bp_disc, SET_DISC, if_index, disc_mode);
}
EXPORT_SYMBOL(set_bp_disc);
static int get_bp_disc(int if_index)
{
DO_BPLIB_GET_ARG_FN(get_bp_disc, GET_DISC, if_index);
}
EXPORT_SYMBOL(get_bp_disc);
static int get_bp_disc_change(int if_index)
{
DO_BPLIB_GET_ARG_FN(get_bp_disc_change, GET_DISC_CHANGE, if_index);
}
EXPORT_SYMBOL(get_bp_disc_change);
static int set_bp_dis_disc(int if_index, int dis_disc)
{
DO_BPLIB_SET_ARG_FN(set_bp_dis_disc, SET_DIS_DISC, if_index, dis_disc);
}
EXPORT_SYMBOL(set_bp_dis_disc);
static int get_bp_dis_disc(int if_index)
{
DO_BPLIB_GET_ARG_FN(get_bp_dis_disc, GET_DIS_DISC, if_index);
}
EXPORT_SYMBOL(get_bp_dis_disc);
static int set_bp_disc_pwup(int if_index, int disc_mode)
{
DO_BPLIB_SET_ARG_FN(set_bp_disc_pwup, SET_DISC_PWUP, if_index,
disc_mode);
}
EXPORT_SYMBOL(set_bp_disc_pwup);
static int get_bp_disc_pwup(int if_index)
{
DO_BPLIB_GET_ARG_FN(get_bp_disc_pwup, GET_DISC_PWUP, if_index);
}
EXPORT_SYMBOL(get_bp_disc_pwup);
static int set_wd_exp_mode(int if_index, int mode)
{
DO_BPLIB_SET_ARG_FN(set_wd_exp_mode, SET_WD_EXP_MODE, if_index, mode);
}
EXPORT_SYMBOL(set_wd_exp_mode);
static int get_wd_exp_mode(int if_index)
{
DO_BPLIB_GET_ARG_FN(get_wd_exp_mode, GET_WD_EXP_MODE, if_index);
}
EXPORT_SYMBOL(get_wd_exp_mode);
static int set_wd_autoreset(int if_index, int time)
{
DO_BPLIB_SET_ARG_FN(set_wd_autoreset, SET_WD_AUTORESET, if_index, time);
}
EXPORT_SYMBOL(set_wd_autoreset);
static int get_wd_autoreset(int if_index)
{
DO_BPLIB_GET_ARG_FN(get_wd_autoreset, GET_WD_AUTORESET, if_index);
}
EXPORT_SYMBOL(get_wd_autoreset);
static int set_tpl(int if_index, int tpl_mode)
{
DO_BPLIB_SET_ARG_FN(set_tpl, SET_TPL, if_index, tpl_mode);
}
EXPORT_SYMBOL(set_tpl);
static int get_tpl(int if_index)
{
DO_BPLIB_GET_ARG_FN(get_tpl, GET_TPL, if_index);
}
EXPORT_SYMBOL(get_tpl);
static int set_bp_hw_reset(int if_index, int mode)
{
DO_BPLIB_SET_ARG_FN(set_tpl, SET_BP_HW_RESET, if_index, mode);
}
EXPORT_SYMBOL(set_bp_hw_reset);
static int get_bp_hw_reset(int if_index)
{
DO_BPLIB_GET_ARG_FN(get_tpl, GET_BP_HW_RESET, if_index);
}
EXPORT_SYMBOL(get_bp_hw_reset);
static int get_bypass_info(int if_index, struct bp_info *bp_info)
{
int ret = 0;
if (is_dev_sd(if_index)) {
SET_BPLIB_INT_FN2(get_bypass_info, int, if_index,
struct bp_info *, bp_info, ret);
} else {
struct net_device *dev;
struct net_device *n;
for_each_netdev_safe(&init_net, dev, n) {
if (dev->ifindex == if_index) {
struct if_bypass_info *bypass_cb;
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
bypass_cb = (struct if_bypass_info *)𝔦
bypass_cb->cmd = GET_BYPASS_INFO;
if (dev->netdev_ops &&
dev->netdev_ops->ndo_do_ioctl)
ret = dev->netdev_ops->ndo_do_ioctl(dev,
&ifr, SIOCGIFBYPASS);
else
ret = -1;
if (ret == 0)
memcpy(bp_info, &bypass_cb->bp_info,
sizeof(struct bp_info));
ret = ret < 0 ? -1 : 0;
break;
}
}
}
return ret;
}
EXPORT_SYMBOL(get_bypass_info);
static int __init init_lib_module(void)
{
printk(VERSION);
return 0;
}
static void __exit cleanup_lib_module(void)
{
}
module_init(init_lib_module);
module_exit(cleanup_lib_module);
|