-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlnlb.c
executable file
·2076 lines (1776 loc) · 58.6 KB
/
lnlb.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
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* LNLB - Linux Network Load Balancing
* Copyright (C) 2007 Primiano Tucci <mail@primianotucci.com>
*
* 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; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#define DRV_NAME "lnlb"
#define DRV_VERSION "0.1.2-beta"
#define DRV_DESCRIPTION "Linux Network Load Balancing"
#define DRV_AUTHOR "Primiano Tucci <mail@primianotucci.com>"
#include <linux/version.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/swap.h>
#include <linux/major.h>
#include <linux/slab.h>
#include <linux/poll.h>
#include <linux/list.h>
#include <linux/proc_fs.h>
#include <linux/slab.h>
#include <linux/timer.h>
#include <linux/fcntl.h>
#include <linux/init.h>
#include <linux/jhash.h>
#include <linux/skbuff.h>
#include <linux/ip.h>
#include <linux/netdevice.h>
#include <linux/sort.h>
#include <linux/etherdevice.h>
#include <linux/ethtool.h>
#include <linux/rtnetlink.h>
#include <linux/if.h>
#include <linux/udp.h>
#include <linux/if_arp.h>
#include <linux/if_ether.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/netfilter_arp.h>
#include <linux/crc32.h>
//<linux/if_lnlb.h>
#include "if_lnlb.h"
#include <linux/delay.h>
#include <asm/div64.h>
#define IP_ADDRESS_COMPARE(A,B) (((u_int32_t)A)!=((u_int32_t)B))
#define DUMPMAC(x) x[0],x[1],x[2],x[3],x[4],x[5]
#define NEXT_HB_TIMER (HZ*heartbeat_interval+jiffies)
#define NEXT_TO_TIMER (HZ*CONVERGENCE_DTIMEOUT/10+jiffies)
#define PROTO_MODULES_UBOUND (proto_modules_installed ? 0xFF : 1)
#define FRAG_EXPIRE_INTERVAL 2
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
# define IP_HDR(skb) ip_hdr(skb)
# define ARP_HDR(skb) arp_hdr(skb)
#else
# define IP_HDR(skb) ((skb)->nh.iph)
# define ARP_HDR(skb) ((skb)->nh.arph)
#endif
#ifndef MIN
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#endif
static int heartbeat_interval=HEARTBEAT_INTERVAL_DEFAULT;
module_param(heartbeat_interval,int,0444);
MODULE_PARM_DESC(heartbeat_interval,"Interval in seconds between each convergence.");
static int mode_unicast=1;
/* Note actually multicast mode (setting mode_unicast=0) has non sense since there is no IGMP support */
module_param(mode_unicast,int,0444);
MODULE_PARM_DESC(mode_unicast,"Select cluster MAC type: 1: Unicast / 0: Multicast.");
static struct ethtool_ops lnlb_ethtool_ops;
/* List of allocated devices (nlb0,nbl1...) */
static LIST_HEAD(dev_list);
static LIST_HEAD(dev_hook_list);
static struct proc_dir_entry* proc_file;
/*Netfilter hook and arp mangle hook structs*/
static struct nf_hook_ops *netfilter_hook=NULL;
static struct nf_hook_ops *arp_mangle_hook=NULL;
static struct lnlb_protocol_module *proto_modules[0xFF]; /* proto_modules[0] is the default handler */
static int proto_modules_installed=0; /* Tells the driver if any handler module (other than the default) has been installed*/
static DEFINE_RWLOCK(proto_modules_lock);
struct arp_payload
{
u_int8_t src_hw[ETH_ALEN];
u_int32_t src_ip;
u_int8_t dst_hw[ETH_ALEN];
u_int32_t dst_ip;
} __attribute__ ((packed));
struct dev_hook
{
struct list_head list;
struct net_device *dev;
struct packet_type heartbeat_ptype;
struct packet_type unicast_ptype;
};
struct lnlb_cluster_msg {
struct list_head list; /* For linking into frag_list */
uint32_t timestamp; /* Timestamp set when frame is received (is not sent across network, it's just to avoid frag_list pollution) */
unsigned char sender[ETH_ALEN]; /* Sender MAC address */
enum lnlb_msg_type type;
uint32_t len; /* Len of current frame data */
uint32_t flags; /* Optional flags */
uint8_t id; /* Message ID */
uint16_t frag_num; /* Fragment num */
uint16_t frag_total; /* Total fragments */
uint32_t crc; /* Global Message CRC32 */
char *data;
};
#define INIT_CLUSTER_MSG(msg) msg=(struct lnlb_cluster_msg) { .len=0, \
.id=0, \
.frag_num=0, \
.frag_total=1, \
.crc=0, \
.data=NULL }
/*------------------------------------------------------------------------------------------------------------------------
Prototypes
*------------------------------------------------------------------------------------------------------------------------*/
static int proc_read_callback(char *, char **, off_t, int, int *, void *);
static int proc_write_callback(struct file *, const char __user *, unsigned long , void *);
static int init_procfs(void);
static void cleanup_procfs(void);
static int net_open(struct net_device *);
static int net_close(struct net_device *);
static int net_xmit(struct sk_buff *, struct net_device *);
static struct net_device_stats *net_stats(struct net_device *);
static int ei_get_settings(struct net_device *, struct ethtool_cmd *);
static void ei_get_drvinfo(struct net_device *, struct ethtool_drvinfo *);
static u32 ei_get_link(struct net_device *);
static void setup_dev_callback(struct net_device *);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
static unsigned int netfilter_frame_hook(unsigned int , struct sk_buff *, const struct net_device *, const struct net_device *, int (*)(struct sk_buff *));
#else
static unsigned int netfilter_frame_hook(unsigned int , struct sk_buff **, const struct net_device *, const struct net_device *, int (*)(struct sk_buff *));
#endif
static int unicast_frame_hook(struct sk_buff *, struct net_device *, struct packet_type *, struct net_device *);
static inline void pass_skb_to_vdevice(struct sk_buff *,struct lnlb_struct *);
static int lnlb_by_skb (const struct sk_buff *,const struct net_device *,struct lnlb_struct **);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
static unsigned int arp_mangle(unsigned int, struct sk_buff *,const struct net_device *, const struct net_device *,int(*)(struct sk_buff*));
#else
static unsigned int arp_mangle(unsigned int, struct sk_buff **,const struct net_device *, const struct net_device *,int(*)(struct sk_buff*));
#endif
static int add_interface_hook(struct net_device *);
static void del_interface_hook(struct net_device *);
static int init_netfilter_hook(void);
static void clear_netfilter_hook(void);
static int instantiate_new_device(char*,struct net_device*,struct lnlb_struct**);
static int delete_device(char *);
static int __init drv_init(void);
static void drv_cleanup(void);
static void ip_to_mac(__be32,unsigned char *);
static struct lnlb_struct * lnlbif_by_name(const char *);
static struct lnlb_struct * lnlbif_by_ip(__be32);
static void device_set_ip(struct lnlb_struct *,__be32);
static int convergence_run(struct lnlb_struct *);
static int check_convergence_completed(struct lnlb_struct *);
static int broadcast_my_weigth(struct lnlb_struct *);
static int retrasm_my_weight(struct lnlb_struct *,unsigned char *);
static int broadcast_join(struct lnlb_struct *);
static int broadcast_welcome(struct lnlb_struct *);
static int broadcast_leave(struct lnlb_struct *);
static int broadcast_conntrack_status(struct lnlb_struct *);
static int send_conntrack_status(struct lnlb_struct *,unsigned char *);
static inline int broadcast_msg(struct lnlb_cluster_msg *,struct lnlb_struct *);
static int send_long_eth_msg(struct lnlb_cluster_msg *,struct lnlb_struct *,unsigned char *);
static int send_eth_msg(struct lnlb_cluster_msg *,struct lnlb_struct *,unsigned char *);
static struct lnlb_node_table_entry* seek_node_from_table(struct lnlb_struct *,unsigned char *);
static struct lnlb_node_table_entry* add_node_to_table(struct lnlb_struct *,unsigned char *);
static void delete_dead_nodes_from_table(struct lnlb_struct *);
static void advertise_node_death(struct lnlb_struct *,unsigned char *);
static void heartbeat_timer(unsigned long);
static void cluster_msg_handler(struct lnlb_cluster_msg *,struct lnlb_struct *);
static void convergence_timeout_timer(unsigned long);
static inline void delete_and_free_frag_msg(struct lnlb_cluster_msg *);
static int heartbeat_frame_hook(struct sk_buff *, struct net_device *, struct packet_type *, struct net_device *);
static int skb_node_affinity(struct lnlb_struct*,struct sk_buff*,int);
//static void dump_table(struct lnlb_struct *);
static int cmp_node_table_entries(const void *,const void *);
static void swap_node_table_entries(void *,void *,int);
static void process_queued_skbuff(struct lnlb_struct *);
static int find_related_node(struct lnlb_struct*,struct sk_buff *,unsigned char *);
int lnlb_register_ph(uint8_t,struct lnlb_protocol_module *);
void lnlb_unregister_ph(uint8_t);
static struct ethtool_ops lnlb_ethtool_ops =
{
.get_settings = ei_get_settings,
.get_drvinfo = ei_get_drvinfo,
.get_link = ei_get_link,
};
/*------------------------------------------------------------------------------------------------------------------------*
Inline functions
*------------------------------------------------------------------------------------------------------------------------*/
static inline int broadcast_msg(struct lnlb_cluster_msg *iMsg,struct lnlb_struct *iDev)
{
return send_eth_msg(iMsg,iDev,iDev->cluster_mac);
}
static inline void pass_skb_to_vdevice(struct sk_buff *iSkb,struct lnlb_struct *iDev)
{
iSkb->dev=iDev->dev;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
skb_reset_mac_header(iSkb);
#else
iSkb->mac.raw=iSkb->data;
#endif
iSkb->pkt_type = PACKET_HOST;
netif_receive_skb(iSkb); //TODO This or netif_rx_ni?
iDev->stats.rx_packets++;
iDev->stats.rx_bytes+=iSkb->data_len;
}
/*------------------------------------------------------------------------------------------------------------------------*
Timers
*------------------------------------------------------------------------------------------------------------------------*/
static void heartbeat_timer(unsigned long iData)
{
struct lnlb_struct *lnlb=(struct lnlb_struct *)iData;
write_lock(&(lnlb->node_table.lock));
convergence_run(lnlb);
write_unlock(&(lnlb->node_table.lock));
check_convergence_completed(lnlb); /* Only useful in the case we're the unique node of the cluster*/
mod_timer(&lnlb->heartbeat_timer,NEXT_HB_TIMER);
}
static void convergence_timeout_timer(unsigned long iData)
{
int i,found_dead=0,reschedule_timeout=0;
enum lnlb_node_status status;
struct lnlb_cluster_msg msg;
struct lnlb_struct *lnlb=(struct lnlb_struct *)iData;
write_lock(&(lnlb->node_table.lock));
for(i=0;i<lnlb->node_table.size;i++)
{
status=lnlb->node_table.entry[i].status;
if(status==LNLB_NS_OK)
continue;
if(status==LNLB_NS_WAITING_HEARTBEAT)
{
lnlb->node_table.entry[i].status=LNLB_NS_ASKED_RETRASM;
/* Ask retransmission to the missing node */
DBG("Asking retrasmission to node %X:%X:%X:%X:%X:%X\n",DUMPMAC(lnlb->node_table.entry[i].hw_addr));
INIT_CLUSTER_MSG(msg);
msg.type=LNLB_MSG_ASKRETRASM;
send_eth_msg(&msg,lnlb,lnlb->node_table.entry[i].hw_addr);
reschedule_timeout=1;
}
else
{
DBG("Node %X:%X:%X:%X:%X:%X is dead\n",DUMPMAC(lnlb->node_table.entry[i].hw_addr));
lnlb->node_table.entry[i].status=LNLB_NS_DEAD;
found_dead=1;
/* Advertise protocol modules that node is dead */
advertise_node_death(lnlb, lnlb->node_table.entry[i].hw_addr);
}
}
if(found_dead)
delete_dead_nodes_from_table(lnlb);
write_unlock(&(lnlb->node_table.lock));
if(reschedule_timeout)
mod_timer(&lnlb->timeout_timer,NEXT_TO_TIMER);
else
check_convergence_completed(lnlb);
}
static void advertise_node_death(struct lnlb_struct *iDev,unsigned char *iNodeAddr)
{
int n;
read_lock(&proto_modules_lock);
for(n=0;n<PROTO_MODULES_UBOUND;n++)
{
if(proto_modules[n]==NULL) continue;
(*(proto_modules[n]->node_died))(iDev,iNodeAddr);
}
read_unlock(&proto_modules_lock);
}
/* Retrieve the "weight" for current node (depending on the source user choosed). */
static void get_weight(struct lnlb_struct *iDev)
{
long long weight=0;
switch(iDev->weight_mode)
{
case WM_MANUAL:
{
weight=iDev->weight;
}
break; /* In case of manual weight setting, weight is directly feed into dev->weight upon userspace call */
case WM_LOADAVG:
{
weight=avenrun[0];
}
break;
case WM_LOADAVG5:
{
weight=avenrun[1];
}
break;
case WM_LOADAVG15:
{
weight=avenrun[2];
}
break;
case WM_MEM:
{
struct sysinfo si;
si_meminfo(&si);
weight=si.totalram - si.freeram;
weight*=LNLB_WEIGHT_MAX;
do_div(weight,si.totalram);
}
break;
}
if(weight<LNLB_WEIGHT_MIN)
weight=LNLB_WEIGHT_MIN;
else if(weight>LNLB_WEIGHT_MAX)
weight=LNLB_WEIGHT_MAX;
iDev->weight=(lnlb_weight_t)weight;
}
/* Start the convergence phase (if not already running). All nodes are set to WAITING_HEARTBEAT. Current weight is broadcasted to cluster
* Note: always called from softirq.
* Note: this function is NOT node_table safe. Caller must lock the table
*/
static int convergence_run(struct lnlb_struct *iDev)
{
int i;
struct lnlb_node_table_entry *node;
static long last_time=0;
int i_am_the_first_node=0;
if(iDev->converging)
return 1; /* Convergence is already running */
/* Avoid racing loops */
if(get_seconds()-last_time<1)
{
printk(KERN_WARNING DRV_NAME "Cluster converging too fast, skipping convergence\n");
return 1;
}
last_time=get_seconds();
iDev->converging=1;
get_weight(iDev);
iDev->locked_weight=iDev->weight;
/* Determine if i'm the first (non JOINING) node in list */
for(i=0;i<iDev->node_table.size;i++){
node=&iDev->node_table.entry[i];
if(node->status==LNLB_NS_JOINING) continue;
i_am_the_first_node=(compare_ether_addr(iDev->bind_dev->dev_addr , node->hw_addr)==0);
break;
}
for(i=0;i<iDev->node_table.size;i++){
node=&iDev->node_table.entry[i];
if(compare_ether_addr(iDev->bind_dev->dev_addr , node->hw_addr)==0){
/* It's me */
node->status=LNLB_NS_OK;
node->weight=iDev->locked_weight;
}
else if(node->status == LNLB_NS_JOINING){
/* Node has just joined, send him the conntrack status (only if i'm the first node of the table) */
node->status=LNLB_NS_WAITING_HEARTBEAT;
if(i_am_the_first_node && iDev->join_complete)
send_conntrack_status(iDev,node->hw_addr); //TODO manda ripetuta
}
else {
/* It's not me, nor a joining node */
node->status=LNLB_NS_WAITING_HEARTBEAT;
}
}
iDev->join_complete=1;
broadcast_my_weigth(iDev);
/* Start the timeout timer...all nodes must converge in CONVERGE_MTIMEOUT msec.
If not, ask missing nodes for a heartbeat duplicate (if still got no reply consider the node dead) */
mod_timer(&iDev->timeout_timer,NEXT_TO_TIMER);
return 0;
}
/*
* Handler for received broadcast messages
* iMsg: the received message
* iDev: the virtual lnlbN device which the message is related to
* Note: It's always executed from softirq (the frame hook)
* Note: msg will be freed after this call is ended. msg->data will *NOT* (so we must free it)
*/
static void cluster_msg_handler(struct lnlb_cluster_msg *iMsg,struct lnlb_struct *iDev)
{
struct lnlb_node_table_entry *node;
switch(iMsg->type)
{
case LNLB_MSG_HEARTBEAT:
case LNLB_MSG_RETRASM:
{
/* Heartbeat received from another node... update its weight in the node table */
write_lock(&(iDev->node_table.lock));
node=seek_node_from_table(iDev,iMsg->sender); /* Seek the sender from the node table */
DBG("Heartbeat received from %X:%X:%X:%X:%X:%X\n",DUMPMAC(iMsg->sender));
if(iMsg->len<sizeof(lnlb_weight_t))
{
/* Malformed packet? */
DBG("Malformed heartbeat received\n");
goto heartbeat_err;
}
if(node==NULL)
{
/* If node is not in the table... add it do the table...this should normally never happen since new nodes executes a repeated JOIN on load */
if((node=add_node_to_table(iDev,iMsg->sender))==NULL)
goto heartbeat_err;
}
else if(!iDev->converging && iMsg->type==LNLB_MSG_HEARTBEAT)
{
convergence_run(iDev);
/* Reschedule the heartbeat timer for next HEARTBEAT_INTERVAL SECONDS (so keep time synchronization between nodes) */
mod_timer(&iDev->heartbeat_timer,NEXT_HB_TIMER);
}
node->weight=ntohs(*((lnlb_weight_t *)iMsg->data));
node->status=LNLB_NS_OK;
write_unlock(&(iDev->node_table.lock));
check_convergence_completed(iDev);
break;
heartbeat_err:
write_unlock(&(iDev->node_table.lock));
}
break;
case LNLB_MSG_JOIN:
case LNLB_MSG_WELCOME:{
write_lock(&(iDev->node_table.lock));
node=seek_node_from_table(iDev,iMsg->sender);
if(node==NULL)
{
DBG("New node discovered: %X:%X:%X:%X:%X:%X\n",DUMPMAC(iMsg->sender));
if((node=add_node_to_table(iDev,iMsg->sender))==NULL)
goto jw_err;
}
write_unlock(&(iDev->node_table.lock));
if(iMsg->type==LNLB_MSG_WELCOME)
break;
/*... Continue here only in the JOIN case */
broadcast_welcome(iDev);
break;
jw_err:
write_unlock(&(iDev->node_table.lock));
}
break;
case LNLB_MSG_CONNTRACK: {
DBG("Received conntrack dump from %X:%X:%X:%X:%X:%X\n",DUMPMAC(iMsg->sender));
read_lock(&proto_modules_lock);
if(iMsg->flags > PROTO_MODULES_UBOUND)
goto ct_exit;
if(proto_modules[iMsg->flags]==NULL)
goto ct_exit;
if( (*(proto_modules[iMsg->flags]->load_status))(iDev,iMsg->data,iMsg->len))
DBG("Error while deserializing conntrack status\n");
ct_exit:
read_unlock(&proto_modules_lock);
}
break;
case LNLB_MSG_LEAVE:
{
DBG("Node is leaving the cluster %X:%X:%X:%X:%X:%X\n",DUMPMAC(iMsg->sender));
write_lock(&(iDev->node_table.lock));
node=seek_node_from_table(iDev,iMsg->sender); /* Seek the sender from the node table */
if(node==NULL) /* We received a "leave" from a node NOT in list... really strange... DROP! */
goto leave_exit;
node->status=LNLB_NS_DEAD;
advertise_node_death(iDev, iMsg->sender);
delete_dead_nodes_from_table(iDev);
leave_exit:
write_unlock(&(iDev->node_table.lock));
}
break;
case LNLB_MSG_ASKRETRASM:
{
retrasm_my_weight(iDev,iMsg->sender);
}
break;
default:
break;
}
/* switch(iMsg->type) { */
/* Free the data previously allocated by heartbeat_frame_hook */
if(iMsg->data)
kfree(iMsg->data);
}
/* Update the cut-table using by the hash based distribution algorithm
* Caller must lock only the dev_node table (the function locks only the cut_table)
*/
static inline void update_cut_table(struct lnlb_struct *iDev)
{
int i,n;
uint32_t weight_sum=0;
lnlb_weight_t tmp_weight;
write_lock(&(iDev->cut_table.lock));
iDev->cut_table.size=0;
iDev->cut_table_sum=0;
for(i=0;i<iDev->node_table.size;i++)
{
if(iDev->node_table.entry[i].weight==0) goto exit;
weight_sum+=iDev->node_table.entry[i].weight;
}
for(i=0,n=0;i<iDev->node_table.size;i++)
{
if(iDev->node_table.entry[i].status!=LNLB_NS_OK) continue;
memcpy(iDev->cut_table.entry[n].hw_addr,iDev->node_table.entry[i].hw_addr,ETH_ALEN);
tmp_weight=weight_sum * MAX_WEIGHT_MUL / iDev->node_table.entry[i].weight;
//BUG_ON(tmp_weight > LNLB_WEIGHT_MAX);
iDev->cut_table.entry[n].weight=tmp_weight;
iDev->cut_table_sum+=tmp_weight;
n++;
}
iDev->cut_table.size=n;
//BUG_ON(iDev->cut_table_sum==0);
if(iDev->cut_table_sum==0)
iDev->cut_table.size=0;
exit:
write_unlock(&(iDev->cut_table.lock));
}
/* Check if we've received heartbeats from all nodes
Note: called always in softirq */
static int check_convergence_completed(struct lnlb_struct *iDev)
{
int i;
int res=0;
read_lock(&(iDev->node_table.lock));
for(i=0;i<iDev->node_table.size;i++)
{
if(iDev->node_table.entry[i].status!=LNLB_NS_OK)
{
res=1;
break;
}
}
if(!res)
update_cut_table(iDev); /* Update the cut-table using by the hash based distribution algorithm */
read_unlock(&(iDev->node_table.lock));
if(res)
return 1;
/* We received heartbeats from all nodes (status is LNLB_NS_OK) ... convergence is ended */
/* Cancel the timeout check timer */
del_timer(&iDev->timeout_timer);
/* Process frames that have been queued in the meanwhile */
process_queued_skbuff(iDev);
iDev->converging=0;
//DBG(":::::::::::::Convergence ended:::::::::::\n");
//dump_table(iDev);
return res;
}
/* Process skbuffs that were queued during the convergence phase */
static void process_queued_skbuff(struct lnlb_struct *iDev)
{
struct sk_buff *skb=NULL;
while((skb=skb_dequeue(&iDev->rx_queue)))
{
if( ! skb_node_affinity(iDev,skb,0))
pass_skb_to_vdevice(skb,iDev); /* Skb is for this node */
else
dev_kfree_skb(skb); /* Skb is for another node */
}
}
/* Broadcast an ethernet message containing my weight to all nodes in the cluster
* Note: always called from softirq
*/
static int broadcast_my_weigth(struct lnlb_struct *iDev)
{
struct lnlb_cluster_msg msg;
lnlb_weight_t weight=htons(iDev->locked_weight);
INIT_CLUSTER_MSG(msg);
msg.type=LNLB_MSG_HEARTBEAT;
msg.len=sizeof(lnlb_weight_t);
msg.data=(char *)&weight;
return broadcast_msg(&msg,iDev);
}
static int retrasm_my_weight(struct lnlb_struct *iDev,unsigned char *iDestAddr)
{
struct lnlb_cluster_msg msg;
lnlb_weight_t weight=htons(iDev->locked_weight);
INIT_CLUSTER_MSG(msg);
msg.type=LNLB_MSG_RETRASM;
msg.len=sizeof(lnlb_weight_t);
msg.data=(char *)&weight;
return send_eth_msg(&msg,iDev,iDestAddr);
}
static int broadcast_join(struct lnlb_struct *iDev)
{
struct lnlb_cluster_msg msg;
INIT_CLUSTER_MSG(msg);
msg.type=LNLB_MSG_JOIN;
return broadcast_msg(&msg,iDev);
}
static int broadcast_welcome(struct lnlb_struct *iDev)
{
struct lnlb_cluster_msg msg;
INIT_CLUSTER_MSG(msg);
msg.type=LNLB_MSG_WELCOME;
return broadcast_msg(&msg,iDev);
}
static int send_conntrack_status(struct lnlb_struct *iDev,unsigned char *iDestAddr){
int i;
int ret=0;
struct lnlb_cluster_msg msg;
read_lock(&proto_modules_lock);
for(i=0;i<PROTO_MODULES_UBOUND;i++){
if(proto_modules[i]==NULL) continue;
DBG("Sending conntrack status\n");
INIT_CLUSTER_MSG(msg);
msg.type=LNLB_MSG_CONNTRACK;
msg.len=(*(proto_modules[i]->save_status))(iDev,&msg.data);
msg.flags=i;
if(!msg.len) continue;
ret=send_long_eth_msg(&msg,iDev,iDestAddr);
if(ret) break;
DBG("Sent conntrack status %u to %X:%X:%X:%X:%X:%X\n",msg.len,DUMPMAC(iDestAddr));
}
read_unlock(&proto_modules_lock);
return ret;
}
static int broadcast_leave(struct lnlb_struct *iDev)
{
struct lnlb_cluster_msg msg;
INIT_CLUSTER_MSG(msg);
msg.type=LNLB_MSG_LEAVE;
return broadcast_msg(&msg,iDev);
}
static int broadcast_conntrack_status(struct lnlb_struct *iDev)
{
return send_conntrack_status(iDev,iDev->cluster_mac);
}
static int send_long_eth_msg(struct lnlb_cluster_msg *iMsg,struct lnlb_struct *iDev,unsigned char *iDestAddr){
struct lnlb_cluster_msg frag_msg;
uint32_t free_data_size;
int res;
static uint8_t lastid=0;
free_data_size=iDev->dev->mtu
-ETH_HLEN
-sizeof(__be32) /* Cluster IP Address */
-sizeof(enum lnlb_msg_type) /* Message type */
-sizeof(uint32_t) /* Data length */
-sizeof(uint8_t) /* Message ID */
-sizeof(uint16_t) /* Fragment num */
-sizeof(uint16_t) /* Total fragments */
-sizeof(uint32_t) /* Global Message CRC32 */
;
if(iMsg->len < free_data_size)
return send_eth_msg(iMsg,iDev,iDestAddr);
frag_msg=*iMsg;
frag_msg.crc=crc32(0,iMsg->data,iMsg->len);
frag_msg.frag_total= iMsg->len / free_data_size + ((iMsg->len % free_data_size) ? 1 : 0);
frag_msg.id=lastid++;
for(frag_msg.frag_num=0;frag_msg.frag_num < frag_msg.frag_total; frag_msg.frag_num++){
frag_msg.len=MIN(free_data_size , iMsg->len - (frag_msg.frag_num * free_data_size));
res=send_eth_msg(&frag_msg,iDev,iDestAddr);
if(res) return res;
frag_msg.data+=frag_msg.len;
}
return 0;
}
static int send_eth_msg(struct lnlb_cluster_msg *iMsg,struct lnlb_struct *iDev,unsigned char *iDestAddr)
{
struct sk_buff *skb;
struct ethhdr *eth;
__be32 *cluster_ip;
enum lnlb_msg_type *msg_type;
uint32_t *data_len,*total_crc,*flags;
uint8_t *msg_id;
uint16_t *frag_num,*frag_total;
void *data;
skb=dev_alloc_skb(ETH_HLEN /* Ethernet header */
+sizeof(__be32) /* Cluster IP Address */
+sizeof(enum lnlb_msg_type) /* Message type */
+sizeof(uint32_t) /* Data length */
+sizeof(uint32_t) /* Flags */
+sizeof(uint8_t) /* Message ID */
+sizeof(uint16_t) /* Fragment num */
+sizeof(uint16_t) /* Total fragments */
+sizeof(uint32_t) /* Global Message CRC32 */
+iMsg->len /* Data */
);
if(skb==NULL)
{
DBG("Cannot allocate skbuff\n");
return -ENOMEM;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
skb_reset_mac_header(skb);
skb_reset_network_header(skb);
#else
skb->nh.raw = skb->mac.raw = skb->data;
#endif
skb->ip_summed = CHECKSUM_NONE;
skb->protocol = __constant_htons(ETH_P_LNLB);
skb->priority = 0;
skb->next = skb->prev = NULL;
skb->dev = iDev->bind_dev;
eth = (struct ethhdr *) skb_put(skb, ETH_HLEN);
memcpy(eth->h_source,iDev->bind_dev->dev_addr,ETH_ALEN);
memcpy(eth->h_dest,iDestAddr,ETH_ALEN);
eth->h_proto=__constant_htons(ETH_P_LNLB);
cluster_ip = (__be32 *) skb_put(skb, sizeof(__be32));
*cluster_ip = iDev->cluster_ip;
msg_type = (enum lnlb_msg_type *) skb_put(skb, sizeof(enum lnlb_msg_type));
*msg_type = htonl(iMsg->type);
data_len = (uint32_t *) skb_put(skb, sizeof(uint32_t));
*data_len = htonl(iMsg->len);
flags = (uint32_t *) skb_put(skb, sizeof(uint32_t));
*flags = htonl(iMsg->flags);
msg_id= (uint8_t *) skb_put(skb, sizeof(uint8_t));
*msg_id=iMsg->id;
frag_num=(uint16_t *) skb_put(skb, sizeof(uint16_t));
*frag_num=htons(iMsg->frag_num);
frag_total=(uint16_t *) skb_put(skb, sizeof(uint16_t));
*frag_total=htons(iMsg->frag_total);
total_crc=(uint32_t *) skb_put(skb, sizeof(uint32_t));
*total_crc=htonl(iMsg->crc);
if(*data_len>0)
{
data=skb_put(skb, iMsg->len);
memcpy(data,iMsg->data,iMsg->len);
}
dev_queue_xmit(skb);
return 0;
}
static inline void delete_and_free_frag_msg(struct lnlb_cluster_msg *iMsg){
list_del(&iMsg->list);
if(iMsg->len>0 && iMsg->data)
kfree(iMsg->data);
kfree(iMsg);
}
/* Hook function for ETH_P_LNLB frames (set via dev_add_pack) */
//TODO tutta da controllare
static int heartbeat_frame_hook(struct sk_buff *iSkb, struct net_device *iIfp, struct packet_type *iPt, struct net_device *iOrig_dev)
{
struct sk_buff *skb=NULL;
struct ethhdr *eth;
struct lnlb_struct *lnlb,*lptr,*lnxt;
unsigned char *ptr;
__be32 cluster_ip;
struct lnlb_cluster_msg *msg=NULL,*mptr,*mptrloop,*fullmsg;
struct list_head *addmptr;
uint32_t total_msg_len=0;
uint32_t msg_data_offset=0;
int32_t last_frag;
int drop=1;
/* Drop if not directed to me nor to cluster MAC */
list_for_each_entry_safe(lptr, lnxt , &dev_list, list){
if(compare_ether_addr(eth_hdr(iSkb)->h_dest,lptr->cluster_mac)==0) {drop=0; break;}
if(compare_ether_addr(eth_hdr(iSkb)->h_dest,lptr->bind_dev->dev_addr)==0) {drop=0; break;}
}
if(drop)
return 0;
/* Check the frame is valid */
if (!pskb_may_pull(iSkb,sizeof(__be32) /* Cluster IP Address */
+sizeof(enum lnlb_msg_type) /* Message type */
+sizeof(uint32_t) /* Data length */
+sizeof(uint32_t) /* Flags */
+sizeof(uint8_t) /* Message ID */
+sizeof(uint16_t) /* Fragment num */
+sizeof(uint16_t) /* Total fragments */
+sizeof(uint32_t) /* Global Message CRC32 */
))
return 0;
msg=(struct lnlb_cluster_msg*)kmalloc(sizeof(struct lnlb_cluster_msg),GFP_ATOMIC);
if(msg == NULL)
return 0;
skb = skb_share_check(iSkb, GFP_ATOMIC);
if (skb == NULL)
return 0;
msg->data=NULL;
msg->timestamp=get_seconds();
eth=eth_hdr(skb);
memcpy(msg->sender,eth->h_source,ETH_ALEN);
ptr=(char *)eth_hdr(skb)+ETH_HLEN;
cluster_ip=*((__be32 *)ptr);
ptr+=sizeof(__be32);
msg->type=ntohl(*((enum lnlb_msg_type *)ptr));
ptr+=sizeof(enum lnlb_msg_type);
msg->len=ntohl(*((uint32_t *)ptr));
ptr+=sizeof(uint32_t);
msg->flags=ntohl(*((uint32_t *)ptr));
ptr+=sizeof(uint32_t);
msg->id=*((uint8_t *)ptr);
ptr+=sizeof(uint8_t);
msg->frag_num=ntohs(*((uint16_t *)ptr));
ptr+=sizeof(uint16_t);
msg->frag_total=ntohs(*((uint16_t *)ptr));
ptr+=sizeof(uint16_t);
msg->crc=ntohl(*((uint32_t *)ptr));
ptr+=sizeof(uint32_t);
/* Copy the frame data from skb to msg structure */
if (!pskb_may_pull(skb,(ptr-((unsigned char *)eth_hdr(skb))-ETH_HLEN)+msg->len))
{ DBG("Malformed heartbeat frame");
goto exit;
}
if(msg->len > 0)
{
msg->data=kmalloc(msg->len,GFP_ATOMIC);
if(msg->data==NULL)
goto exit;
memcpy(msg->data,ptr,msg->len);
ptr+=msg->len;
}
lnlb=lnlbif_by_ip(cluster_ip);
if(lnlb==NULL) goto exit;
if(iIfp!=lnlb->bind_dev) goto exit; /*If the frame is received from another iface that is not bound to current VIP drop it*/
/* If it's a long message (more than 1 fragment), do not parse until all fragments are received */
dev_kfree_skb(skb);
skb=NULL;
if(msg->frag_total > 1){
spin_lock(&lnlb->frag_list_lock);
/* --- Add (ordered) to the frag_lis corresponding to msg->idt --- */
addmptr=&lnlb->frag_list[msg->id];
list_for_each_entry_safe(mptr, mptrloop, &lnlb->frag_list[msg->id], list){
/* check for expired fragments */
if(get_seconds() - msg->timestamp > FRAG_EXPIRE_INTERVAL){
delete_and_free_frag_msg(mptr);
continue;
}
if(msg->frag_num > mptr->frag_num) continue;
if(msg->frag_num == mptr->frag_num) {
addmptr=mptr->list.next;
delete_and_free_frag_msg(mptr);
}
else
addmptr=&mptr->list;
break;
}
list_add_tail(&msg->list,addmptr);
/* Check if all fragments has been received */
last_frag=-1;
total_msg_len=0;
list_for_each_entry(mptr, &lnlb->frag_list[msg->id], list){
if(last_frag+1 < mptr->frag_num)
break;
total_msg_len+=mptr->len;
last_frag++;
}
if(last_frag == msg->frag_total-1){
/*All fragments received */
fullmsg=(struct lnlb_cluster_msg *)kmalloc(sizeof(struct lnlb_cluster_msg),GFP_ATOMIC);
if(!fullmsg) {spin_unlock(&lnlb->frag_list_lock); return 0;}
INIT_CLUSTER_MSG(*fullmsg);
fullmsg->type=msg->type;
fullmsg->len=total_msg_len;
fullmsg->crc=msg->crc;
memcpy(fullmsg->sender,msg->sender,ETH_ALEN);
fullmsg->data=kmalloc(total_msg_len,GFP_ATOMIC);
if(!fullmsg->data) {
kfree(fullmsg);
spin_unlock(&lnlb->frag_list_lock);
return 0;
}
/* Fragment could mix at this point (e.g. if frag_total is 3 but we received for any reason fragment 1,2,3,4,5, all fragments will be parsed (the CRC check will drop it finally)*/
/* Melt all fragments in list in a single big message (and free the fragments) */
list_for_each_entry_safe(mptr, mptrloop, &lnlb->frag_list[msg->id], list){