aboutsummaryrefslogtreecommitdiffstats
path: root/src/uri/uri_socket_test.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:
 */
>.add(cmd, **args).get_sw_if_index(err_msg) @staticmethod def create_memif_interface(node, filename, mid, sid, rxq=1, txq=1, role="SLAVE"): """Create Memif interface on the given node. :param node: Given node to create Memif interface on. :param filename: Memif interface socket filename. :param mid: Memif interface ID. :param sid: Socket ID. :param rxq: Number of RX queues; 0 means do not set. :param txq: Number of TX queues; 0 means do not set. :param role: Memif interface role [master=0|slave=1]. Default is master. :type node: dict :type filename: str :type mid: str :type sid: str :type rxq: int :type txq: int :type role: str :returns: SW interface index. :rtype: int :raises ValueError: If command 'create memif' fails. """ role = getattr(MemifRole, role.upper()).value # Create socket Memif._memif_socket_filename_add_del(node, True, filename, sid) # Create memif sw_if_index = Memif._memif_create( node, mid, sid, rxq=rxq, txq=txq, role=role) # Update Topology if_key = Topology.add_new_port(node, 'memif') Topology.update_interface_sw_if_index(node, if_key, sw_if_index) ifc_name = Memif.vpp_get_memif_interface_name(node, sw_if_index) Topology.update_interface_name(node, if_key, ifc_name) ifc_mac = Memif.vpp_get_memif_interface_mac(node, sw_if_index) Topology.update_interface_mac_address(node, if_key, ifc_mac) Topology.update_interface_memif_socket(node, if_key, '/tmp/' + filename) Topology.update_interface_memif_id(node, if_key, mid) Topology.update_interface_memif_role(node, if_key, str(role)) return sw_if_index @staticmethod def show_memif(node): """Show Memif data for the given node. :param node: Given node to show Memif data on. :type node: dict """ Memif._memif_details(node) @staticmethod def show_memif_on_all_duts(nodes): """Show Memif data on all DUTs. :param nodes: Topology nodes. :type nodes: dict """ for node in nodes.values(): if node['type'] == NodeType.DUT: Memif.show_memif(node) @staticmethod def vpp_get_memif_interface_name(node, sw_if_index): """Get Memif interface name from Memif interfaces dump. :param node: DUT node. :param sw_if_index: DUT node. :type node: dict :type sw_if_index: int :returns: Memif interface name, or None if not found. :rtype: str """ details = Memif._memif_details(node) for memif in details: if memif["sw_if_index"] == sw_if_index: return memif["if_name"] return None @staticmethod def vpp_get_memif_interface_mac(node, sw_if_index): """Get Memif interface MAC address from Memif interfaces dump. :param node: DUT node. :param sw_if_index: DUT node. :type node: dict :type sw_if_index: int :returns: Memif interface MAC address, or None if not found. :rtype: str """ details = Memif._memif_details(node) for memif in details: if memif["sw_if_index"] == sw_if_index: return memif["hw_addr"] return None