OpenNSL API Guide and Reference Manual
util.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 util.c
19  *
20  * \brief OpenNSL utility routines required for example applications
21  *
22  * \details OpenNSL utility routines required for example applications
23  *
24  ************************************************************************/
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sal/driver.h>
30 #include <opennsl/error.h>
31 #include <opennsl/init.h>
32 #include <opennsl/l2.h>
33 #include <opennsl/vlan.h>
34 #include <opennsl/stg.h>
35 #include <opennsl/link.h>
36 #include <examples/util.h>
37 
38 #define DEFAULT_VLAN 1
39 #define MAX_DIGITS_IN_CHOICE 5
40 
41 /*************************************************************************/
51 int example_is_dnx_device(int unit)
52 {
53  int rv = FALSE;
54  opennsl_info_t info;
55  opennsl_info_get(unit, &info);
56 
57  if(info.device == 0x8375) /* Qumran MX */
58  {
59  rv = TRUE;
60  }
61  return rv;
62 }
63 /* __doxy_func_body_end__ */
64 
65 /*************************************************************************/
75 int example_is_qmx_device(int unit)
76 {
77  int rv = FALSE;
78  opennsl_info_t info;
79  opennsl_info_get(unit, &info);
80 
81  if(info.device == 0x8375) /* Qumran MX */
82  {
83  rv = TRUE;
84  }
85  return rv;
86 }
87 /* __doxy_func_body_end__ */
88 
89 /*****************************************************************/
98 {
100  opennsl_port_info_t info;
101  int rv;
102  int port;
103  int stp_state = OPENNSL_STG_STP_FORWARD;
104  int stg = 1;
105  int dnx_device = FALSE;
106 
107  dnx_device = example_is_dnx_device(unit);
108  /*
109  * Create VLAN with id DEFAULT_VLAN and
110  * add ethernet ports to the VLAN
111  */
113 
114  rv = opennsl_port_config_get(unit, &pcfg);
115  if (rv != OPENNSL_E_NONE)
116  {
117  printf("Failed to get port configuration. Error %s\n", opennsl_errmsg(rv));
118  return rv;
119  }
120 
121  /* Set the STP state to forward in default STG for all ports */
122  OPENNSL_PBMP_ITER(pcfg.e, port)
123  {
124  rv = opennsl_stg_stp_set(unit, stg, port, stp_state);
125  if (rv != OPENNSL_E_NONE)
126  {
127  printf("Failed to set STP state for unit %d port %d, Error %s\n",
128  unit, port, opennsl_errmsg(rv));
129  return rv;
130  }
131  }
132 
133  /* Setup default configuration on the ports */
135 
136  if(dnx_device == FALSE)
137  {
138  info.speed = 0;
139  }
144  info.autoneg = FALSE;
145  info.enable = 1;
146 
153 
154  if(dnx_device == FALSE)
155  {
157  }
158  OPENNSL_PBMP_ITER(pcfg.e, port)
159  {
160  rv = opennsl_port_selective_set(unit, port, &info);
161  if (OPENNSL_FAILURE(rv))
162  {
163  printf("Failed to set port config for unit %d, port %d, Error %s",
164  unit, port, opennsl_errmsg(rv));
165  return rv;
166  }
167  }
168 
169  return OPENNSL_E_NONE;
170 }
171 /* __doxy_func_body_end__ */
172 
173 /*****************************************************************/
181 {
183  int port;
184  int rv;
185 
186  /*
187  * Create VLAN with id DEFAULT_VLAN and
188  * add ethernet ports to the VLAN
189  */
190  rv = opennsl_port_config_get(unit, &pcfg);
191  if (rv != OPENNSL_E_NONE) {
192  printf("Failed to get port configuration. Error %s\n", opennsl_errmsg(rv));
193  return rv;
194  }
195 
196  rv = opennsl_vlan_port_add(unit, DEFAULT_VLAN, pcfg.e, pcfg.e);
197  if (rv != OPENNSL_E_NONE) {
198  printf("Failed to add ports to VLAN. Error %s\n", opennsl_errmsg(rv));
199  return rv;
200  }
201 
202  OPENNSL_PBMP_ITER(pcfg.e, port)
203  {
205  if (OPENNSL_FAILURE(rv))
206  {
207  printf("Failed to set port untagged VLAN for unit %d, port %d, Error %s",
208  unit, port, opennsl_errmsg(rv));
209  return rv;
210  }
211  }
212 
213  return OPENNSL_E_NONE;
214 }
215 /* __doxy_func_body_end__ */
216 
217 /**************************************************************************/
225 char *example_read_user_string(char *buf, size_t buflen)
226 {
227  int ch;
228 
229  if (fgets(buf, buflen, stdin) != 0)
230  {
231  size_t len = strlen(buf);
232  if (len > 0 && buf[len-1] == '\n')
233  {
234  buf[len-1] = '\0';
235  }
236  else
237  {
238  while ((ch = getc(stdin)) != EOF && ch != '\n');
239  }
240  return buf;
241  }
242  return 0;
243 }
244 /* __doxy_func_body_end__ */
245 /**************************************************************************/
252 int example_read_user_choice(int *choice)
253 {
254  char val;
255  char digits[MAX_DIGITS_IN_CHOICE + 1];
256  int idx = 0;
257  int valid = TRUE;
258 
259  /* parse input string until \n */
260  while((val = getchar()) != '\n')
261  {
262  if ((val >= '0' && val <= '9') && idx < MAX_DIGITS_IN_CHOICE)
263  {
264  digits[idx++] = val;
265  }
266  else
267  {
268  valid = FALSE;
269  }
270  }
271  if ((valid == TRUE) && idx != 0)
272  {
273  digits[idx] = '\0';
274  *choice = atoi(digits);
275  return(OPENNSL_E_NONE);
276  }
277  else
278  {
279  *choice = -1;
280  return(OPENNSL_E_FAIL);
281  }
282 }
283 /* __doxy_func_body_end__ */
284 
285 /*****************************************************************/
293 int opennsl_mac_parse(char *buf, unsigned char *macp)
294 {
295  int i, c1, c2;
296  char *s;
297 #define MAC_ADDR_LEN 17
298 
299  macp[0] = macp[1] = macp[2] = 0;
300  macp[3] = macp[4] = macp[5] = 0;
301 
302  if ((buf == NULL) || (strlen(buf) > MAC_ADDR_LEN)) {
303  return OPENNSL_E_FAIL;
304  }
305 
306  /* skip leading 0x if plain hex format */
307  if (buf[0] == '0' && (buf[1] == 'x' || buf[1] == 'X')) {
308  buf += 2;
309  }
310 
311  /* start at end of string and work backwards */
312  for (s = buf; *s; s++) {
313  ;
314  }
315  for (i = 5; i >= 0 && s >= buf; i--) {
316  c1 = c2 = 0;
317  if (--s >= buf) {
318  if (*s >= '0' && *s <= '9') {
319  c2 = *s - '0';
320  } else if (*s >= 'a' && *s <= 'f') {
321  c2 = *s - 'a' + 10;
322  } else if (*s >= 'A' && *s <= 'F') {
323  c2 = *s - 'A' + 10;
324  } else if (*s == ':') {
325  ;
326  } else {
327  return OPENNSL_E_FAIL;
328  }
329  }
330  if (*s != ':' && --s >= buf) {
331  if (*s >= '0' && *s <= '9') {
332  c1 = *s - '0';
333  } else if (*s >= 'a' && *s <= 'f') {
334  c1 = *s - 'a' + 10;
335  } else if (*s >= 'A' && *s <= 'F') {
336  c1 = *s - 'A' + 10;
337  } else if (*s == ':') {
338  ;
339  } else {
340  return OPENNSL_E_FAIL;
341  }
342  }
343 
344  if (s > buf && s[-1] == ':') {
345  --s;
346  }
347  macp[i] = c1 << 4 | c2;
348  }
349  return OPENNSL_E_NONE;
350 }
351 /* __doxy_func_body_end__ */
352 
353 /*****************************************************************/
358 void l2_print_mac(char *str, opennsl_mac_t mac_address){
359  unsigned int a,b,c,d,e,f;
360  a = 0xff & mac_address[0];
361  b = 0xff & mac_address[1];
362  c = 0xff & mac_address[2];
363  d = 0xff & mac_address[3];
364  e = 0xff & mac_address[4];
365  f = 0xff & mac_address[5];
366  printf("%s %02x:%02x:%02x:%02x:%02x:%02x",
367  str,
368  a,b,c,
369  d,e,f);
370  return;
371 }
372 /* __doxy_func_body_end__ */
373 
374 /*****************************************************************/
382 int opennsl_ip_parse(char *ip_str, unsigned int *ip_val)
383 {
384  unsigned int num = 0, val;
385  char *tok;
386  int count = 0;
387  char buf[16]; /* Maximum length of IP address in dotten notation */
388 
389  if((ip_str == NULL) || (ip_val == NULL) || (strlen(ip_str) > 16))
390  {
391  return -1;
392  }
393  strcpy(buf, ip_str);
394  tok = strtok(buf, ".");
395  while(tok != NULL)
396  {
397  count++;
398  val = atoi(tok);
399  if((val < 0) || (val > 0xff))
400  {
401  return -1;
402  }
403  num = (num << 8) + val;
404  tok = strtok(NULL, ".");
405  }
406  if(count != 4)
407  {
408  return -1;
409  }
410  *ip_val = num;
411 
412  return 0;
413 }
414 /* __doxy_func_body_end__ */
415 
416 /**************************************************************************/
425 void print_ip_addr(char *str, unsigned int host)
426 {
427  int a,b,c,d;
428 
429  a = (host >> 24) & 0xff;
430  b = (host >> 16) & 0xff;
431  c = (host >> 8 ) & 0xff;
432  d = host & 0xff;
433  printf("%s %d.%d.%d.%d", str, a,b,c,d);
434 }
435 /* __doxy_func_body_end__ */
436 
437 /**************************************************************************/
446 int opennsl_ctoi(const char *s, char **end)
447 {
448  unsigned int n, neg;
449  int base = 10;
450 
451  if (s == 0) {
452  if (end != 0) {
453  end = 0;
454  }
455  return 0;
456  }
457 
458  s += (neg = (*s == '-'));
459 
460  if (*s == '0') {
461  s++;
462  if (*s == 'x' || *s == 'X') {
463  base = 16;
464  s++;
465  } else if (*s == 'b' || *s == 'B') {
466  base = 2;
467  s++;
468  } else {
469  base = 8;
470  }
471  }
472 
473  for (n = 0; ((*s >= 'a' && *s < 'a' + base - 10) ||
474  (*s >= 'A' && *s < 'A' + base - 10) ||
475  (*s >= '0' && *s <= '9')); s++) {
476  n = n * base + ((*s <= '9' ? *s : *s + 9) & 15);
477  }
478 
479  if (end != 0) {
480  *end = (char *) s;
481  }
482 
483  return (int) (neg ? -n : n);
484 }
485 /* __doxy_func_body_end__ */
486 
487 /**************************************************************************/
495 int example_max_port_count_get(int unit, int *count)
496 {
497  int rc;
499  int num_ports;
500  int num_front_panel_ports;
501 
502  rc = opennsl_port_config_get(unit, &pcfg);
503  if (rc != OPENNSL_E_NONE) {
504  printf("Failed to get port configuration. Error %s\n", opennsl_errmsg(rc));
505  return rc;
506  }
507 
508  OPENNSL_PBMP_COUNT(pcfg.ge, num_ports);
509  num_front_panel_ports = num_ports;
510  OPENNSL_PBMP_COUNT(pcfg.xe, num_ports);
511  num_front_panel_ports += num_ports;
512 
513  *count = num_front_panel_ports;
514  return rc;
515 }
516 /* __doxy_func_body_end__ */
517