-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlpxproto.c
More file actions
6393 lines (5428 loc) · 182 KB
/
lpxproto.c
File metadata and controls
6393 lines (5428 loc) · 182 KB
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
/**********************************************************************
* Copyright ( C ) 2012 IOCELL Networks
* All rights reserved.
**********************************************************************/
#include "linux_ver.h"
#include "inc/xplatcfg.h"
#include "inc/lpx/lpx.h"
#include "inc/lpx/lpxutil.h"
#include "inc/sal/sync.h" // sal_spinlock sal_sema
#include "inc/sal/debug.h" // sal_debug_print
#include "inc/sal/types.h"
#include "inc/xlib/dpc.h"
#include "inc/xlib/xbuf.h" // DEBUG_XLIB_XBUF
#include "inc/xlib/gtypes.h" // g_ntohl g_htonl g_ntohs g_htons
#include "inc/sal/net.h"
#include "inc/sal/mem.h"
#include "inc/sal/libc.h"
#include "inc/sal/time.h"
#include "inc/ndasuser/ndasuser.h"
#include "inc/ndasuser/ndaserr.h"
#include "inc/netdisk/list.h"
#ifdef DEBUG
#include "sal/debug.h" // sal_debug_print
#define debug_itf(l, x...) do { \
if ( l <= DEBUG_LEVEL_ITF ) { \
sal_debug_print("LX|%d|%s|",l,__FUNCTION__);\
sal_debug_println(x);\
}\
} while(0)
#define debug_lpx(l, x...) do { \
if ( l <= DEBUG_LEVEL_LPX ) { \
sal_debug_print("LX|%d|%s|",l,__FUNCTION__);\
sal_debug_println(x);\
}\
} while(0)
#define debug_rexmit(l, x...) do {\
if ( l <= DEBUG_LEVEL_LPX_REXMIT ) { \
sal_debug_print("LXRX|%d|%s|",l,__FUNCTION__);\
sal_debug_println(x);\
}\
} while(0)
#define debug_lpx_packet(l, x...) do {\
if(l <= DEBUG_LEVEL_LPX_PACKET ) {\
sal_debug_print("LXP|%d|%s|",l,__FUNCTION__);\
sal_debug_println(x);\
}\
} while(0)
#define debug_lpx_datagram(l, x...) do {\
if(l <= DEBUG_LEVEL_LPX_DATAGRAM ) {\
sal_debug_print("LXD|%d|%s|",l,__FUNCTION__);\
sal_debug_println(x);\
}\
} while(0)
#define debug_lpx_sio(l, x...) do {\
if(l <= DEBUG_LEVEL_LPX_SIO ) {\
sal_debug_print("LXS|%d|%s|",l,__FUNCTION__);\
sal_debug_println(x);\
}\
} while(0)
#define LPX_INLINE
#else // DEBUG
#define debug_itf(l, x...) do {} while(0)
#define debug_lpx(l, x...) do {} while(0)
#define debug_rexmit(l, x...) do {} while(0)
#define debug_lpx_packet(l, x...) do {} while(0)
#define debug_lpx_datagram(l, x...) do {} while(0)
#define debug_lpx_sio(l, x...) do {} while(0)
#define LPX_INLINE inline
#endif // DEBUG
#ifdef UCOSII
// UCOSII has tight resource restriction. Wait until destroying socket is completed destroyed before creating new.
#define XPLAT_OPT_THROTTLE_SOCK_CREATE 1
#endif
/*
* sio_notify_to_user should be called in bpc thread.
* Otherwise, dpc cause a dead lock if dpc call the direct i/o such as conn_connect
*/
#ifdef XPLAT_SIO
#ifndef XPLAT_BPC
#error nxpo-bpc should be enabled if nxpo-sio is enabled
#endif
/*
SIO control block creation/destroy
*/
static
inline
lpx_siocb_i *
_siocb_create(
struct lpx_sock *sk,
void *data,
lpx_sio_handler func,
void *arg,
int keep_notifying)
{
lpx_siocb_i *scb;
scb = sal_malloc(sizeof(lpx_siocb_i));
if ( !scb )
return NULL;
INIT_LIST_HEAD(&scb->node);
scb->func = func;
scb->user = arg;
scb->sock = sk;
scb->cb.filedes = sk->lpxfd;
scb->cb.buf = data;
//acb->cb.nbytes = size;
scb->cb.result = NDAS_OK; // unknown
scb->notify_bpc = DPC_INVALID_ID;
scb->timeout_timer = DPC_INVALID_ID;
scb->keep_notifying = keep_notifying;
// Creation reference
sal_atomic_set(&scb->refcnt, 1);
return scb;
}
inline
void
_siocb_inc_ref(
lpx_siocb_i *scb
){
sal_assert(sal_atomic_read(&scb->refcnt) >= 1);
sal_atomic_inc(&scb->refcnt);
}
static
void
_siocb_do_destroy(
lpx_siocb_i *scb
){
debug_lpx_sio(1, "scb=%p notify=%p",scb, scb->notify_bpc);
// BPC and DPC Must be cancelled before destruction.
if(scb->notify_bpc != DPC_INVALID_ID) {
#ifdef DEBUG
sal_assert(bpc_cancel(scb->notify_bpc) != NDAS_OK);
#endif
bpc_destroy(scb->notify_bpc);
scb->notify_bpc = DPC_INVALID_ID;
}
if(scb->timeout_timer != DPC_INVALID_ID) {
#ifdef DEBUG
sal_assert(dpc_cancel(scb->timeout_timer) != NDAS_OK);
#endif
dpc_destroy(scb->timeout_timer);
scb->timeout_timer = DPC_INVALID_ID;
}
sal_free(scb);
}
inline
void
_siocb_destroy(
lpx_siocb_i *scb
){
int lastref;
sal_assert(sal_atomic_read(&scb->refcnt) >= 1);
lastref = sal_atomic_dec_and_test(&scb->refcnt);
if(lastref) {
_siocb_do_destroy(scb);
}
}
#endif
/*
Local variables
*/
#define LPX_SOCKET_HASH_SIZE 11
LOCAL lpx_interface *lpx_interfaces = NULL;
LOCAL struct lpx_portinfo lpx_port_binder;
LOCAL struct lpx_eth_cache lpx_dest_cache;
LOCAL sal_spinlock lpx_socks_lock; /* Note accessed from interrupt context. Use spinlock */
LOCAL struct list_head lpx_socks[LPX_SOCKET_HASH_SIZE];
LOCAL sal_atomic lpx_sock_count = sal_atomic_init(0);
LOCAL int next_lpx_sock_fd = 1;
LOCAL int lpx_max_transfer_size; /* in bytes */
#ifdef XPLAT_OPT_THROTTLE_SOCK_CREATE
LOCAL int v_destroy_pending_count=0;
#endif
#define lpxitf_mss(sk) ((LPX_OPT(sk)->interface)?LPX_OPT(sk)->interface->mss:1514)
/*
Forward declarations
*/
LOCAL ndas_error_t lpxitf_destroy(lpx_interface *interface);
LOCAL void lpx_bind_table_init(void);
LOCAL void lpx_dest_cache_init(void);
LOCAL void lpxitf_insert(lpx_interface *interface);
LOCAL LPX_INLINE void lpxitf_demux_sock(lpx_interface *interface, struct xbuf *xb);
LOCAL ndas_error_t dgm_transmit_packet(struct lpx_sock *sk, struct sockaddr_lpx *usaddr, char* data, int len);
//LOCAL void lpx_link_status_change_handler(sal_netdev_desc nd, int link_status);
NDAS_CALL LOCAL void lpx_net_rx_handler(sal_netdev_desc nd, sal_net_buff nbuf, sal_net_buf_seg* bufs);
//LOCAL void lpxitf_up(lpx_interface *interface);
//LOCAL void lpxitf_down(lpx_interface *interface);
LOCAL void lpxitf_remove(lpx_interface *interface);
LOCAL LPX_INLINE void lpxitf_rcv(struct xbuf *xb, sal_netdev_desc nd);
LOCAL void lpx_sock_queue_rcv_xbuf(struct lpx_sock* sk, struct xbuf* xb);
LOCAL void lpx_sock_state_change(struct lpx_sock *sk);
LOCAL void lpx_sock_data_ready(struct lpx_sock* sk, int len);
LOCAL void lpx_sock_destroy(struct lpx_sock* sk);
/* LPX stream functions */
LOCAL LPX_INLINE void stm_hold(struct lpx_sock *sk);
LOCAL LPX_INLINE void stm_release(struct lpx_sock *sk, int creationref);
/* LPX datagram functions */
LOCAL LPX_INLINE void dgm_hold(struct lpx_sock *sk);
LOCAL LPX_INLINE void dgm_release(struct lpx_sock *sk, int creationref);
/* dest cache function */
LOCAL struct lpx_cache * lpx_find_dest_cache(struct lpx_sock *sk);
LOCAL void lpx_update_dest_cache(struct lpx_sock * sk);
LOCAL int lpx_dest_cache_timer(void* p1, void* p2);
/* port-binding related function */
LOCAL void lpx_bind_table_init(void);
LOCAL void lpx_bind_table_down(void);
LOCAL void lpx_dest_cache_init(void);
LOCAL void lpx_dest_cache_down(void);
LOCAL void lpx_dest_cache_disable(unsigned char * node);
LOCAL struct lpx_sock * lpx_find_port_bind(unsigned short port, int inc_refcnt);
LOCAL unsigned short lpx_get_free_port_num(void);
LOCAL void lpx_reg_bind(struct lpx_sock * sk);
LOCAL void lpx_unreg_bind(struct lpx_sock * sk);
#define lpx_bind_hash(port) ((port)%PORT_HASH)
/* dgram functions */
LOCAL void dgm_rcv(struct lpx_sock *sk, struct xbuf *xb);
LOCAL ndas_error_t dgm_create(struct lpx_sock *sk, int protocol);
/* stream functions */
LOCAL LPX_INLINE void stm_rcv(struct lpx_sock *sk, struct xbuf *xb);
LOCAL ndas_error_t stm_create(struct lpx_sock *sk, int protocol);
LOCAL LPX_INLINE ndas_error_t stm_do_rcv(struct lpx_sock* sk, struct xbuf *xb);
LOCAL void stm_destroy_sock(struct lpx_sock *sk);
LOCAL int lpx_stream_snd_test(struct lpx_sock *sk, struct xbuf* xb);
/* Misc functoins */
LOCAL int lpx_is_valid_addr(struct lpx_sock * sk,unsigned char * node);
/**************************
xbuf related functions
*************************/
/*
* len: length of lpx data (excludes lpx_header & mac header
* Assume sk is locked
*/
LOCAL inline struct xbuf* lpx_sock_alloc_xb(struct lpx_sock *sk, int data_len, int noblock)
{
struct xbuf* xb;
#ifdef USE_XBUF_FROM_NET_BUF_AT_TX
{
sal_net_buff nbuf;
sal_net_buf_seg segbuf;
nbuf = sal_net_alloc_platform_buf_ex(sk->system_sock, LPX_HEADER_SIZE, data_len, &segbuf, noblock);
if ( !nbuf ) {
debug_lpx(1,"sal_net_alloc_platform_buf_ex() data_len=%d noblock=%d failed.", data_len, noblock);
return NULL;
}
xb = xbuf_alloc_from_net_buf(nbuf, &segbuf);
if ( !xb ) {
sal_net_free_platform_buf(nbuf);
debug_lpx(1,"xbuf_alloc_from_net_buf() failed.");
return NULL;
}
}
sal_assert(xb->sys_buf);
#else
xb = xbuf_alloc(data_len + LPX_HEADER_SIZE);
if ( !xb ) {
debug_lpx(1,"xbuf_alloc() failed.");
return NULL;
}
xbuf_reserve_header(xb, LPX_HEADER_SIZE);
#endif
xb->owner = (void*)sk;
sal_assert(xb->owner);
return xb;
}
LOCAL inline
ndas_error_t
lpx_sock_rebuild_xb_netbuf(struct lpx_sock *sk, struct xbuf *xb, int noblock)
{
#ifdef USE_XBUF_FROM_NET_BUF_AT_TX
{
sal_net_buff nbuf;
sal_net_buf_seg segbuf;
int ret;
sal_assert(xb->sys_buf == NULL);
ret = xbuf_get_netbuf_seg(xb, &segbuf);
if(ret < 0) {
return NDAS_ERROR;
}
// Allocate system network buffer with net buf segments.
// The function will return network header pointers.
nbuf = sal_net_alloc_buf_bufseg(
sk->system_sock,
&segbuf);
if ( !nbuf ) {
debug_lpx(1,"sal_net_alloc_buf_bufseg() failed.");
return NDAS_ERROR;
}
// Set network header pointers
xb->mach = segbuf.mac_header.ptr;
xb->nh = segbuf.network_header.ptr;
xb->sys_buf = nbuf;
}
#endif
return NDAS_OK;
}
/**************************
lpx_sock related functions
***************************/
LOCAL inline
int lpx_hash_sockfd(int sockfd) {
return (sockfd % LPX_SOCKET_HASH_SIZE);
}
LOCAL struct lpx_sock* lpx_get_sock_from_fd(int fd)
{
struct list_head *i;
sal_spinlock_take_softirq(lpx_socks_lock);
list_for_each(i, &lpx_socks[lpx_hash_sockfd(fd)])
{
struct lpx_sock *sk = list_entry(i,struct lpx_sock, all_node);
sal_assert(sk);
if (sk->lpxfd == fd) {
sal_spinlock_give_softirq(lpx_socks_lock);
return sk;
}
}
sal_spinlock_give_softirq(lpx_socks_lock);
debug_lpx(1, "Invalid lpx fd: %d", fd);
return NULL;
}
LOCAL inline void lock_sock(struct lpx_sock* sk, unsigned long* flags)
{
*flags = 0;
sal_spinlock_take_softirq(sk->lock);
}
LOCAL inline void unlock_sock(struct lpx_sock* sk, unsigned long* flags)
{
sal_spinlock_give_softirq(sk->lock);
}
LOCAL ndas_error_t lpx_sock_error(struct lpx_sock* sk)
{
ndas_error_t err;
err = sk->err;
sk->err = NDAS_OK;
return err;
}
static inline void lpx_sock_inc_ref(struct lpx_sock* sk)
{
sal_atomic_inc(&sk->refcnt);
}
/* Return 1 if decreased value is 0 */
static inline int lpx_sock_dec_ref(struct lpx_sock* sk)
{
return sal_atomic_dec_and_test(&sk->refcnt);
}
static inline int lpx_sock_read_ref(struct lpx_sock* sk)
{
return sal_atomic_read(&sk->refcnt);
}
/************************
lpx protocol
**************************/
#ifdef STAT_REXMIT
#define REXMIT_INTERVAL (30 * SAL_TICKS_PER_SEC)
static int rexmit_bad_count = 0;
static int rexmit_good_count = 0;
LOCAL int lpx_rexmit_stat(void *a, void *b)
{
dpc_id dpcid;
int ret;
sal_error_print("rexmit stat:%d/%d\n", rexmit_bad_count, rexmit_good_count);
dpcid = dpc_create(DPC_PRIO_NORMAL, lpx_rexmit_stat, NULL, NULL, NULL, 0);
if(dpcid) {
ret = dpc_queue(dpcid, REXMIT_INTERVAL);
if(ret < 0) {
dpc_destroy(dpcid);
}
return 0;
}
#endif
LOCAL ndas_error_t lpx_proto_init(void)
{
#ifdef STAT_REXMIT
dpc_id dpcid;
int ret;
#endif
lpx_bind_table_init();
lpx_dest_cache_init();
#ifdef STAT_REXMIT
dpcid = dpc_create(DPC_PRIO_NORMAL, lpx_rexmit_stat, NULL, NULL, NULL, 0);
if(dpcid) {
ret = dpc_queue(dpcid, REXMIT_INTERVAL);
if(ret < 0) {
dpc_destroy(dpcid);
}
}
#endif
return NDAS_OK;
}
LOCAL void lpx_proto_finito(void)
{
lpx_interface *ifc;
lpx_interface *next_ifc;
debug_lpx(3, "ing");
next_ifc = lpx_interfaces;
while(next_ifc) {
ifc = next_ifc;
next_ifc = ifc->itf_next;
lpxitf_destroy(ifc);
}
lpx_dest_cache_down();
lpx_bind_table_down();
debug_lpx(3, "ed");
return;
}
LOCAL inline lpx_interface *lpxitf_find_using_devname(const char* dev_name)
{
lpx_interface *i;
for(i = lpx_interfaces; i && (sal_strcmp(dev_name, i->dev)!=0); i = i->itf_next);
return i;
}
LOCAL ndas_error_t lpxitf_create(const char *dev_name)
{
lpx_interface *itf;
sal_netdev_desc nd;
int ret;
debug_lpx(3, "ing dev_name=%s", dev_name);
if(lpxitf_find_using_devname(dev_name) != NULL)
return NDAS_ERROR_EXIST;
nd = sal_netdev_open(dev_name);
if (nd == SAL_INVALID_NET_DESC) {
/* sal_debug_print("Failed open device %s\r\n", dev_name); */
return NDAS_ERROR_NO_DEVICE;
}
itf = (lpx_interface *)sal_malloc(sizeof(lpx_interface));
if(itf == NULL) {
ret = NDAS_ERROR_OUT_OF_MEMORY;
goto out1;
}
itf->nd = nd;
itf->itf_sklist = NULL;
ret = sal_spinlock_create("itf-sk-lock", &itf->itf_sklock);
if (!ret) {
ret = NDAS_ERROR_OUT_OF_MEMORY;
goto out2;
}
sal_strcpy(itf->dev, dev_name);
sal_netdev_get_address(nd, itf->itf_node);
itf->mss = sal_netdev_get_mtu(nd) - sizeof(struct lpxhdr);
sal_assert(itf->mss + sizeof(struct lpxhdr)>= MIN_MTU_SIZE); /* We assume MTU is large enough to hold two disk sector */
itf->hard_header_len = SAL_ETHER_HEADER_SIZE;
lpxitf_insert(itf);
ret = sal_netdev_register_proto(nd, g_htons(ETH_P_LPX), lpx_net_rx_handler);
if (ret != 0) {
sal_debug_print("sal_netdev_register_proto failed");
ret = NDAS_ERROR_PROTO_REGISTRATION_FAIL;
goto out3;
}
return NDAS_OK;
out3:
lpxitf_remove(itf);
out2:
sal_free(itf);
out1:
sal_netdev_close(nd);
return ret;
}
/**
* lpxitf_destroy - destroy the interface for lpx protocol.
*/
LOCAL ndas_error_t lpxitf_destroy(lpx_interface *interface)
{
struct lpx_sock *s;
ndas_error_t ret = NDAS_OK;
unsigned long flags;
debug_lpx(1, "ing interface=%p", interface);
sal_netdev_unregister_proto(interface->nd, g_htons(ETH_P_LPX));// TODO error handling
lpx_dest_cache_disable(interface->itf_node);
lpxitf_remove(interface);
for(s = interface->itf_sklist; s != NULL; s=s->next) {
lock_sock(s, &flags);
LPX_OPT(s)->interface = NULL;
unlock_sock(s, &flags);
}
sal_netdev_close(interface->nd);
sal_spinlock_destroy(interface->itf_sklock);
sal_free(interface);
debug_lpx(2, "ed");
return ret;;
}
LOCAL void lpx_dest_cache_init(void)
{
int ret;
debug_lpx(5, "lpx_dest_cache_init");
sal_spinlock_create("cache_lock", &lpx_dest_cache.cache_lock);
lpx_dest_cache.head = NULL;
lpx_dest_cache.cache_timer = dpc_create(
DPC_PRIO_NORMAL,
lpx_dest_cache_timer,
(void*)&lpx_dest_cache,
NULL,
NULL,
DPCFLAG_DO_NOT_FREE);
if(lpx_dest_cache.cache_timer == DPC_INVALID_ID) {
sal_error_print("ndas: failed to create cache timer.\n");
return;
}
ret = dpc_queue(lpx_dest_cache.cache_timer, CACHE_TIMEOUT);
if(ret < 0) {
sal_error_print("ndas: failed to queue cache timer.\n");
return;
}
}
void
lpx_dest_cache_down(void)
{
struct lpx_cache *cache, *p_tmp;
debug_lpx(4, "lpx_dest_cache_down");
// Stop the periodic timer
dpc_cancel(lpx_dest_cache.cache_timer);
dpc_destroy(lpx_dest_cache.cache_timer);
lpx_dest_cache.cache_timer = DPC_INVALID_ID;
sal_spinlock_take_softirq(lpx_dest_cache.cache_lock);
cache = lpx_dest_cache.head;
while(cache)
{
p_tmp = cache;
cache = cache->next;
sal_free(p_tmp);
}
lpx_dest_cache.head = NULL;
sal_spinlock_give_softirq(lpx_dest_cache.cache_lock);
sal_spinlock_destroy(lpx_dest_cache.cache_lock);
}
LOCAL inline lpx_interface *lpxitf_find_using_netdesc(sal_netdev_desc nd)
{
lpx_interface *i;
for(i = lpx_interfaces; i && (i->nd!=nd); i = i->itf_next);
return i;
}
#ifdef XPLAT_NDASHIX
/* For UUID generation */
sal_netdev_desc lpxitf_find_first() {
return lpx_interfaces ? lpx_interfaces->nd : (sal_netdev_desc)NULL;
}
int lpxitf_iterate_address(void (*func)(xuchar*,void*), void* arg)
{
lpx_interface *i;
int count = 0;
debug_lpx(1, "ing func=%p", func);
for(i = lpx_interfaces; i ; i = i->itf_next) {
func(i->itf_node,arg);
count ++;
}
return count;
}
#endif
/**
* insert the interface into list
*/
LOCAL void lpxitf_insert(lpx_interface *interface)
{
lpx_interface *i;
debug_lpx(4, "lpxitf_insert");
interface->itf_next = NULL;
if(lpx_interfaces == NULL)
lpx_interfaces = interface;
else {
for(i = lpx_interfaces; i->itf_next != NULL; i = i->itf_next);
i->itf_next = interface;
}
return;
}
/**
* remove interface from the list
*/
LOCAL void lpxitf_remove(lpx_interface *interface)
{
lpx_interface *i;
if(interface == lpx_interfaces)
lpx_interfaces = interface->itf_next;
else {
for(i = lpx_interfaces;
(i != NULL) && (i->itf_next != interface);
i = i->itf_next);
if((i != NULL) && (i->itf_next == interface))
i->itf_next = interface->itf_next;
}
}
/*
Can be run in interrupt context. Do not sleep.
*/
NDAS_CALL LOCAL void lpx_net_rx_handler(sal_netdev_desc nd, sal_net_buff nbuf, sal_net_buf_seg* bufs)
{
struct xbuf* xb;
struct lpxhdr *lpxhdr;
int lpxsize;
debug_lpx(6, "nd=%p nbuf=%p bufs=%p", nd, (void*) nbuf, bufs);
if(bufs->data_len < LPX_HEADER_SIZE) {
sal_net_free_platform_buf(nbuf);
debug_lpx(1, "too small payload %d", bufs->data_len);
return;
}
sal_assert(bufs->nr_data_segs == 1);
if (bufs->network_header.len == 0) {
/* setup network ( LPX ) part */
bufs->network_header.len = LPX_HEADER_SIZE;
bufs->network_header.ptr = bufs->data_segs[0].ptr;
sal_assert(bufs->data_segs[0].len >= LPX_HEADER_SIZE);
bufs->data_segs[0].ptr = (xpointer)(((char*)bufs->data_segs[0].ptr) + LPX_HEADER_SIZE);
bufs->data_segs[0].len -= LPX_HEADER_SIZE;
bufs->data_len -= LPX_HEADER_SIZE;
}
// LPX packet length validation
lpxhdr = (struct lpxhdr *)bufs->network_header.ptr;
debug_itf(6, "lpxhdr=%p",lpxhdr);
debug_itf(8, "lpxhdr=%s",DEBUG_LPX_HDR(lpxhdr));
lpxsize = g_ntohs(lpxhdr->pu.pktsize & ~LPX_TYPE_MASK);
if(lpxsize < LPX_HEADER_SIZE ||
lpxsize > LPX_HEADER_SIZE + bufs->data_segs[0].len)
{
sal_net_free_platform_buf(nbuf);
debug_itf(1, "lpx size is incorrect, lpxhdr = %d pkt = %d payload len = %d",
(int)LPX_HEADER_SIZE, lpxsize, (int)bufs->data_segs[0].len);
return;
}
// cut off payload padding of ethernet drivers.
bufs->data_segs[0].len = lpxsize - LPX_HEADER_SIZE;
xb = xbuf_alloc_from_net_buf(nbuf,bufs);
if (xb) {
debug_itf(6, "From %s:%4X To: %s:%4X",
SAL_DEBUG_HEXDUMP_S(XBUF_MAC_HEADER(xb)->src, LPX_NODE_LEN),
g_ntohs(lpxhdr->source_port),
SAL_DEBUG_HEXDUMP_S(XBUF_MAC_HEADER(xb)->des, LPX_NODE_LEN),
g_ntohs(lpxhdr->dest_port)
);
lpxitf_rcv(xb, nd);
} else {
sal_net_free_platform_buf(nbuf);
debug_lpx(1, "Failed to allocate xbuf.");
}
debug_lpx(6, "ed");
}
LOCAL lpx_interface *lpxitf_find_using_node(unsigned char *node)
{
lpx_interface *i;
unsigned char zero_node[LPX_NODE_LEN] = {0, 0, 0, 0, 0, 0};
for(i = lpx_interfaces; i && sal_memcmp(i->itf_node, node, LPX_NODE_LEN); i = i->itf_next);
if(i == NULL && sal_memcmp(zero_node, node, LPX_NODE_LEN) == 0) {
i = lpx_interfaces;
}
return i;
}
LOCAL void lpxitf_insert_sock(lpx_interface *interface, struct lpx_sock *sk)
{
struct lpx_sock *s;
debug_lpx(4, "lpxitf_insert_sock");
LPX_OPT(sk)->interface = interface;
sk->next = NULL;
sal_spinlock_take_softirq(interface->itf_sklock);
if(interface->itf_sklist == NULL) {
interface->itf_sklist = sk;
} else {
for (s = interface->itf_sklist; s->next != NULL; s = s->next);
s->next = sk;
}
sal_spinlock_give_softirq(interface->itf_sklock);
sk->zapped = 0;
return;
}
LOCAL void lpxitf_remove_sock(struct lpx_sock *sk)
{
struct lpx_sock *s;
lpx_interface *interface;
debug_itf(2, "ing sk=%p",sk);
/* Unbind the socket from the port number */
lpx_unreg_bind(sk);
interface = LPX_OPT(sk)->interface;
LPX_OPT(sk)->interface = NULL;
sk->zapped = 1;
if(interface == NULL) {
debug_itf(4, "ed interface is destroyed");
return;
}
debug_itf(4, "interface=%p", interface);
sal_spinlock_take_softirq(interface->itf_sklock);
s = interface->itf_sklist;
/* If the socket is placed in the head. */
if(s == sk) {
interface->itf_sklist = s->next;
sal_spinlock_give_softirq(interface->itf_sklock);
return;
}
/* Loop until found */
while(s && s->next) {
if(s->next == sk) {
s->next = sk->next;
debug_itf(4, "ed s=%p",s);
sal_spinlock_give_softirq(interface->itf_sklock);
return;
}
s = s->next;
}
sal_spinlock_give_softirq(interface->itf_sklock);
debug_itf(2, "ed no match");
sal_assert(0); // fix me
}
LOCAL int lpx_dest_cache_timer(void* data, void* p2)
{
struct lpx_cache * cache, *p_cache;
sal_tick cur_time = sal_get_tick();
int ret;
debug_lpx(6,"lpx_dest_cache_timer");
sal_spinlock_take_softirq(lpx_dest_cache.cache_lock);
cache = lpx_dest_cache.head ;
p_cache = NULL;
while(cache)
{
if( sal_tick_sub(cur_time, cache->time) > CACHE_ALIVE )
{
if(NULL == p_cache) {
lpx_dest_cache.head = cache->next;
sal_free(cache);
cache = lpx_dest_cache.head;
}
else {
p_cache->next = cache->next;
sal_free(cache);
cache = p_cache->next;
}
continue;
}
p_cache = cache;
cache = cache->next;
}
sal_spinlock_give_softirq(lpx_dest_cache.cache_lock);
// Queue the DPC again.
ret = dpc_queue(lpx_dest_cache.cache_timer, CACHE_TIMEOUT);
if(ret < 0) {
sal_error_print("ndas: failed to queue cache timer\n");
}
return 0;
}
// check validation of portnumber and seach interface
LOCAL int lpx_is_valid_addr(struct lpx_sock * sk, unsigned char * node)
{
lpx_interface * i;
unsigned char zero_node[LPX_NODE_LEN] = {0,0,0,0,0,0};
debug_lpx(5, "lpx_is_valid_addr");
if (sal_memcmp(zero_node, node, LPX_NODE_LEN) == 0) {
//
// Zero node address means any interface.
// Check this before iterating interface because some uninitialized interface has zero address
//
LPX_OPT(sk)->virtual_mapping = 1;
LPX_OPT(sk)->interface = NULL;
sal_memcpy(LPX_OPT(sk)->source_addr.node, zero_node, LPX_NODE_LEN);
return 1;
}
for(i = lpx_interfaces; i && sal_memcmp(i->itf_node,node,LPX_NODE_LEN) ; i = i->itf_next);
if(i){
LPX_OPT(sk)->interface = i;
sal_memcpy(LPX_OPT(sk)->source_addr.node,i->itf_node, LPX_NODE_LEN);
return 1;
} else {
return 0;
}
}
/*--------------------------------------------------
Port number management
--------------------------------------------------*/
LOCAL void lpx_bind_table_init(void)
{
int i;
debug_lpx(5, "lpx_bind_table_init");
lpx_port_binder.last_alloc_port = 0;
sal_spinlock_create("bind_lock", &lpx_port_binder.bind_lock );
for(i = 0; i < PORT_HASH ; i++)
{
lpx_port_binder.port_hashtable[i].bind_next = NULL;
}
}
LOCAL void lpx_bind_table_down(void)
{
sal_spinlock_destroy(lpx_port_binder.bind_lock);
}
/* return socket binded with port */
LOCAL struct lpx_sock * lpx_find_port_bind( unsigned short port, int inc_refcnt)
{
struct lpx_bindhead * head;
struct lpx_sock * bind_sk = NULL;
int found = 0;
debug_lpx(9, "lpx_find_port_bind:port=%d", port);
//
// Assuming that a thread in hardware interrupt context never come to LPX.
//
sal_spinlock_take_softirq(lpx_port_binder.bind_lock);
head = &lpx_port_binder.port_hashtable[lpx_bind_hash(port)];
for(bind_sk = head->bind_next; bind_sk; bind_sk = bind_sk->bind_next) {
if(LPX_OPT(bind_sk)->source_addr.port == port) {
found = 1;
break;
}
}
// Increase the socket reference count if needed
if(found)
{
if(inc_refcnt) {
switch(bind_sk->type)
{
case LPX_TYPE_STREAM:
stm_hold(bind_sk);
break;
case LPX_TYPE_DATAGRAM:
dgm_hold(bind_sk);
break;
case LPX_TYPE_RAW:
sal_assert(0);
break;
default:
sal_assert(0);
break;
}
}
} else {
bind_sk = NULL;
}
sal_spinlock_give_softirq(lpx_port_binder.bind_lock);
debug_lpx(9, "lpx_find_port_bind:bound =%p", bind_sk);
return bind_sk;
}
// return port number is free
LOCAL unsigned short lpx_get_free_port_num(void)
{
unsigned int low = LPX_MIN_EPHEMERAL_SOCKET;
unsigned int high = LPX_MAX_EPHEMERAL_SOCKET;
unsigned int remaining = (high - low) +1 ;
unsigned int port;
unsigned int port_exists;
struct lpx_bindhead * head;
struct lpx_sock * bind_sk;
int ret = 0;
debug_lpx(5, "lpx_get_free_port_num");
sal_spinlock_take_softirq(lpx_port_binder.bind_lock);
port = lpx_port_binder.last_alloc_port;
// search availabe port number
do{
port++;
if ((port < low) || (port > high)) port = low;
head = &lpx_port_binder.port_hashtable[lpx_bind_hash(port)];
port_exists = FALSE;
for(bind_sk = head->bind_next; bind_sk; bind_sk = bind_sk->bind_next) {
if(LPX_OPT(bind_sk)->source_addr.port == port) {
port_exists = TRUE;
break;
}
}
if (!port_exists) { // proper port found
lpx_port_binder.last_alloc_port = port;
ret = port;
break;
}
} while ( --remaining > 0);
sal_spinlock_give_softirq(lpx_port_binder.bind_lock);