aboutsummaryrefslogtreecommitdiffstats
path: root/extras/apps/src/socket_echo_client.c
blob: 4469b03d4c2b819444cf8dc52c013ae5033a7d3b (plain)
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
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <vppinfra/format.h>
#include <sys/time.h>

int
main (int argc, char *argv[])
{
  int sockfd, portno, n;
  struct sockaddr_in serv_addr;
  struct hostent *server;
  u8 *rx_buffer = 0, *tx_buffer = 0, no_echo = 0, test_bytes = 0;
  u32 offset;
  long bytes = 1 << 20, to_send;
  int i;
  struct timeval start, end;
  double deltat;

  if (argc >= 3)
    {
      portno = atoi (argv[2]);
      server = gethostbyname (argv[1]);
      if (server == NULL)
	{
	  clib_unix_warning ("gethostbyname");
	  exit (1);
	}

      argc -= 3;
      argv += 3;

      if (argc)
	{
	  bytes = ((long) atoi (argv[0])) << 20;
	  argc--;
	  argv++;
	}
      if (argc)
	{
	  no_echo = atoi (argv[0]);
	  argc--;
	  argv++;
	}
      if (argc)
	{
	  test_bytes = atoi (argv[0]);
	  argc--;
	  argv++;
	}
    }
  else
    {
      portno = 1234;		// atoi(argv[2]);
      server = gethostbyname ("6.0.1.1" /* argv[1] */ );
      if (server == NULL)
	{
	  clib_unix_warning ("gethostbyname");
	  exit (1);
	}
    }

  to_send = bytes;
  sockfd = socket (AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0)
    {
      clib_unix_error ("socket");
      exit (1);
    }

  bzero ((char *) &serv_addr, sizeof (serv_addr));
  serv_addr.sin_family = AF_INET;
  bcopy ((char *) server->h_addr,
	 (char *) &serv_addr.sin_addr.s_addr, server->h_length);
  serv_addr.sin_port = htons (portno);
  if (connect (sockfd, (const void *) &serv_addr, sizeof (serv_addr)) < 0)
    {
      clib_unix_warning ("connect");
      exit (1);
    }

  vec_validate (rx_buffer, 128 << 10);
  vec_validate (tx_buffer, 128 << 10);

  for (i = 0; i < vec_len (tx_buffer); i++)
    tx_buffer[i] = (i + 1) % 0xff;

  /*
   * Send one packet to warm up the RX pipeline
   */
  n = send (sockfd, tx_buffer, vec_len (tx_buffer), 0 /* flags */ );
  if (n != vec_len (tx_buffer))
    {
      clib_unix_warning ("write");
      exit (0);
    }

  gettimeofday (&start, NULL);
  while (bytes > 0)
    {
      /*
       * TX
       */
      n = send (sockfd, tx_buffer, vec_len (tx_buffer), 0 /* flags */ );
      if (n != vec_len (tx_buffer))
	{
	  clib_unix_warning ("write");
	  exit (0);
	}
      bytes -= n;

      if (no_echo)
	continue;

      /*
       * RX
       */

      offset = 0;
      do
	{
	  n = recv (sockfd, rx_buffer + offset,
		    vec_len (rx_buffer) - offset, 0 /* flags */ );
	  if (n < 0)
	    {
	      clib_unix_warning ("read");
	      exit (0);
	    }
	  offset += n;
	}
      while (offset < vec_len (rx_buffer));

      if (test_bytes)
	{
	  for (i = 0; i < vec_len (rx_buffer); i++)
	    {
	      if (rx_buffer[i] != tx_buffer[i])
		{
		  clib_warning ("[%d] read 0x%x not 0x%x", rx_buffer[i],
				tx_buffer[i]);
		  exit (1);
		}
	    }
	}
    }
  close (sockfd);
  gettimeofday (&end, NULL);

  deltat = (end.tv_sec - start.tv_sec);
  deltat += (end.tv_usec - start.tv_usec) / 1000000.0;	// us to ms
  clib_warning ("Finished in %.6f", deltat);
  clib_warning ("%.4f Gbit/second %s", (((f64) to_send * 8.0) / deltat / 1e9),
		no_echo ? "half" : "full");
  return 0;
}


/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
s="n">first_free_elt_uoffset_by_bin[MHEAP_N_BINS]; #else u32 first_free_elt_uoffset_by_bin[MHEAP_N_BINS]; #endif /* Bitmap of non-empty free list bins. */ uword non_empty_free_elt_heads[(MHEAP_N_BINS + BITS (uword) - 1) / BITS (uword)]; mheap_small_object_cache_t small_object_cache; u32 flags; #define MHEAP_FLAG_TRACE (1 << 0) #define MHEAP_FLAG_DISABLE_VM (1 << 1) #define MHEAP_FLAG_THREAD_SAFE (1 << 2) #define MHEAP_FLAG_SMALL_OBJECT_CACHE (1 << 3) #define MHEAP_FLAG_VALIDATE (1 << 4) /* Lock use when MHEAP_FLAG_THREAD_SAFE is set. */ volatile u32 lock; volatile u32 owner_cpu; int recursion_count; /* Number of allocated objects. */ u64 n_elts; /* Maximum size (in bytes) this heap is allowed to grow to. Set to ~0 to grow heap (via vec_resize) arbitrarily. */ u64 max_size; uword vm_alloc_offset_from_header; uword vm_alloc_size; /* Each successful mheap_validate call increments this serial number. Used to debug heap corruption problems. GDB breakpoints can be made conditional on validate_serial. */ u64 validate_serial; mheap_trace_main_t trace_main; mheap_stats_t stats; } mheap_t; always_inline mheap_t * mheap_header (u8 * v) { return vec_aligned_header (v, sizeof (mheap_t), 16); } always_inline u8 * mheap_vector (mheap_t * h) { return vec_aligned_header_end (h, sizeof (mheap_t), 16); } always_inline uword mheap_elt_uoffset (void *v, mheap_elt_t * e) { return (uword) e->user_data - (uword) v; } always_inline mheap_elt_t * mheap_user_pointer_to_elt (void *v) { return v - STRUCT_OFFSET_OF (mheap_elt_t, user_data); } /* For debugging we keep track of offsets for valid objects. We make sure user is not trying to free object with invalid offset. */ always_inline uword mheap_offset_is_valid (void *v, uword uo) { return uo >= MHEAP_ELT_OVERHEAD_BYTES && uo <= vec_len (v); } always_inline mheap_elt_t * mheap_elt_at_uoffset (void *v, uword uo) { ASSERT (mheap_offset_is_valid (v, uo)); return (mheap_elt_t *) (v + uo - STRUCT_OFFSET_OF (mheap_elt_t, user_data)); } always_inline void * mheap_elt_data (void *v, mheap_elt_t * e) { return v + mheap_elt_uoffset (v, e); } always_inline uword mheap_elt_data_bytes (mheap_elt_t * e) { return e->n_user_data * sizeof (e->user_data[0]); } always_inline uword mheap_data_bytes (void *v, uword uo) { mheap_elt_t *e = mheap_elt_at_uoffset (v, uo); return mheap_elt_data_bytes (e); } #define mheap_len(v,d) (mheap_data_bytes((v),(void *) (d) - (void *) (v)) / sizeof ((d)[0])) always_inline mheap_elt_t * mheap_next_elt (mheap_elt_t * e) { ASSERT (e->n_user_data < MHEAP_N_USER_DATA_INVALID); return (mheap_elt_t *) (e->user_data + e->n_user_data); } always_inline mheap_elt_t * mheap_prev_elt (mheap_elt_t * e) { ASSERT (e->prev_n_user_data < MHEAP_N_USER_DATA_INVALID); return ((void *) e - e->prev_n_user_data * sizeof (e->user_data[0]) - MHEAP_ELT_OVERHEAD_BYTES); } /* Exported operations. */ always_inline uword mheap_elts (void *v) { return v ? mheap_header (v)->n_elts : 0; } always_inline uword mheap_max_size (void *v) { return v ? mheap_header (v)->max_size : ~0; } /* Free previously allocated offset. */ void mheap_put (void *v, uword offset); /* Allocate object from mheap. */ void *mheap_get_aligned (void *v, uword size, uword align, uword align_offset, uword * offset_return); #endif /* included_mem_mheap_h */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */