-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathofc_cntrl.c
1775 lines (1562 loc) · 59 KB
/
ofc_cntrl.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
/********************************************************************
*
* Filename: ofc_cntrl.c
*
* Description: This file contains the OpenFlow control path sub
* module. It interacts with SDN controller and
* exchanges OpenFlow control packets with the controller.
* It also interfaces with OpenFlow control path task
* to interact with Flow Classification application.
*
*******************************************************************/
#include "ofc_hdrs.h"
tOfcCpGlobals gOfcCpGlobals;
extern unsigned int gCntrlIpAddr;
extern char *gpOpenFlowIf[OFC_MAX_OF_IF_NUM];
extern int gNumOpenFlowIf;
/******************************************************************
* Function: OfcCpMainInit
*
* Description: This function performs the initialization tasks of
* the control path sub module
*
* Input: None
*
* Output: None
*
* Returns: OFC_SUCCESS/OFC_FAILURE
*
*******************************************************************/
int OfcCpMainInit (void)
{
memset (&gOfcCpGlobals, 0, sizeof (gOfcCpGlobals));
/* Initialize semaphore */
sema_init (&gOfcCpGlobals.semId, 1);
sema_init (&gOfcCpGlobals.dpMsgQSemId, 1);
/* Initialize queues */
INIT_LIST_HEAD (&gOfcCpGlobals.dpMsgListHead);
/* Create TCP socket to interact with controller */
if (OfcCpCreateCntrlSocket() != OFC_SUCCESS)
{
printk (KERN_CRIT "Control socket creation failed!!\r\n");
return OFC_FAILURE;
}
gOfcCpGlobals.isModInit = OFC_TRUE;
return OFC_SUCCESS;
}
/******************************************************************
* Function: OfcCpMainTask
*
* Description: Main function for OpenFlow control path task
*
* Input: None
*
* Output: None
*
* Returns: OFC_SUCCESS/OFC_FAILURE
*
*******************************************************************/
int OfcCpMainTask (void *args)
{
int event = 0;
/* Initialize memory and structures */
if (OfcCpMainInit() != OFC_SUCCESS)
{
/* Check if thread needs to be killed */
printk (KERN_CRIT "OpenFlow control path task intialization"
" failed!!\r\n");
return OFC_FAILURE;
}
while (1)
{
if (OfcCpReceiveEvent (OFC_CTRL_PKT_EVENT | OFC_DP_TO_CP_EVENT,
&event) == OFC_SUCCESS)
{
if (event & OFC_CTRL_PKT_EVENT)
{
/* Receive packet from controller */
OfcCpRxControlPacket();
}
if (event & OFC_DP_TO_CP_EVENT)
{
/* Process information sent by data path task */
OfcCpRxDataPathMsg();
}
}
}
return OFC_SUCCESS;
}
/******************************************************************
* Function: OfcCpRxControlPacket
*
* Description: This function receives OpenFlow control packets
* from SDN controller and processes these packets
*
* Input: None
*
* Output: None
*
* Returns: OFC_SUCCESS/OFC_FAILURE
*
*******************************************************************/
int OfcCpRxControlPacket (void)
{
tOfcOfHdr *pOfHdr = NULL;
__u8 *pCntrlPkt = NULL;
__u8 *pPkt = NULL;
__u16 pktLen = 0;
__u16 cntrlPktLen = 0;
__u16 bytesProcessed = 0;
int retVal = OFC_SUCCESS;
if (OfcCpRecvCntrlPktOnSock (&pPkt, &pktLen)
!= OFC_SUCCESS)
{
printk (KERN_CRIT "Failed to read control packet from"
" socket\r\n");
return OFC_FAILURE;
}
printk (KERN_INFO "Control packet received (pktLen:%d)\r\n", pktLen);
/* On each read call, socket returns all the control messages
* present in the socket queue. Therefore, the packets need
* need to be separated and processed sequentially */
pCntrlPkt = pPkt;
while (bytesProcessed < pktLen)
{
pOfHdr = (tOfcOfHdr *) ((void *) pCntrlPkt);
cntrlPktLen = ntohs (pOfHdr->length);
printk (KERN_INFO "Processing control packet (bytesProcessed:%d,"
" cntrlPktLen:%d)\r\n", bytesProcessed, cntrlPktLen);
/* Validate OpenFlow version */
if (pOfHdr->version != OFC_VERSION)
{
/* OpenFlow version mismatch allowed only in
* Hello message */
if (pOfHdr->type != OFPT_HELLO)
{
printk (KERN_CRIT "OpenFlow version mismatch!!\r\n");
/* TODO: Send error message and handle for higher versions */
bytesProcessed += cntrlPktLen;
pCntrlPkt += cntrlPktLen;
retVal = OFC_FAILURE;
continue;
}
}
/* Validate packet type */
if (pOfHdr->type >= OFPT_MAX_PKT_TYPE)
{
printk (KERN_CRIT "Invalid OpenFlow packet type!!\r\n");
/* TODO: Send error message */
bytesProcessed += cntrlPktLen;
pCntrlPkt += cntrlPktLen;
retVal = OFC_FAILURE;
continue;
}
switch (pOfHdr->type)
{
case OFPT_HELLO:
OfcCpSendHelloPacket (pOfHdr->xid);
break;
case OFPT_ECHO_REQUEST:
OfcCpSendEchoReply (pCntrlPkt, cntrlPktLen);
break;
case OFPT_ECHO_REPLY:
break;
case OFPT_FEATURES_REQUEST:
OfcCpSendFeatureReply (pCntrlPkt);
break;
case OFPT_GET_CONFIG_REQUEST:
break;
case OFPT_SET_CONFIG:
break;
case OFPT_PACKET_OUT:
OfcCpProcessPktOut (pCntrlPkt, cntrlPktLen);
break;
case OFPT_FLOW_MOD:
OfcCpProcessFlowMod (pCntrlPkt, cntrlPktLen);
break;
case OFPT_PORT_MOD:
break;
case OFPT_TABLE_MOD:
break;
case OFPT_MULTIPART_REQUEST:
OfcCpProcessMultipartReq (pCntrlPkt, cntrlPktLen);
break;
case OFPT_BARRIER_REQUEST:
OfcCpSendBarrierReply (pOfHdr->xid);
break;
default:
printk (KERN_CRIT "Packet not currently supported\r\n");
break;
}
bytesProcessed += cntrlPktLen;
pCntrlPkt += cntrlPktLen;
}
/* Release processed control packet */
kfree (pPkt);
pPkt = NULL;
return retVal;
}
/******************************************************************
* Function: OfcCpRxDataPathMsg
*
* Description: This function dequeues messages sent by data path
* task and forwards them to the controller.
*
* Input: None
*
* Output: None
*
* Returns: OFC_SUCCESS/OFC_FAILURE
*
*******************************************************************/
void OfcCpRxDataPathMsg (void)
{
tDpCpMsgQ *pMsgQ = NULL;
__u8 *pOpenFlowPkt = NULL;
__u16 pktLen = 0;
down_interruptible (&gOfcCpGlobals.dpMsgQSemId);
while ((pMsgQ = OfcCpRecvFromDpMsgQ()) != NULL)
{
/* Send packet as packet-in to controller */
OfcCpConstructPacketIn (pMsgQ->pPkt, pMsgQ->pktLen,
pMsgQ->inPort, pMsgQ->msgType,
pMsgQ->tableId, pMsgQ->cookie,
pMsgQ->pFlowEntry->matchFields,
&pOpenFlowPkt);
if (pOpenFlowPkt == NULL)
{
printk (KERN_CRIT "Failed to construct Packet-in "
"message\r\n");
if (pMsgQ->pPkt != NULL)
{
kfree (pMsgQ->pPkt);
pMsgQ->pPkt = NULL;
}
kfree (pMsgQ);
pMsgQ = NULL;
continue;
}
pktLen = ntohs (((tOfcOfHdr *) pOpenFlowPkt)->length);
OfcCpSendCntrlPktFromSock (pOpenFlowPkt, pktLen);
printk (KERN_INFO "Sent Packet-In to controller\r\n");
/* Release message */
kfree (pOpenFlowPkt);
pOpenFlowPkt = NULL;
if (pMsgQ->pPkt != NULL)
{
kfree (pMsgQ->pPkt);
pMsgQ->pPkt = NULL;
}
kfree (pMsgQ);
pMsgQ = NULL;
}
up (&gOfcCpGlobals.dpMsgQSemId);
return;
}
/******************************************************************
* Function: OfcCpAddOpenFlowHdr
*
* Description: This function adds OpenFlow standard header to
* an OpenFlow packet type
*
* Input:
*
* Output: ppOpenFlowPkt - Pointer to OpenFlow packet
*
* Returns: OFC_SUCCESS/OFC_FAILURE
*
*******************************************************************/
int OfcCpAddOpenFlowHdr (__u8 *pPktHdr, __u16 pktHdrLen,
__u8 msgType, __u32 xid,
__u8 **ppOfPkt)
{
tOfcOfHdr *pOfHdr = NULL;
__u16 ofHdrLen = 0;
ofHdrLen = OFC_OPENFLOW_HDR_LEN + pktHdrLen;
pOfHdr = (tOfcOfHdr *) kmalloc (ofHdrLen, GFP_KERNEL);
if (pOfHdr == NULL)
{
printk (KERN_CRIT "Failed to allocate memory to OpenFlow "
"header\r\n");
return OFC_FAILURE;
}
memset (pOfHdr, 0, ofHdrLen);
pOfHdr->version = OFC_VERSION;
pOfHdr->type = msgType;
pOfHdr->length = htons (ofHdrLen);
/* xid same as that of controller, hence already in
* network byte order */
pOfHdr->xid = xid;
if (pPktHdr != NULL)
{
memcpy (((__u8 *) pOfHdr) + OFC_OPENFLOW_HDR_LEN,
pPktHdr, pktHdrLen);
}
*ppOfPkt = (__u8 *) pOfHdr;
return OFC_SUCCESS;
}
/******************************************************************
* Function: OfcCpSendHelloPacket
*
* Description: This function is invoked to create the HELLO packet.
* It would invoke the OfcCpSendCntrlPktFromSock
* function to send it out to controller
*
* Input: xid - Transaction ID.
*
* Output: None
*
* Returns: OFC_SUCCESS/OFC_FAILURE
*
*******************************************************************/
int OfcCpSendHelloPacket (__u32 xid)
{
__u8 *pHelloPkt = NULL;
printk (KERN_INFO "Hello message Rx\r\n");
if (OfcCpAddOpenFlowHdr (NULL, 0, OFPT_HELLO, xid, &pHelloPkt)
!= OFC_SUCCESS)
{
printk (KERN_CRIT "Failed to construct Hello packet\r\n");
return OFC_FAILURE;
}
if (OfcCpSendCntrlPktFromSock (pHelloPkt, OFC_OPENFLOW_HDR_LEN)
!= OFC_SUCCESS)
{
printk (KERN_CRIT "Failed to send Hello packet\r\n");
kfree (pHelloPkt);
pHelloPkt = NULL;
return OFC_FAILURE;
}
printk (KERN_INFO "Hello message Tx\r\n");
kfree (pHelloPkt);
pHelloPkt = NULL;
return OFC_SUCCESS;
}
/******************************************************************
* Function: OfcCpSendEchoReply
*
* Description: This function sends echo reply message to the
* controller.
*
* Input: pCntrlPkt - Pointer to control packet (Barrier Request)
* cntrlPktLen - Control packet length
*
* Output: None
*
* Returns: OFC_SUCCESS/OFC_FAILURE
*
*******************************************************************/
int OfcCpSendEchoReply (__u8 *pCntrlPkt, __u16 cntrlPktLen)
{
__u8 *pBarrierReply = NULL;
__u8 *pData = NULL;
__u32 xid = 0;
__u16 dataLen = 0;
printk (KERN_INFO "Echo Request Rx\r\n");
dataLen = ntohs (((tOfcOfHdr *) pCntrlPkt)->length) -
OFC_OPENFLOW_HDR_LEN;
if (dataLen != 0)
{
pData = pCntrlPkt + OFC_OPENFLOW_HDR_LEN;
}
xid = ((tOfcOfHdr *) pCntrlPkt)->xid;
if (OfcCpAddOpenFlowHdr (pData, dataLen, OFPT_ECHO_REPLY, xid,
&pBarrierReply)
!= OFC_SUCCESS)
{
printk (KERN_CRIT "Failed to construct Echo Reply\r\n");
return OFC_FAILURE;
}
if (OfcCpSendCntrlPktFromSock (pBarrierReply,
OFC_OPENFLOW_HDR_LEN + dataLen)
!= OFC_SUCCESS)
{
printk (KERN_CRIT "Failed to send Echo Reply\r\n");
kfree (pBarrierReply);
pBarrierReply = NULL;
return OFC_FAILURE;
}
printk (KERN_INFO "Echo Reply Tx\r\n");
kfree (pBarrierReply);
pBarrierReply = NULL;
return OFC_SUCCESS;
}
/******************************************************************
* Function: OfcCpSendFeatureReply
*
* Description: This function is invoked to reply to FEATURE_REQUEST
* message from the controller.
*
* Input: pCntrlPkt - The packet received from the controller.
*
* Output: None
*
* Returns: OFC_SUCCESS/OFC_FAILURE
*
*******************************************************************/
int OfcCpSendFeatureReply (__u8 *pCntrlPkt)
{
tOfcFeatReply *pResponseMsg = NULL;
struct net_device *dev = NULL;
__u8 *pOpenFlowPkt = NULL;
__u16 featReplyLen = 0;
printk (KERN_INFO "Feature Request Rx\r\n");
pResponseMsg = (tOfcFeatReply *) kmalloc (sizeof(tOfcFeatReply),
GFP_KERNEL);
if (!pResponseMsg)
{
printk (KERN_CRIT "Failed to allocate memory to Feature "
"Response packet\r\n");
return OFC_FAILURE;
}
dev = OfcGetNetDevByIp (gCntrlIpAddr);
if (!dev)
{
printk (KERN_CRIT "Failed to retrieve interface for "
"controller IP\r\n");
kfree (pResponseMsg);
pResponseMsg = NULL;
return OFC_FAILURE;
}
memset (pResponseMsg, 0, sizeof (tOfcFeatReply));
memcpy (pResponseMsg->macDatapathId, dev->dev_addr,
OFC_MAC_ADDR_LEN);
pResponseMsg->maxBuffers = htonl (OFC_MAX_PKT_BUFFER);
pResponseMsg->maxTables = OFC_MAX_FLOW_TABLES;
pResponseMsg->auxilaryId = OFC_CTRL_MAIN_CONNECTION;
pResponseMsg->capabilities =
htonl (OFPC_FLOW_STATS | OFPC_TABLE_STATS);
featReplyLen = sizeof (pResponseMsg->impDatapathId) +
sizeof (pResponseMsg->macDatapathId) +
sizeof (pResponseMsg->maxBuffers) +
sizeof (pResponseMsg->maxTables) +
sizeof (pResponseMsg->auxilaryId) +
sizeof (pResponseMsg->pad) +
sizeof (pResponseMsg->capabilities) +
sizeof (pResponseMsg->reserved);
if (OfcCpAddOpenFlowHdr ((__u8 *) pResponseMsg, featReplyLen,
OFPT_FEATURES_REPLY, ((tOfcOfHdr *) pCntrlPkt)->xid,
&pOpenFlowPkt) != OFC_SUCCESS)
{
printk (KERN_CRIT "Failed to add OpenFlow header in "
"Feature Reply message\r\n");
kfree (pResponseMsg);
pResponseMsg = NULL;
return OFC_FAILURE;
}
if (OfcCpSendCntrlPktFromSock (pOpenFlowPkt,
ntohs (((tOfcOfHdr *) pOpenFlowPkt)->length)) != OFC_SUCCESS)
{
printk (KERN_CRIT "Failed to send Feature Reply message\r\n");
kfree (pResponseMsg);
pResponseMsg = NULL;
kfree (pOpenFlowPkt);
pOpenFlowPkt = NULL;
return OFC_FAILURE;
}
printk (KERN_INFO "Feature Reply Tx\r\n");
kfree(pResponseMsg);
pResponseMsg = NULL;
kfree(pOpenFlowPkt);
pOpenFlowPkt = NULL;
return OFC_SUCCESS;
}
/******************************************************************
* Function: OfcCpConstructPacketIn
*
* Description: This function constructs packet-in message to be
* sent to the controller
*
* Input:
*
* Output: ppOpenFlowPkt - Pointer to OpenFlow packet
*
* Returns: OFC_SUCCESS/OFC_FAILURE
*
*******************************************************************/
int OfcCpConstructPacketIn (__u8 *pPkt, __u32 pktLen, __u8 inPort,
__u8 msgType, __u8 tableId,
tOfcEightByte cookie,
tOfcMatchFields matchFields,
__u8 **ppOpenFlowPkt)
{
tOfcPktInHdr *pPktInHdr = NULL;
tOfcMatchTlv *pMatchTlv = NULL;
tOfcMatchOxmTlv *pOxmTlv = NULL;
__u32 fourByteField = 0;
__u16 twoByteField = 0;
__u16 pktInLen = 0;
__u16 pktInHdrLen = 0;
__u16 matchTlvLen = 0;
__u8 oxmTlvLen = 0;
__u8 padBytes = 0;
__u8 aNullMacAddr[OFC_MAC_ADDR_LEN];
if (pPkt == NULL)
{
printk (KERN_CRIT "[%s]: Data packet missing\r\n", __func__);
return OFC_FAILURE;
}
printk (KERN_INFO "Constructing Packet-In\r\n");
/* Construct match field TLV */
pMatchTlv = (tOfcMatchTlv *) kmalloc (OFC_MTU_SIZE, GFP_KERNEL);
if (pMatchTlv == NULL)
{
printk (KERN_CRIT "Failed to allocate memory to match "
"fields\r\n");
return OFC_FAILURE;
}
memset (pMatchTlv, 0, OFC_MTU_SIZE);
memset (aNullMacAddr, 0, sizeof(aNullMacAddr));
pMatchTlv->type = htons (OFPMT_OXM);
pOxmTlv = (tOfcMatchOxmTlv *) (void *) (((__u8 *) pMatchTlv) +
sizeof(pMatchTlv->type) +
sizeof(pMatchTlv->length));
oxmTlvLen = sizeof(pOxmTlv->Class) + sizeof(pOxmTlv->field) +
sizeof(pOxmTlv->length);
/* TODO: Not supporting field mask option */
/* Add OXM match field TLVs */
if (memcmp (matchFields.aDstMacAddr, aNullMacAddr,
OFC_MAC_ADDR_LEN))
{
pOxmTlv->Class = htons (OFPXMC_OPENFLOW_BASIC);
pOxmTlv->field = OFCXMT_OFB_ETH_DST << 1;
pOxmTlv->length = OFC_MAC_ADDR_LEN;
memcpy (pOxmTlv->aValue, matchFields.aDstMacAddr,
OFC_MAC_ADDR_LEN);
matchTlvLen += oxmTlvLen + pOxmTlv->length;
pOxmTlv = (tOfcMatchOxmTlv *) (void *)
(((__u8 *) pOxmTlv) + oxmTlvLen + pOxmTlv->length);
}
if (memcmp (matchFields.aSrcMacAddr, aNullMacAddr,
OFC_MAC_ADDR_LEN))
{
pOxmTlv->Class = htons (OFPXMC_OPENFLOW_BASIC);
pOxmTlv->field = OFCXMT_OFB_ETH_SRC << 1;
pOxmTlv->length = OFC_MAC_ADDR_LEN;
memcpy (pOxmTlv->aValue, matchFields.aSrcMacAddr,
OFC_MAC_ADDR_LEN);
matchTlvLen += oxmTlvLen + pOxmTlv->length;
pOxmTlv = (tOfcMatchOxmTlv *) (void *)
(((__u8 *) pOxmTlv) + oxmTlvLen + pOxmTlv->length);
}
if (matchFields.vlanId != 0)
{
pOxmTlv->Class = htons (OFPXMC_OPENFLOW_BASIC);
pOxmTlv->field = OFCXMT_OFB_VLAN_VID << 1;
pOxmTlv->length = sizeof (matchFields.vlanId);
twoByteField = htons (matchFields.vlanId);
memcpy (pOxmTlv->aValue, &twoByteField,
sizeof (matchFields.vlanId));
matchTlvLen += oxmTlvLen + pOxmTlv->length;
pOxmTlv = (tOfcMatchOxmTlv *) (void *)
(((__u8 *) pOxmTlv) + oxmTlvLen + pOxmTlv->length);
}
if (matchFields.etherType != 0)
{
pOxmTlv->Class = htons (OFPXMC_OPENFLOW_BASIC);
pOxmTlv->field = OFCXMT_OFB_ETH_TYPE << 1;
pOxmTlv->length = sizeof (matchFields.etherType);
twoByteField = htons (matchFields.etherType);
memcpy (pOxmTlv->aValue, &twoByteField,
sizeof (matchFields.etherType));
matchTlvLen += oxmTlvLen + pOxmTlv->length;
pOxmTlv = (tOfcMatchOxmTlv *) (void *)
(((__u8 *) pOxmTlv) + oxmTlvLen + pOxmTlv->length);
}
if (matchFields.srcIpAddr != 0)
{
pOxmTlv->Class = htons (OFPXMC_OPENFLOW_BASIC);
pOxmTlv->field = OFCXMT_OFB_IPV4_SRC << 1;
pOxmTlv->length = sizeof (matchFields.srcIpAddr);
fourByteField = htonl (matchFields.srcIpAddr);
memcpy (pOxmTlv->aValue, &fourByteField,
sizeof (matchFields.srcIpAddr));
matchTlvLen += oxmTlvLen + pOxmTlv->length;
pOxmTlv = (tOfcMatchOxmTlv *) (void *)
(((__u8 *) pOxmTlv) + oxmTlvLen + pOxmTlv->length);
}
if (matchFields.dstIpAddr != 0)
{
pOxmTlv->Class = htons (OFPXMC_OPENFLOW_BASIC);
pOxmTlv->field = OFCXMT_OFB_IPV4_DST << 1;
pOxmTlv->length = sizeof (matchFields.dstIpAddr);
fourByteField = htonl (matchFields.dstIpAddr);
memcpy (pOxmTlv->aValue, &fourByteField,
sizeof (matchFields.dstIpAddr));
matchTlvLen += oxmTlvLen + pOxmTlv->length;
pOxmTlv = (tOfcMatchOxmTlv *) (void *)
(((__u8 *) pOxmTlv) + oxmTlvLen + pOxmTlv->length);
}
if (matchFields.protocolType != 0)
{
pOxmTlv->Class = htons (OFPXMC_OPENFLOW_BASIC);
pOxmTlv->field = OFCXMT_OFB_IP_PROTO << 1;
pOxmTlv->length = sizeof (matchFields.protocolType);
memcpy (pOxmTlv->aValue, &matchFields.protocolType,
sizeof (matchFields.protocolType));
matchTlvLen += oxmTlvLen + pOxmTlv->length;
pOxmTlv = (tOfcMatchOxmTlv *) (void *)
(((__u8 *) pOxmTlv) + oxmTlvLen + pOxmTlv->length);
}
if (matchFields.srcPortNum != 0)
{
pOxmTlv->Class = htons (OFPXMC_OPENFLOW_BASIC);
switch (matchFields.l4HeaderType)
{
case OFC_TCP_PROT_TYPE:
pOxmTlv->field = OFCXMT_OFB_TCP_SRC << 1;
break;
case OFC_UDP_PROT_TYPE:
pOxmTlv->field = OFCXMT_OFB_UDP_SRC << 1;
break;
}
pOxmTlv->length = sizeof (matchFields.srcPortNum);
twoByteField = htons (matchFields.srcPortNum);
memcpy (pOxmTlv->aValue, &twoByteField,
sizeof (matchFields.srcPortNum));
matchTlvLen += oxmTlvLen + pOxmTlv->length;
pOxmTlv = (tOfcMatchOxmTlv *) (void *)
(((__u8 *) pOxmTlv) + oxmTlvLen + pOxmTlv->length);
}
if (matchFields.dstPortNum != 0)
{
pOxmTlv->Class = htons (OFPXMC_OPENFLOW_BASIC);
switch (matchFields.l4HeaderType)
{
case OFC_TCP_PROT_TYPE:
pOxmTlv->field = OFCXMT_OFB_TCP_DST << 1;
break;
case OFC_UDP_PROT_TYPE:
pOxmTlv->field = OFCXMT_OFB_UDP_DST << 1;
break;
}
pOxmTlv->length = sizeof (matchFields.dstPortNum);
twoByteField = htons (matchFields.dstPortNum);
memcpy (pOxmTlv->aValue, &twoByteField,
sizeof (matchFields.dstPortNum));
matchTlvLen += oxmTlvLen + pOxmTlv->length;
pOxmTlv = (tOfcMatchOxmTlv *) (void *)
(((__u8 *) pOxmTlv) + oxmTlvLen + pOxmTlv->length);
}
/* Add input port in match field TLV OXM fields */
pOxmTlv->Class = htons (OFPXMC_OPENFLOW_BASIC);
pOxmTlv->field = OFCXMT_OFB_IN_PORT << 1;
pOxmTlv->length = sizeof (__u32);
fourByteField = inPort;
fourByteField = htonl (fourByteField);
memcpy (pOxmTlv->aValue, &fourByteField, sizeof(fourByteField));
matchTlvLen += oxmTlvLen + pOxmTlv->length;
pOxmTlv = (tOfcMatchOxmTlv *) (void *)
(((__u8 *) pOxmTlv) + oxmTlvLen + pOxmTlv->length);
matchTlvLen += sizeof (pMatchTlv->type) +
sizeof (pMatchTlv->length);
pMatchTlv->length = htons (matchTlvLen);
/* Add match padding bytes */
if (matchTlvLen % 8)
{
matchTlvLen = (matchTlvLen + 8) - (matchTlvLen % 8);
}
/* Construct packet-in message */
pktInLen = sizeof (((tOfcPktInHdr *) 0)->bufId) +
sizeof (((tOfcPktInHdr *) 0)->totLength) +
sizeof (((tOfcPktInHdr *) 0)->reason) +
sizeof (((tOfcPktInHdr *) 0)->tableId) +
sizeof (((tOfcPktInHdr *) 0)->cookie);
pktInHdrLen = pktInLen + matchTlvLen + pktLen;
#if 0
/* Add padding if required */
if (pktInHdrLen % 8)
{
padBytes = 8 - (pktInHdrLen % 8);
}
#endif
/* Add packet-in padding bytes */
padBytes += 2;
pktInHdrLen += padBytes;
pPktInHdr = (tOfcPktInHdr *) kmalloc (pktInHdrLen, GFP_KERNEL);
if (pPktInHdr == NULL)
{
printk (KERN_CRIT "Failed to allocate memory to packet-in"
" message\r\n");
kfree (pMatchTlv);
pMatchTlv = NULL;
return OFC_FAILURE;
}
memset (pPktInHdr, 0, pktInHdrLen);
pPktInHdr->bufId = htonl (OFC_NO_BUFFER);
pPktInHdr->totLength = htons (pktLen);
pPktInHdr->reason = msgType;
pPktInHdr->tableId = tableId;
pPktInHdr->cookie.lo = cookie.lo;
pPktInHdr->cookie.hi = cookie.hi;
memcpy (((__u8 *) pPktInHdr) + pktInLen, pMatchTlv, matchTlvLen);
memcpy (((__u8 *) pPktInHdr) + pktInLen + matchTlvLen + padBytes,
pPkt, pktLen);
/* Add standard OpenFlow header */
OfcCpAddOpenFlowHdr ((__u8 *) pPktInHdr, pktInHdrLen,
OFPT_PACKET_IN, 0, ppOpenFlowPkt);
kfree (pPktInHdr);
pPktInHdr = NULL;
kfree (pMatchTlv);
pMatchTlv = NULL;
return OFC_SUCCESS;
}
/******************************************************************
* Function: OfcCpProcessPktOut
*
* Description: This function processes packet out messages
* received from the controller and takes actions
* as mentioned in the message
*
* Input: pPkt - Pointer to control packet
* pktLen - Length of control packet
*
* Output: None
*
* Returns: OFC_SUCCESS/OFC_FAILURE
*
*******************************************************************/
int OfcCpProcessPktOut (__u8 *pPkt, __u16 pktLen)
{
struct list_head *pActionListHead;
tOfcActionList *pActionList = NULL;
tOfcActionTlv *pActionTlv = NULL;
tOfcPktOutHdr *pPktOut = NULL;
tDpCpMsgQ msgQ;
__u16 actionListLen = 0;
__u16 dataPktLen = 0;
__u8 *pPktParser = NULL;
__u8 *pDataPkt = NULL;
printk (KERN_INFO "Packet-Out message Rx\r\n");
pActionListHead = (struct list_head *) kmalloc
(sizeof(struct list_head), GFP_KERNEL);
if (pActionListHead == NULL)
{
printk (KERN_CRIT "Failed to allocate memory to action"
" list head\r\n");
return OFC_FAILURE;
}
INIT_LIST_HEAD (pActionListHead);
pPktOut =
(tOfcPktOutHdr *) (void *) (pPkt + OFC_OPENFLOW_HDR_LEN);
actionListLen = ntohs (pPktOut->actionsLen);
pActionTlv = (tOfcActionTlv *) (void *) (((__u8 *) pPktOut) +
sizeof (tOfcPktOutHdr));
/* Extract action list to be sent to data path task
* from packet-out message, since actions shall be taken by
* data path task */
while (actionListLen > 0)
{
pActionList = (tOfcActionList *) kmalloc
(sizeof (tOfcActionList), GFP_KERNEL);
if (pActionList == NULL)
{
printk (KERN_CRIT "Failed to allocate memory to "
"action list\r\n");
/* TODO: Delete action list */
OfcDeleteList (pActionListHead);
kfree (pActionListHead);
pActionListHead = NULL;
return OFC_FAILURE;
}
memset (pActionList, 0, sizeof (tOfcActionList));
pActionList->actionType = ntohs (pActionTlv->type);
INIT_LIST_HEAD (&pActionList->list);
switch (pActionList->actionType)
{
case OFCAT_OUTPUT:
pPktParser = (__u8 *) (void *)
(((__u8 *) pActionTlv) +
sizeof (pActionTlv->type) +
sizeof (pActionTlv->length));
memcpy (&pActionList->u.outPort, pPktParser,
sizeof (pActionList->u.outPort));
pActionList->u.outPort = ntohl (pActionList->u.outPort);
list_add_tail (&pActionList->list, pActionListHead);
break;
default:
break;
}
actionListLen -= ntohs (pActionTlv->length);
pActionTlv = (tOfcActionTlv *) (void *)
(((__u8 *) pActionTlv) + ntohs (pActionTlv->length));
}
/* Extract data packet */
pPktParser = (__u8 *) (void *) pActionTlv;
dataPktLen = ntohs (((tOfcOfHdr *) pPkt)->length) -
OFC_OPENFLOW_HDR_LEN - sizeof (tOfcPktOutHdr) -
(ntohs (pPktOut->actionsLen));
pDataPkt = (__u8 *) kmalloc (dataPktLen, GFP_KERNEL);
if (pDataPkt == NULL)
{
printk (KERN_CRIT "[%s]: Failed to allocate memory to "
"data packet\r\n", __func__);
OfcDeleteList (pActionListHead);
kfree (pActionListHead);
pActionListHead = NULL;
return OFC_FAILURE;
}
memset (pDataPkt, 0, dataPktLen);
memcpy (pDataPkt, pPktParser, dataPktLen);
/* Send message to data path task */
memset (&msgQ, 0, sizeof (msgQ));
msgQ.msgType = OFC_PACKET_OUT;
msgQ.pPkt = pDataPkt;
msgQ.pktLen = dataPktLen;
msgQ.pActionListHead = pActionListHead;
OfcCpSendToDpQ (&msgQ);
OfcDpSendEvent (OFC_CP_TO_DP_EVENT);
return OFC_SUCCESS;
}
/******************************************************************
* Function: OfcCpProcessFlowMod
*
* Description: This function processes flow mod messages received
* from the controller and installs or deletes flows
* in the flow table
*
* Input: pPkt - Pointer to control packet
* pktLen - Length of control packet
*
* Output: None
*
* Returns: OFC_SUCCESS/OFC_FAILURE
*
*******************************************************************/
int OfcCpProcessFlowMod (__u8 *pPkt, __u16 pktLen)
{
tOfcFlowModHdr *pFlowMod = NULL;
tOfcFlowEntry *pFlowEntry = NULL;
tDpCpMsgQ msgQ;
__u16 flowModLen = 0;
printk (KERN_INFO "Flow Mod Message Rx\r\n");
pFlowMod =
(tOfcFlowModHdr *) (void *) (pPkt + OFC_OPENFLOW_HDR_LEN);
flowModLen = pktLen - OFC_OPENFLOW_HDR_LEN;
switch (pFlowMod->command)
{
case OFPFC_ADD:
/* Intentional fall through */
case OFPFC_DELETE:
pFlowEntry = OfcCpExtractFlow (pFlowMod, flowModLen);
if (pFlowEntry == NULL)
{
printk (KERN_CRIT "Failed to extract flow from Flow"
" Mod message\r\n");
return OFC_FAILURE;
}
break;
default:
printk (KERN_CRIT "Flow Mod command not supported!!\r\n");
return OFC_FAILURE;
}
/* Send the extracted flow to data path task for insertion or
* deletion in flow table */
memset (&msgQ, 0, sizeof (msgQ));
msgQ.pFlowEntry = pFlowEntry;
msgQ.msgType = (pFlowMod->command == OFPFC_ADD) ?
OFC_FLOW_MOD_ADD : OFC_FLOW_MOD_DEL;
OfcCpSendToDpQ (&msgQ);
OfcDpSendEvent (OFC_CP_TO_DP_EVENT);
return OFC_SUCCESS;
}
/******************************************************************
* Function: OfcCpExtractFlow
*
* Description: This function extracts a flow entry from Flow Mod
* message
*
* Input: pFlowMod - Pointer to flow mod header
* flowModLen - Length of flow mod packet
*
* Output: None
*
* Returns: Pointer to flow entry
*
*******************************************************************/
tOfcFlowEntry *OfcCpExtractFlow (tOfcFlowModHdr *pFlowMod,
__u16 flowModLen)
{