API Reference Manual  1.46.0
odp_ipfragreass_helpers.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2017-2018 Linaro Limited
3  */
4 
7 #include <stdio.h>
8 #include <stdlib.h>
9 
10 #include <assert.h>
11 #include <arpa/inet.h>
12 
13 #include "odp_ipfragreass_helpers.h"
14 #include "odp_ipfragreass_ip.h"
15 
16 #define IP_SRC_ADDR 0xC0A80001 /* 192.168.0.1 */
17 #define IP_DST_ADDR 0xC0A80002 /* 192.168.0.2 */
18 
19 static void set_random_payload(odp_packet_t packet, uint32_t payload_offset,
20  uint32_t size)
21 {
22  uint32_t i, j;
23  unsigned char *buffer;
24  unsigned char seed = rand() % (UINT8_MAX + 1);
25  uint32_t bytes_remaining = 0;
26 
27  /*
28  * Set the payload to a run of consecutive numbers from a random seed.
29  * Not completely random as some structure is good for debugging.
30  */
31  for (i = 0; i < size; ++i) {
32  buffer = odp_packet_offset(packet, payload_offset + i,
33  &bytes_remaining, NULL);
34  for (j = 0; i < size && j < bytes_remaining; ++j, ++i)
35  buffer[j] = (unsigned char)(seed + i);
36  }
37 }
38 
39 odp_packet_t pack_udp_ipv4_packet(odp_pool_t pool, odp_u16be_t ip_id,
40  uint32_t max_size, uint32_t min_size)
41 {
42  odp_packet_t result;
43  unsigned char *buffer;
44  odph_ipv4hdr_t *header;
45  uint32_t size;
46  uint32_t header_len;
47  uint32_t header_len_words;
48 
49  size = (rand() % (max_size - min_size)) + min_size;
50  result = odp_packet_alloc(pool, size);
51  if (result == ODP_PACKET_INVALID)
52  return ODP_PACKET_INVALID;
54 
55  /* Set the IPv4 header */
56  header_len_words = (rand() % (IP_IHL_MAX - IP_IHL_MIN + 1));
57  header_len_words += IP_IHL_MIN;
58  header_len = WORDS_TO_BYTES(header_len_words);
59  assert(header_len >= IP_HDR_LEN_MIN && header_len <= IP_HDR_LEN_MAX);
60  odp_packet_l3_offset_set(result, 0);
61  odp_packet_has_ipv4_set(result, 1);
62  buffer = odp_packet_data(result);
63  header = (odph_ipv4hdr_t *)buffer;
64  ipv4hdr_init(header);
65  header->id = odp_cpu_to_be_16(ip_id);
66  header->proto = ODPH_IPPROTO_UDP;
67  header->src_addr = odp_cpu_to_be_32(IP_SRC_ADDR);
68  header->dst_addr = odp_cpu_to_be_32(IP_DST_ADDR);
69  ipv4hdr_set_ihl_words(header, header_len_words);
70  ipv4hdr_set_payload_len(header, size - header_len);
71  header->chksum = 0;
72  odph_ipv4_csum_update(result);
73 
74  /* Set the payload */
75  size -= header_len;
76  set_random_payload(result, header_len, size);
77 
78  return result;
79 }
80 
81 void shuffle(odp_packet_t *packets, int num_packets)
82 {
83  int i;
84 
85  if (num_packets <= 1)
86  return;
87 
88  for (i = 0; i < num_packets; ++i) {
89  odp_packet_t tmp = packets[i];
90  int j = rand() % num_packets;
91 
92  packets[i] = packets[j];
93  packets[j] = tmp;
94  }
95 }
96 
97 int packet_memcmp(odp_packet_t a, odp_packet_t b, uint32_t offset_a,
98  uint32_t offset_b, uint32_t length)
99 {
100  uint32_t i = 0;
101  void *data_a, *data_b;
102  uint32_t bytes_remaining_a = 0;
103  uint32_t bytes_remaining_b = 0;
104 
105  while (i < length) {
106  int status;
107  uint32_t bytes_remaining;
108 
109  data_a = odp_packet_offset(a, offset_a + i, &bytes_remaining_a,
110  NULL);
111  data_b = odp_packet_offset(b, offset_b + i, &bytes_remaining_b,
112  NULL);
113  bytes_remaining = min(bytes_remaining_a, bytes_remaining_b);
114  bytes_remaining = min(bytes_remaining, length - i);
115  assert(bytes_remaining != 0 && data_a && data_b);
116 
117  status = memcmp(data_a, data_b, bytes_remaining);
118  if (status != 0)
119  return status;
120  i += bytes_remaining;
121  }
122 
123  return 0;
124 }
odp_u16be_t odp_cpu_to_be_16(uint16_t cpu16)
Convert cpu native uint16_t to 16bit big endian.
uint16_t odp_u16be_t
unsigned 16bit big endian
odp_u32be_t odp_cpu_to_be_32(uint32_t cpu32)
Convert cpu native uint32_t to 32bit big endian.
int odp_packet_l3_offset_set(odp_packet_t pkt, uint32_t offset)
Set layer 3 start offset.
void * odp_packet_data(odp_packet_t pkt)
Packet data pointer.
odp_packet_t odp_packet_alloc(odp_pool_t pool, uint32_t len)
Allocate a packet from a packet pool.
#define ODP_PACKET_INVALID
Invalid packet.
void odp_packet_ts_set(odp_packet_t pkt, odp_time_t timestamp)
Set packet timestamp.
void * odp_packet_offset(odp_packet_t pkt, uint32_t offset, uint32_t *len, odp_packet_seg_t *seg)
Packet offset pointer.
void odp_packet_has_ipv4_set(odp_packet_t pkt, int val)
Set flag for IPv4.
odp_time_t odp_time_global(void)
Current global time.