Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / verif / env / common / vera / niu_ippktgen / C / wrapper / genCpacket.c
CommitLineData
86530b38
AT
1#include <stdio.h>
2#include "vera_directc.h"
3
4// #if (HAVE_CONFIG_H)
5 // #include "../include/config.h"
6// #endif
7// #include "./libnet_test.h"
8
9#include "include/libnet.h"
10
11#include "include/vera_flow_vars.h"
12#include "include/packet.h"
13
14#define FLOW_SIZE 200
15#define SIZE_OF_PKT_FIELDS 8
16
17extern void uChartoVec32( char *in, int size, vec32** s);
18extern void uInttoVec32( unsigned int *in, int size, vec32 **s) ;
19
20extern void Vec32touChar ( char *in, int size, vec32 **s);
21extern char * libnet_call(struct packet_desc *packet_vars, u_int32_t *len);
22extern unsigned int * getPktFields(struct packet_desc *packet_vars) ;
23
24uint8_t decode_old_type( uint8_t frame_type, uint8_t frame_class, uint8_t layer ) {
25 uint8_t returnval;
26
27 returnval = 0;
28 // L4 Decodes
29 if(layer==4) {
30
31 if( (frame_class == PKTGEN_CL_TCP) || (frame_class == PKTGEN_CL_TCP_FRAG ) ||
32 (frame_class == PKTGEN_CL_TCP_OPT ) || (frame_class == PKTGEN_CL_TCP_IP_V6)) {
33 returnval = L4_PKT_TYPE_TCP;
34 }
35 else if( (frame_class == PKTGEN_CL_UDP) || (frame_class == PKTGEN_CL_UDP_FRAG ) ||
36 (frame_class == PKTGEN_CL_UDP_OPT) || (frame_class == PKTGEN_CL_UDP_IP_V6)) {
37 returnval = L4_PKT_TYPE_UDP;
38 }
39 else if( (frame_class == PKTGEN_CL_SCTP) || (frame_class == PKTGEN_CL_SCTP_FRAG) ||
40 (frame_class == PKTGEN_CL_SCTP_OPT) || (frame_class == PKTGEN_CL_SCTP_IP_V6)) {
41 returnval = L4_PKT_TYPE_SCTP;
42 }
43 else if( (frame_class == PKTGEN_CL_IP_SEC_AH) || (frame_class == PKTGEN_CL_IP_V6_SEC_AH)) {
44 returnval = L4_PKT_TYPE_AH;
45 }
46 else if( (frame_class == PKTGEN_CL_IP_SEC_ESP) || (frame_class == PKTGEN_CL_IP_V6_SEC_ESP)) {
47 returnval = L4_PKT_TYPE_ESP;
48 }
49 else {
50 /* TO BE DONE*/
51 }
52
53 }
54 // L3 Decodes
55 if(layer==3) {
56 if((frame_type&0x2) && ( frame_type & 0x8)) {
57 returnval = L3_PKT_TYPE_IPV6;
58 } else if( (frame_type&0x2)) {
59 returnval = L3_PKT_TYPE_IPV4;
60 }
61
62 if( (frame_class == PKTGEN_CL_TCP_FRAG) || (frame_class == PKTGEN_CL_UDP_FRAG) || (frame_class == PKTGEN_CL_SCTP_FRAG)) {
63 returnval = returnval | L3_PKT_TYPE_FRAG;
64 }
65
66 // Add more code here for FRAG, OPTions
67 // returnval = returnval | L3_PKT_TYPE_OPT
68 // returnval = returnval | L3_PKT_TYPE_FRAG
69 }
70
71 // L2 Decodes
72 if(layer==2) {
73 returnval = L2_PKT;
74 if((frame_type&0x4)) {
75 returnval = returnval | L2_PKT_TYPE_TAGGED;
76 }
77 if((frame_type&0x1)) {
78 returnval = returnval | L2_PKT_TYPE_LLCSNAP;
79 }
80 if(frame_class == PKTGEN_CL_ARP) {
81 returnval = returnval | L2_PKT_TYPE_ARP;
82 }
83 if(frame_class == PKTGEN_CL_RARP) {
84 returnval = returnval | L2_PKT_TYPE_RARP;
85 }
86 if(frame_class == PKTGEN_CL_RUNT) {
87 returnval= L2_RUNT;
88 }
89 // Add more code here for other l2 types
90
91
92 }
93 return(returnval);
94
95
96}
97
98unsigned int * getPktFields(struct packet_desc *packet_vars) {
99// This is to be used by the vera-pktgen to figure out the various fields calculated by
100// C pktgen
101//
102 unsigned int * pkt_fields;
103 pkt_fields = ( unsigned int * ) malloc(SIZE_OF_PKT_FIELDS*(sizeof(unsigned int)));
104 pkt_fields[L2_PAYLOAD_LENGTH] = packet_vars->l2_len;
105 pkt_fields[L2_HDR_LENGTH] = packet_vars->l2_hdr;
106 pkt_fields[L3_PAYLOAD_LENGTH] = packet_vars->l3_len;
107 pkt_fields[L3_HDR_LENGTH] = packet_vars->l3_hdr;
108 pkt_fields[L4_PAYLOAD_LENGTH] = packet_vars->l4_len;
109 pkt_fields[L4_HDR_LENGTH] = packet_vars->l4_hdr;
110
111 return(pkt_fields);
112
113}
114
115struct packet_desc parseVeraflowVars (unsigned char * flow) {
116 int i;
117 struct packet_desc packet_vars;
118 int l2_headers, l3_headers, l4_headers;
119
120 memset(&packet_vars, 0, sizeof (struct packet_desc));
121
122
123
124 packet_vars.frame_type = flow[FRAME_TYPE_INDEX] ;
125 packet_vars.frame_class = flow[FRAME_CLASS_INDEX] ;
126
127 for(i = 0;i< L2_DA_SIZE ;i++) {
128 packet_vars.ether.dst_addr[L2_DA_SIZE -1 -i]= flow[L2_DA_INDEX + i];
129 // printf(" C Index - %d value - %x \n",L2_DA_INDEX + i,flow[L2_DA_INDEX + i]);
130 }
131
132 for(i = 0;i< L2_SA_SIZE;i++) {
133 packet_vars.ether.src_addr[L2_SA_SIZE -1 -i] = flow[L2_SA_INDEX + i];
134 // printf(" C Index - %d value - %x \n",L2_SA_INDEX + i,flow[L2_SA_INDEX + i]);
135 }
136
137 packet_vars.ether.vlan_id = 0;
138 for(i = 0;i< TPID_SIZE;i++)
139 packet_vars.ether.vlan_id = (flow[TPID_INDEX + i] << (i*8)) | ( packet_vars.ether.vlan_id );
140
141 packet_vars.ether.tci = 0;
142 for(i = 0;i< TCI_SIZE;i++)
143 packet_vars.ether.tci = (flow[TCI_INDEX + i] << (i*8)) | ( packet_vars.ether.tci);
144
145 packet_vars.ip.options = flow[IP_OPTION_INDEX];
146
147
148 packet_vars.ip.frag = 0;//default 0x0;
149 for(i = 0;i< IP_FRAG_SIZE ;i++)
150 packet_vars.ip.frag = (flow[ IP_FRAG_INDEX+ i] << (i*8)) | ( packet_vars.ip.frag);
151
152 packet_vars.ip.src_addr = 0;
153 for(i = 0;i< IPV4_SRC_ADDR_SIZE;i++)
154 packet_vars.ip.src_addr = (flow [IPV4_SRC_ADDR_INDEX + i] << (i*8)) | (packet_vars.ip.src_addr );
155
156 packet_vars.ip.dst_addr = 0;
157 for(i = 0;i< IPV4_DST_ADDR_SIZE;i++)
158 packet_vars.ip.dst_addr = (flow[IPV4_DST_ADDR_INDEX + i] << (i*8))| ( packet_vars.ip.dst_addr );
159
160 packet_vars.tcp.src_port = 0;
161 packet_vars.udp.src_port = 0;
162 for(i = 0;i< IPV4_SRC_PRT_SIZE;i++) {
163 packet_vars.tcp.src_port = (flow[IPV4_SRC_PRT_INDEX + i] << (i*8)) | ( packet_vars.tcp.src_port ) ;
164 packet_vars.udp.src_port = (flow[IPV4_SRC_PRT_INDEX + i] << (i*8)) | ( packet_vars.udp.src_port ) ;
165 }
166
167 packet_vars.tcp.dst_port = 0;
168 packet_vars.udp.dst_port = 0;
169 for(i = 0;i< IPV4_DST_PRT_SIZE;i++) {
170 packet_vars.tcp.dst_port = (flow[IPV4_DST_PRT_INDEX + i] << (i*8)) | ( packet_vars.tcp.dst_port );
171 packet_vars.udp.dst_port = (flow[IPV4_DST_PRT_INDEX + i] << (i*8)) | ( packet_vars.udp.dst_port );
172 }
173
174 packet_vars.tcp.tcp_flags = flow[TCP_FLAGS_INDEX];
175
176 packet_vars.ipsec_ah.spi = 0;
177 for(i = 0;i< SPI_SIZE;i++)
178 packet_vars.ipsec_ah.spi = (flow[SPI_INDEX + i] << (i*8)) | ( packet_vars.ipsec_ah.spi );
179
180 packet_vars.ipsec_ah.seq = 0;
181 for(i = 0;i< ESP_AH_SEQ_NO_SIZE;i++)
182 packet_vars.ipsec_ah.seq = (flow[ESP_AH_SEQ_NO_INDEX + i] << (i*8)) | ( packet_vars.ipsec_ah.seq );
183
184 for(i = 0;i< IPV6_SRC_ADDR_SIZE;i++)
185 packet_vars.ipv6.src_addr[IPV6_SRC_ADDR_SIZE - i -1] = flow[IPV6_SRC_ADDR_INDEX + i];
186
187 for(i = 0;i< IPV6_DST_ADDR_SIZE;i++) {
188 packet_vars.ipv6.dst_addr[IPV6_DST_ADDR_SIZE - i - 1] = flow[IPV6_DST_ADDR_INDEX + i];
189 // printf(" Index - %d Vera Value - %x C Value - %x \n",IPV6_DST_ADDR_INDEX + i,flow[IPV6_DST_ADDR_INDEX + i], packet_vars.ipv6.dst_addr[IPV6_DST_ADDR_SIZE - i - 1]);
190 }
191 // Old packet gen uses the TOS field for traffic class in case of Ipv6
192 packet_vars.ip.tos = flow[TOS_INDEX];
193 packet_vars.ipv6.tc = flow[TOS_INDEX];
194 // printf("Index - %d Tos - %x \n",TOS_INDEX,packet_vars.ipv6.tc);
195
196
197 // To OverWrite from test
198 packet_vars.ipv6.nexthdr = flow[IPV6_NXT_HDR ] ;
199 packet_vars.ipv6.nexthdr = 0xff; // Remove this later
200
201
202 packet_vars.tcp.seq_no = 0;
203 for(i = 0;i< TCP_SEQ_NO_SIZE;i++) {
204 packet_vars.tcp.seq_no = (flow[TCP_SEQ_NO_INDEX + i] << (i*8)) | ( packet_vars.tcp.seq_no );
205 }
206 packet_vars.tcp.ack_no = 0;
207 // Need more parameters for ipsec_ah packets
208
209// packet_vars.tcp.seq_no = 0x00190114;
210 // packet_vars.tcp.ack_no = 0x00000014;
211
212
213 /*sctp*/
214 packet_vars.sctp.vtag = 0;
215 for(i = 0;i< SCTP_VTAG_SIZE;i++)
216 packet_vars.sctp.vtag = (flow[SCTP_VTAG_INDEX + i] << (i*8)) | ( packet_vars.sctp.vtag );
217
218 packet_vars.sctp.src_port = 0;
219 for(i = 0;i< SCTP_SRC_PRT_SIZE;i++) {
220 packet_vars.sctp.src_port = (flow[SCTP_SRC_PRT_INDEX + i] << (i*8)) | ( packet_vars.sctp.src_port );
221 }
222 packet_vars.sctp.dst_port = 0;
223 for(i = 0;i< SCTP_DST_PRT_SIZE;i++) {
224 packet_vars.sctp.dst_port = (flow[SCTP_DST_PRT_INDEX + i] << (i*8)) | ( packet_vars.sctp.dst_port );
225 }
226 packet_vars.sctp.checksum = 0;
227
228
229 packet_vars.payload.data_length = 0;
230 for(i = 0;i< DATA_LENGTH_SIZE ;i++)
231 packet_vars.payload.data_length = (flow[DATA_LENGTH_INDEX + i] << (i*8)) | ( packet_vars.payload.data_length );
232
233 // printf(" Data Length in C - %d \n",packet_vars.payload.data_length);
234 // printf(" Flow in C - %x %x \n",flow[DATA_LENGTH_INDEX +0],flow[DATA_LENGTH_INDEX +1]);
235
236 packet_vars.payload.l2_pad_length = 0;
237 for(i = 0;i< L2_PAD_LENGTH_SIZE ;i++)
238 packet_vars.payload.l2_pad_length = (flow[L2_PAD_LENGTH_INDEX + i] << (i*8)) | ( packet_vars.payload.l2_pad_length );
239
240 // printf(" PAD Length in C - %d \n",packet_vars.payload.l2_pad_length);
241 // printf(" Index - %d %d, Value - %x %x\n",L2_PAD_LENGTH_INDEX,L2_PAD_LENGTH_INDEX + 1,flow[L2_PAD_LENGTH_INDEX + 0],flow[L2_PAD_LENGTH_INDEX + 1]);
242 // packet_vars.payload.data_seed = 0x03020100;
243 // packet_vars.payload.data_type = 0x1;
244 packet_vars.payload.data_type = flow[DATA_TYPE_INDEX]&0xff;
245 packet_vars.payload.data_seed = 0;
246 for(i = 0;i< DATA_SEED_SIZE ;i++)
247 packet_vars.payload.data_seed = (flow[DATA_SEED_INDEX + i] << (i*8)) | ( packet_vars.payload.data_seed );
248 // printf("DataSeed - %x Data Type - %d \n",packet_vars.payload.data_seed,packet_vars.payload.data_type);
249
250
251 packet_vars.payload.error_code = 0;
252 for(i = 0;i< DATA_ERR_CODE_SIZE ;i++) {
253 packet_vars.payload.error_code = (flow[DATA_ERR_CODE_INDEX + i] << (i*8)) | ( packet_vars.payload.error_code );
254 // printf("Index - %d Value - %d \n",DATA_ERR_CODE_INDEX + i, flow[DATA_ERR_CODE_INDEX + i]);
255 }
256
257 packet_vars.payload.error_data = 0;
258 for(i = 0;i< DATA_ERR_DATA_SIZE ;i++)
259 packet_vars.payload.error_data = (flow[DATA_ERR_DATA_INDEX + i] << (i*8)) | ( packet_vars.payload.error_data );
260 // printf("ErrorCode - %x ErrorData - %x \n",packet_vars.payload.error_code,packet_vars.payload.error_data);
261
262
263 // check for any checksum error injection from the test,if so change it here
264 if( ( packet_vars.payload.error_code & PG_CHKSUM_USER_MODE) ) {
265 packet_vars.udp.checksum = packet_vars.payload.error_data&0xffff;
266 packet_vars.tcp.checksum = packet_vars.payload.error_data&0xffff;
267 } else if(packet_vars.payload.error_code & PG_CHKSUM_ERR ){
268 //packet_vars.udp.checksum = 0xdead;
269 //packet_vars.tcp.checksum = 0xbeef;
270 packet_vars.udp.checksum = 0;
271 packet_vars.tcp.checksum = 0;
272 } else {
273 packet_vars.udp.checksum = 0;
274 packet_vars.tcp.checksum = 0;
275 }
276
277 packet_vars.ip.proto = flow[L4_PROTO_ERR_INDEX] ;
278
279 packet_vars.l4_pkt_type = 0;
280 packet_vars.l3_pkt_type = 0;
281 packet_vars.l2_pkt_type = 0;
282 // To maintain compatibility with old pktgen
283 packet_vars.l4_pkt_type = decode_old_type(packet_vars.frame_type, packet_vars.frame_class,4);
284 packet_vars.l3_pkt_type = decode_old_type(packet_vars.frame_type, packet_vars.frame_class,3);
285 packet_vars.l2_pkt_type = decode_old_type(packet_vars.frame_type, packet_vars.frame_class,2);
286
287 // Set various lengths - in case of total frame length being specified
288
289 if(packet_vars.l2_pkt_type) {
290 l2_headers = 0;
291 if(packet_vars.l2_pkt_type & L2_PKT_TYPE_LLCSNAP) {
292 l2_headers +=LIBNET_802_2SNAP_H;
293 }
294 if(packet_vars.l2_pkt_type & L2_PKT_TYPE_TAGGED) {
295 l2_headers += 4;
296 }
297 l2_headers+= LIBNET_ETH_H;
298 packet_vars.l2_len = packet_vars.payload.data_length - 4 - l2_headers;
299 } else packet_vars.l2_len = 0;
300
301 /* Fix for ARP, RARP Packets */
302 /* Old Pktgen seems to harcode values for various fields with the ARP, RARP packets
303 * These fields needs to be controlled by the tests. In the absence of that, these
304 * fields are being set to some random value- untell they are tied to through the tests*/
305 if(packet_vars.l2_pkt_type & L2_PKT_TYPE_ARP) {
306 packet_vars.l3_pkt_type = 0;
307 packet_vars.l2_len = packet_vars.payload.data_length - 4 - LIBNET_ARP_ETH_IP_H -l2_headers;
308 }
309 if(packet_vars.l2_pkt_type & L2_PKT_TYPE_RARP) {
310 packet_vars.l3_pkt_type = 0;
311 packet_vars.l2_len = packet_vars.payload.data_length - 4 -LIBNET_ARP_ETH_IP_H -l2_headers;
312 }
313 packet_vars.arp.hw_type =0;
314 for(i = 0;i< ARP_HW_TYPE_SIZE ;i++)
315 packet_vars.arp.hw_type = (flow[ARP_HW_TYPE_INDEX + i] << (i*8)) | ( packet_vars.arp.hw_type);
316
317 packet_vars.arp.proto_type = 0x0; // default - 0x0800
318 for(i = 0;i< ARP_PROTO_TYPE_SIZE ;i++)
319 packet_vars.arp.proto_type = (flow[ARP_PROTO_TYPE_INDEX + i] << (i*8)) | ( packet_vars.arp.proto_type);
320
321 packet_vars.arp.hw_len = flow[ARP_HW_LEN_INDEX]&0xff;// default 0x6;
322 packet_vars.arp.proto_len = flow[ARP_PROTO_LEN_INDEX]&0xff;//default 0x4;
323
324 packet_vars.arp.op_code = 0;//default 0x1;
325 for(i = 0;i< ARP_PROTO_TYPE_SIZE ;i++)
326 packet_vars.arp.op_code = (flow[ ARP_OPCODE_INDEX+ i] << (i*8)) | ( packet_vars.arp.op_code);
327
328 /* Various Address fields in the Arp Packet */
329 for(i=0;i<ARP_SRC_ADDR_SIZE;i++) {
330 packet_vars.arp.src_ha[ARP_SRC_ADDR_SIZE - 1 - i] = flow[ARP_SRC_ADDR_INDEX + i] ;
331 packet_vars.arp.tgt_ha[ARP_SRC_ADDR_SIZE - 1 - i] = flow[ARP_TGT_ADDR_INDEX + i] ;
332 }
333 for(i=0;i<ARP_SRCIP_ADDR_SIZE;i++) {
334 packet_vars.arp.src_ip[ARP_SRCIP_ADDR_SIZE - 1 -i] = flow[ARP_SRCIP_ADDR_INDEX + i] ;
335 packet_vars.arp.tgt_ip[ARP_SRCIP_ADDR_SIZE - 1 -i] = flow[ARP_TGTIP_ADDR_INDEX + i] ;
336 }
337
338
339
340 if(packet_vars.l3_pkt_type) {
341 if( packet_vars.l3_pkt_type & L3_PKT_TYPE_IPV4) {
342 l3_headers = LIBNET_IPV4_H + packet_vars.ip.options;
343 packet_vars.l3_len = packet_vars.payload.data_length - 4 - l2_headers - l3_headers;
344 } else if(packet_vars.l3_pkt_type & L3_PKT_TYPE_IPV6) {
345 l3_headers = LIBNET_IPV6_H + packet_vars.ip.options;
346 packet_vars.l3_len = packet_vars.payload.data_length - 4 - l2_headers - l3_headers;
347 }
348 packet_vars.l2_len = 0;
349
350#if 0
351 // old code!!
352 // choose the fragment bits and offset
353 if(packet_vars.l3_pkt_type & L3_PKT_TYPE_FRAG) {
354 packet_vars.ip.frag = 0; // NO SURE WHAT TO CHOOSE
355 packet_vars.ip.frag = 0x20fa;
356 }
357#endif
358 } else packet_vars.l3_len = 0;
359
360 if(packet_vars.l4_pkt_type) {
361 if(packet_vars.l4_pkt_type & L4_PKT_TYPE_TCP) {
362 l4_headers = LIBNET_TCP_H;
363 packet_vars.l4_len = packet_vars.payload.data_length - 4 - l2_headers - l3_headers - l4_headers;
364 } else if(packet_vars.l4_pkt_type & L4_PKT_TYPE_UDP) {
365 l4_headers = LIBNET_UDP_H;
366 packet_vars.l4_len = packet_vars.payload.data_length - 4 - l2_headers - l3_headers - l4_headers;
367 } else if(packet_vars.l4_pkt_type & L4_PKT_TYPE_AH) {
368 l4_headers = LIBNET_IPSEC_AH_H;
369 packet_vars.l4_len = packet_vars.payload.data_length - 4 - l2_headers - l3_headers - l4_headers;
370 } else if(packet_vars.l4_pkt_type & L4_PKT_TYPE_ESP) {
371 l4_headers = LIBNET_IPSEC_ESP_HDR_H;
372 packet_vars.l4_len = packet_vars.payload.data_length - 4 - l2_headers - l3_headers - l4_headers;
373 } else if(packet_vars.l4_pkt_type & L4_PKT_TYPE_SCTP) {
374 l4_headers = LIBNET_SCTP_H;
375 packet_vars.l4_len = packet_vars.payload.data_length - 4 - l2_headers - l3_headers - l4_headers;
376 } else {}
377 packet_vars.l3_len = 0;
378 packet_vars.l2_len = 0;
379 } else {
380 packet_vars.l4_len = 0;
381 }
382 packet_vars.l2_hdr = l2_headers;
383 packet_vars.l3_hdr = l3_headers;
384 packet_vars.l4_hdr = l4_headers;
385
386 // printf(" L4 Length Set to %d \n",packet_vars.l4_len);
387
388 return(packet_vars);
389
390
391}
392void genCPacket ( int index, vec32** flow_vars, vec32** s, vec32** p ) {
393 char * data;
394 struct packet_desc packet_vars;
395 u_int32_t pktlen=0;
396 unsigned char *flow;
397 unsigned int *pkt_fields;
398
399 flow = (unsigned char *) malloc(FLOW_SIZE*sizeof(unsigned char));
400
401 Vec32touChar(flow,FLOW_SIZE/*CHOOSE CORRECT SIZE*/ ,flow_vars);
402 packet_vars = parseVeraflowVars(flow);
403
404
405 pkt_fields = getPktFields(&packet_vars);
406 uInttoVec32(pkt_fields ,SIZE_OF_PKT_FIELDS ,p);
407
408 // Parse flow_vars and populate struct for libnet
409 // generate packet through libnet, return it to vera
410 // check for user error!!
411 if( (packet_vars.l4_len<0) | (packet_vars.l3_len<0) |(packet_vars.l2_len<0)){
412 printf("USER ERROR!!! payload length set to be less than 0!! \n");
413 } else {
414 data = libnet_call(&packet_vars, &pktlen);
415 uChartoVec32(data ,/*packet_vars.l2_len*/pktlen ,s);
416 }
417
418}
419
420