OpenNSL API Guide and Reference Manual
example_packet_transmit.c
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * (C) Copyright Broadcom Corporation 2013-2017
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  **********************************************************************
18  * \file example_packet_transmit.c
19  *
20  * \brief Example code for packet transmission
21  *
22  * \details This example demonstrates the transmission of a packet
23  * through a specified port.
24  *
25  ************************************************************************/
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sal/driver.h>
31 #include <sal/version.h>
32 #include <opennsl/error.h>
33 #include <opennsl/vlan.h>
34 #include <opennsl/port.h>
35 #include <opennsl/switch.h>
36 #include <opennsl/pkt.h>
37 #include <opennsl/tx.h>
38 #include <opennsl/stack.h>
39 #include <examples/util.h>
40 
41 #define DEFAULT_UNIT 0
42 #define DEFAULT_VLAN 1
43 #define MAX_DIGITS_IN_CHOICE 5
44 
45 char example_usage[] =
46 "Syntax: example_packet_transmit \n\r"
47 " \n\r"
48 "Paramaters: None. \n\r"
49 " \n\r"
50 "Example: The following command is used to demonstrate transmit and \n\r"
51 " packet API's. \n\r"
52 " example_packet_transmit \n\r"
53 " \n\r"
54 "Usage Guidelines: None. \n\r";
55 
56 /*****************************************************************/
64 int example_pkt_dump(uint8 *data, int len)
65 {
66  int i;
67  int line_len = 16;
68 
69  printf ("\n packet dump for len = %d\n", len);
70 
71  for (i = 0; i < len; i++)
72  {
73  if (i % line_len == 0)
74  {
75  if (i !=0) printf("\n");
76  printf("%04x ", i);
77  }
78  printf ("%02x ", (data[i]) & 0xff);
79  }
80 
81  printf ("\n\n");
82  return 0;
83 }
84 /* __doxy_func_body_end__ */
85 
86 
87 /*****************************************************************/
97 {
98  int rv = OPENNSL_E_NONE;
99  int pkt_flags;
100  int pkt_len = 128;
101  int pkt_header_len = 0;
102  uint8 pkt_header[64];
103  uint8 pkt_tagged_data[64] =
104  {
105  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, /* DA */
106  0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, /* SA */
107  0x81, 0x00, 0x00, 0x01, /* TPID + VID */
108  0x08, 0x00, 0x45, 0x10, 0x00, 0x46, 0x00, 0x00, 0x00, 0x00, 0x32, 0x06,
109  0x82, 0xab, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
110  0x03, 0x04, 0x00, 0x04, 0x01, 0x00, 0x56, 0xf1, 0x39, 0x00, 0x00,
111  0x11, 0x22, 0x33, 0x44, 0x55, 0x66
112  };
113  uint8 pkt_buf[128];
114  opennsl_gport_t sysport = 0;
115 
116  pkt_flags = OPENNSL_TX_CRC_ALLOC;
117 
118  opennsl_pkt_t *tx_pkt;
119  rv = opennsl_pkt_alloc(unit, pkt_len, pkt_flags, &tx_pkt);
120  if (rv != OPENNSL_E_NONE)
121  {
122  printf("Failed to allocate pkt memory, rv %s\n", opennsl_errmsg(rv));
123  return rv;
124  }
125 
126  rv = opennsl_stk_gport_sysport_get(unit, port, &sysport);
127  if (rv != OPENNSL_E_NONE)
128  {
129  printf("Failed to get system port for unit %d port %d, rv %s\n", unit, port, opennsl_errmsg(rv));
130  return rv;
131  }
132 
133  void * cookie = (void *)1;
134  tx_pkt->call_back = 0;
135  tx_pkt->blk_count = 1;
136  tx_pkt->unit = 0;
137 
138  /* PTCH */
139  pkt_header[pkt_header_len++] = 0x50;
140  pkt_header[pkt_header_len++] = 0;
141  /* ITMH */
142  pkt_header[pkt_header_len++] = 0x01;
143  pkt_header[pkt_header_len++] = 0x00;
144  pkt_header[pkt_header_len++] = (sysport & 0xff);
145  pkt_header[pkt_header_len++] = 0x00;
146 
147  pkt_len = pkt_header_len + 64;
148  /* VID */
149  pkt_tagged_data[14] = (vlan & 0xff00) >> 8;
150  pkt_tagged_data[15] = (vlan & 0x00ff);
151 
152  memset(pkt_buf, 0, 128);
153  memcpy(pkt_buf, pkt_header, pkt_header_len);
154  memcpy(&pkt_buf[pkt_header_len], pkt_tagged_data, 64);
155  pkt_len = pkt_header_len + 64;
156  opennsl_pkt_memcpy (tx_pkt, 0, pkt_buf, pkt_len);
157 
158  rv = opennsl_tx (unit, tx_pkt, cookie);
159  if (rv != OPENNSL_E_NONE)
160  {
161  printf("Failed to transmit packet, rv %s\n", opennsl_errmsg(rv));
162  return rv;
163  }
164 
165  example_pkt_dump (pkt_buf, pkt_len);
166 
167  rv = opennsl_pkt_free(unit, tx_pkt);
168  if (rv != OPENNSL_E_NONE)
169  {
170  printf("Failed to free up packet memory, rv %s\n", opennsl_errmsg(rv));
171  return rv;
172  }
173 
174  return rv;
175 }
176 /* __doxy_func_body_end__ */
177 
178 /*****************************************************************/
189  opennsl_vlan_t vlan, int count)
190 {
191  int i;
192  int rv;
193 
194  for (i = 0; i < count; i++)
195  {
196  rv = example_pkt_send(unit, port, vlan);
197 
198  if (!OPENNSL_SUCCESS(rv))
199  {
200  printf("Error Sending Packet: %d. Error: %s\n", i + 1, opennsl_errmsg(rv));
201  }
202  else
203  {
204  printf("Transmitted Packet %d\n", i + 1);
205  }
206  }
207 }
208 /* __doxy_func_body_end__ */
209 
210 /*****************************************************************/
217 int main(int argc, char *argv[])
218 {
219  opennsl_error_t rv;
220  int count;
222  opennsl_vlan_t vlan;
223  int unit = DEFAULT_UNIT;
224  int choice;
225  int index = 0;
226 
227  if(strcmp(argv[0], "gdb") == 0)
228  {
229  index = 1;
230  }
231 
232  if((argc != (index + 1)) || ((argc > (index + 1)) && (strcmp(argv[index + 1], "--help") == 0)))
233  {
234  printf("%s\n\r", example_usage);
235  return OPENNSL_E_PARAM;
236  }
237 
238  /* Initialize the system. */
239  printf("Initializing the system.\r\n");
241 
242  if(rv != OPENNSL_E_NONE)
243  {
244  printf("\r\nFailed to initialize the system. Error %s\r\n",
245  opennsl_errmsg(rv));
246  return rv;
247  }
248 
249  /* cold boot initialization commands */
250  rv = example_port_default_config(unit);
251  if (rv != OPENNSL_E_NONE)
252  {
253  printf("\r\nFailed to apply default config on ports, rc = %d (%s).\r\n",
254  rv, opennsl_errmsg(rv));
255  }
256 
257  /* Add ports to default vlan. */
258  printf("Adding ports to default vlan.\r\n");
260  if(rv != OPENNSL_E_NONE)
261  {
262  printf("\r\nFailed to add default ports. rv: %s\r\n", opennsl_errmsg(rv));
263  return rv;
264  }
265 
266  while (1)
267  {
268  printf("\r\nUser menu: Select one of the following options\r\n");
269  printf("1. Transmit a packet\n");
270  printf("2. Display OpenNSL version\n");
271 #ifdef INCLUDE_DIAG_SHELL
272  printf("9. Launch diagnostic shell\n");
273 #endif
274  printf("0. Quit the application\n");
275 
277  {
278  printf("Invalid option entered. Please re-enter.\n");
279  continue;
280  }
281  switch(choice){
282 
283  case 1:
284  {
285  printf("\r\nEnter the port on which packet needs to be transmitted.\r\n");
287  {
288  printf("Invalid option entered. Please re-enter.\n");
289  continue;
290  }
291 
292  printf("\r\nEnter VLAN ID of the packet.\r\n");
294  {
295  printf("Invalid option entered. Please re-enter.\n");
296  continue;
297  }
298  vlan = (opennsl_vlan_t) choice;
299 
300  printf("\r\nEnter number of packets to be sent.\r\n");
302  {
303  printf("Invalid option entered. Please re-enter.\n");
304  continue;
305  }
306 
307  /* Transmit packet(s) on the specified port */
308  example_multi_pkt_send(unit, port, vlan, count);
309 
310  break;
311  } /* End of case 1 */
312 
313  case 2:
314  {
315  printf("OpenNSL version: %s\n", opennsl_version_get());
316  break;
317  }
318 
319 #ifdef INCLUDE_DIAG_SHELL
320  case 9:
321  {
322  opennsl_driver_shell();
323  break;
324  }
325 #endif
326 
327  case 0:
328  {
329  printf("Exiting the application.\n");
330  rv = opennsl_driver_exit();
331  return rv;
332  }
333 
334  default:
335  break;
336  } /* End of switch */
337  } /* End of while */
338 
339  return OPENNSL_E_NONE;
340 }
341 /* __doxy_func_body_end__ */