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 #define ZPERF_VERSION "1.1"
47 
48 struct zperf_udp_datagram {
49 	int32_t id;
50 	uint32_t tv_sec;
51 	uint32_t tv_usec;
52 } __packed;
53 
54 BUILD_ASSERT(sizeof(struct zperf_udp_datagram) <= PACKET_SIZE_MAX, "Invalid PACKET_SIZE_MAX");
55 
56 struct zperf_client_hdr_v1 {
57 	int32_t flags;
58 	int32_t num_of_threads;
59 	int32_t port;
60 	int32_t buffer_len;
61 	int32_t bandwidth;
62 	int32_t num_of_bytes;
63 };
64 
65 struct zperf_server_hdr {
66 	int32_t flags;
67 	int32_t total_len1;
68 	int32_t total_len2;
69 	int32_t stop_sec;
70 	int32_t stop_usec;
71 	int32_t error_cnt;
72 	int32_t outorder_cnt;
73 	int32_t datagrams;
74 	int32_t jitter1;
75 	int32_t jitter2;
76 };
77 
78 struct zperf_async_upload_context {
79 	struct k_work work;
80 	struct zperf_upload_params param;
81 	zperf_callback callback;
82 	void *user_data;
83 };
84 
time_delta(uint32_t ts,uint32_t t)85 static inline uint32_t time_delta(uint32_t ts, uint32_t t)
86 {
87 	return (t >= ts) ? (t - ts) : (ULONG_MAX - ts + t);
88 }
89 
90 int zperf_get_ipv6_addr(char *host, char *prefix_str, struct in6_addr *addr);
91 struct sockaddr_in6 *zperf_get_sin6(void);
92 
93 int zperf_get_ipv4_addr(char *host, struct in_addr *addr);
94 struct sockaddr_in *zperf_get_sin(void);
95 
96 extern void connect_ap(char *ssid);
97 
98 const struct in_addr *zperf_get_default_if_in4_addr(void);
99 const struct in6_addr *zperf_get_default_if_in6_addr(void);
100 
101 int zperf_prepare_upload_sock(const struct sockaddr *peer_addr, int tos,
102 			      int priority, int proto);
103 
104 uint32_t zperf_packet_duration(uint32_t packet_size, uint32_t rate_in_kbps);
105 
106 void zperf_async_work_submit(struct k_work *work);
107 void zperf_udp_uploader_init(void);
108 void zperf_tcp_uploader_init(void);
109 void zperf_udp_receiver_init(void);
110 void zperf_tcp_receiver_init(void);
111 
112 void zperf_shell_init(void);
113 
114 #endif /* __ZPERF_INTERNAL_H */
115