1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #ifndef __ZPERF_INTERNAL_H
7 #define __ZPERF_INTERNAL_H
8 
9 #include <limits.h>
10 #include <zephyr/net/net_ip.h>
11 #include <zephyr/net/zperf.h>
12 #include <zephyr/shell/shell.h>
13 #include <zephyr/sys/__assert.h>
14 
15 #define IP6PREFIX_STR2(s) #s
16 #define IP6PREFIX_STR(p) IP6PREFIX_STR2(p)
17 
18 #define MY_PREFIX_LEN 64
19 #define MY_PREFIX_LEN_STR IP6PREFIX_STR(MY_PREFIX_LEN)
20 
21 /* Note that you can set local endpoint address in config file */
22 #if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_CONFIG_SETTINGS)
23 #define MY_IP6ADDR CONFIG_NET_CONFIG_MY_IPV6_ADDR
24 #define DST_IP6ADDR CONFIG_NET_CONFIG_PEER_IPV6_ADDR
25 #define MY_IP6ADDR_SET
26 #else
27 #define MY_IP6ADDR NULL
28 #define DST_IP6ADDR NULL
29 #endif
30 
31 #if defined(CONFIG_NET_IPV4) && defined(CONFIG_NET_CONFIG_SETTINGS)
32 #define MY_IP4ADDR CONFIG_NET_CONFIG_MY_IPV4_ADDR
33 #define DST_IP4ADDR CONFIG_NET_CONFIG_PEER_IPV4_ADDR
34 #define MY_IP4ADDR_SET
35 #else
36 #define MY_IP4ADDR NULL
37 #define DST_IP4ADDR NULL
38 #endif
39 
40 #define PACKET_SIZE_MAX CONFIG_NET_ZPERF_MAX_PACKET_SIZE
41 
42 #define MY_SRC_PORT 50000
43 #define DEF_PORT 5001
44 #define DEF_PORT_STR STRINGIFY(DEF_PORT)
45 
46 /* Upload defaults */
47 #define DEF_DURATION_SECONDS 1
48 #define DEF_DURATION_SECONDS_STR STRINGIFY(DEF_DURATION_SECONDS)
49 #define DEF_PACKET_SIZE 256
50 #define DEF_PACKET_SIZE_STR STRINGIFY(DEF_PACKET_SIZE)
51 #define DEF_RATE_KBPS 10
52 #define DEF_RATE_KBPS_STR STRINGIFY(DEF_RATE_KBPS)
53 
54 #define ZPERF_VERSION "1.1"
55 
56 struct zperf_udp_datagram {
57 	int32_t id;
58 	uint32_t tv_sec;
59 	uint32_t tv_usec;
60 } __packed;
61 
62 BUILD_ASSERT(sizeof(struct zperf_udp_datagram) <= PACKET_SIZE_MAX, "Invalid PACKET_SIZE_MAX");
63 
64 struct zperf_client_hdr_v1 {
65 	int32_t flags;
66 	int32_t num_of_threads;
67 	int32_t port;
68 	int32_t buffer_len;
69 	int32_t bandwidth;
70 	int32_t num_of_bytes;
71 };
72 
73 struct zperf_server_hdr {
74 	int32_t flags;
75 	int32_t total_len1;
76 	int32_t total_len2;
77 	int32_t stop_sec;
78 	int32_t stop_usec;
79 	int32_t error_cnt;
80 	int32_t outorder_cnt;
81 	int32_t datagrams;
82 	int32_t jitter1;
83 	int32_t jitter2;
84 };
85 
86 struct zperf_async_upload_context {
87 	struct k_work work;
88 	struct zperf_upload_params param;
89 	zperf_callback callback;
90 	void *user_data;
91 };
92 
time_delta(uint32_t ts,uint32_t t)93 static inline uint32_t time_delta(uint32_t ts, uint32_t t)
94 {
95 	return (t >= ts) ? (t - ts) : (ULONG_MAX - ts + t);
96 }
97 
98 int zperf_get_ipv6_addr(char *host, char *prefix_str, struct in6_addr *addr);
99 struct sockaddr_in6 *zperf_get_sin6(void);
100 
101 int zperf_get_ipv4_addr(char *host, struct in_addr *addr);
102 struct sockaddr_in *zperf_get_sin(void);
103 
104 extern void connect_ap(char *ssid);
105 
106 int zperf_prepare_upload_sock(const struct sockaddr *peer_addr, uint8_t tos,
107 			      int priority, int tcp_nodelay, int proto);
108 
109 uint32_t zperf_packet_duration(uint32_t packet_size, uint32_t rate_in_kbps);
110 
111 void zperf_async_work_submit(struct k_work *work);
112 void zperf_udp_uploader_init(void);
113 void zperf_tcp_uploader_init(void);
114 
115 void zperf_shell_init(void);
116 
117 #endif /* __ZPERF_INTERNAL_H */
118