OpenNSL API Guide and Reference Manual
example_mirror.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  *
19  * \file example_mirror.c
20  *
21  * \brief OpenNSL example application for port mirroring
22  *
23  * \details Port mirroring application can be used to monitor packets
24  * received/transmitted by a given port.
25  *
26  **********************************************************************/
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sal/driver.h>
31 #include <opennsl/error.h>
32 #include <opennsl/stack.h>
33 #include <opennsl/vlan.h>
34 #include <opennsl/mirror.h>
35 #include <examples/util.h>
36 
37 #define DEFAULT_UNIT 0
38 #define DEFAULT_VLAN 1
39 
40 char example_usage[] =
41 "Syntax: example_mirror mode sport dport \n\r"
42 " \n\r"
43 "Paramaters: \n\r"
44 " mode - 1, to turn on ingress mirroring. \n\r"
45 " 2, to turn on egress mirroring. \n\r"
46 " 3, to turn on ingress and egress mirroring. \n\r"
47 " sport - Port on which mirroring needs to be enabled. \n\r"
48 " dport - Port on which traffic needs to be probed. \n\r"
49 " \n\r"
50 "Example: The following command is used to monitor ingress and egress \n\r"
51 " traffic from port 5 on port 10. \n\r"
52 " example_mirror 3 5 10 \n\r"
53 " \n\r"
54 "Usage Guidelines: None. \n\r";
55 
56 static int example_mirror_setup(int unit, int mode, int source_port,
57  int probe_port);
58 
59 /*****************************************************************/
66 int main(int argc, char *argv[])
67 {
68  int rv = 0;
69  int choice;
70  int mode;
71  int source_port;
72  int probe_port;
73  int unit = DEFAULT_UNIT;
74 
75  if((argc != 4) || ((argc > 1) && (strcmp(argv[1], "--help") == 0))) {
76  printf("%s\n\r", example_usage);
77  return OPENNSL_E_PARAM;
78  }
79 
80  /* Initialize the system. */
82 
83  if(rv != OPENNSL_E_NONE) {
84  printf("\r\nFailed to initialize the system. Error: %s\r\n",
85  opennsl_errmsg(rv));
86  return rv;
87  }
88 
89  /* cold boot initialization commands */
90  rv = example_port_default_config(unit);
91  if (rv != OPENNSL_E_NONE) {
92  printf("\r\nFailed to apply default config on ports, rc = %d (%s).\r\n",
93  rv, opennsl_errmsg(rv));
94  }
95 
96  /* Add ports to default vlan. */
97  printf("Adding ports to default vlan.\r\n");
99  if(rv != OPENNSL_E_NONE) {
100  printf("\r\nFailed to add default ports. Error: %s\r\n",
101  opennsl_errmsg(rv));
102  }
103 
104  /* Extract input parameters */
105  mode = atoi(argv[1]);
106  source_port = atoi(argv[2]);
107  probe_port = atoi(argv[3]);
108 
109  if(mode < 1 || mode > 3) {
110  printf("Invalid mode is entered.\r\n");
111  return OPENNSL_E_PARAM;
112  }
113  printf("\r\nInput parameters: Mode %d, Source Port %d,"
114  " Probe Port %d.\r\n", mode, source_port, probe_port);
115 
116  /* Setup port mirroring */
117  rv = example_mirror_setup(unit, mode, source_port, probe_port);
118  if(rv != OPENNSL_E_NONE) {
119  printf("\r\nFailed to set port mirroring. Error: %s.\r\n",
120  opennsl_errmsg(rv));
121  }
122  else {
123  printf("\r\nPort %d is mirrored to port %d successfully.\r\n",
124  source_port, probe_port);
125  }
126 
127  while(1) {
128  printf("\r\nUser menu: Select one of the following options\r\n");
129 #ifdef INCLUDE_DIAG_SHELL
130  printf("9. Launch diagnostic shell\n");
131 #endif
132  printf("0. Quit the application.\r\n");
133 
135  {
136  printf("Invalid option entered. Please re-enter.\n");
137  continue;
138  }
139 
140  switch(choice)
141  {
142 #ifdef INCLUDE_DIAG_SHELL
143  case 9:
144  {
145  opennsl_driver_shell();
146  break;
147  }
148 #endif
149 
150  case 0:
151  {
152  printf("Exiting the application.\n");
153  rv = opennsl_driver_exit();
154  return rv;
155  }
156  default:
157  break;
158  } /* End of switch */
159  }
160 
161  return rv;
162 }
163 /* __doxy_func_body_end__ */
164 
165 /**************************************************************************/
175 static int example_mirror_setup(int unit, int mode, int source_port,
176  int probe_port)
177 {
178  int rv;
179  opennsl_gport_t gp_source_port;
180  unsigned int flags = 0;
181  opennsl_mirror_destination_t mirror_dest;
182 
183  rv = opennsl_mirror_init(unit);
184  if(rv != OPENNSL_E_NONE) {
185  printf("\r\nFailed to init mirror module. Error: %s\r\n",
186  opennsl_errmsg(rv));
187  }
188 
189  opennsl_mirror_destination_t_init(&mirror_dest);
190  OPENNSL_GPORT_LOCAL_SET(mirror_dest.gport, probe_port);
191  OPENNSL_GPORT_LOCAL_SET(gp_source_port, source_port);
192 
193  rv = opennsl_mirror_destination_create(unit, &mirror_dest);
194  if(rv != OPENNSL_E_NONE) {
195  printf("\r\nFailed to create mirror dest. Error: %s\r\n",
196  opennsl_errmsg(rv));
197  }
198 
199  /* Configure the source port and mirror to port along with mirror mode */
200  switch (mode)
201  {
202  case 1:
203  /* Ingress mirroring only */
205  break;
206 
207  case 2:
208  /* Egress mirroring only */
210  break;
211 
212  case 3:
213  /* Both Ingress and Egress mirroring */
215  break;
216 
217  default:
218  break;
219  }
220 
221  rv = opennsl_mirror_port_dest_add(unit, gp_source_port, flags, mirror_dest.mirror_dest_id);
222  if(rv != OPENNSL_E_NONE) {
223  printf("\r\nFailed to enable mirroring on the port. Error: %s\r\n",
224  opennsl_errmsg(rv));
225  }
226 
227  return rv;
228 }
229 /* __doxy_func_body_end__ */