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 srcPort, int probePort);
57 
58 /*****************************************************************/
65 int main(int argc, char *argv[])
66 {
67  int rv = 0;
68  int choice;
69  int mode;
70  int srcPort;
71  int probePort;
72  int unit = DEFAULT_UNIT;
73 
74  if((argc != 4) || ((argc > 1) && (strcmp(argv[1], "--help") == 0))) {
75  printf("%s\n\r", example_usage);
76  return OPENNSL_E_PARAM;
77  }
78 
79  /* Initialize the system. */
81 
82  if(rv != OPENNSL_E_NONE) {
83  printf("\r\nFailed to initialize the system. Error: %s\r\n",
84  opennsl_errmsg(rv));
85  return rv;
86  }
87 
88  /* cold boot initialization commands */
89  rv = example_port_default_config(unit);
90  if (rv != OPENNSL_E_NONE) {
91  printf("\r\nFailed to apply default config on ports, rc = %d (%s).\r\n",
92  rv, opennsl_errmsg(rv));
93  }
94 
95  /* Add ports to default vlan. */
96  printf("Adding ports to default vlan.\r\n");
98  if(rv != OPENNSL_E_NONE) {
99  printf("\r\nFailed to add default ports. Error: %s\r\n",
100  opennsl_errmsg(rv));
101  }
102 
103  /* Extract input parameters */
104  mode = atoi(argv[1]);
105  srcPort = atoi(argv[2]);
106  probePort = atoi(argv[3]);
107 
108  if(mode < 1 || mode > 3) {
109  printf("Invalid mode is entered.\r\n");
110  return OPENNSL_E_PARAM;
111  }
112  printf("\r\nInput parameters: Mode %d, Source Port %d,"
113  " Probe Port %d.\r\n", mode, srcPort, probePort);
114 
115  /* Setup port mirroring */
116  rv = example_mirror_setup(unit, mode, srcPort, probePort);
117  if(rv != OPENNSL_E_NONE) {
118  printf("\r\nFailed to set port mirroring. Error: %s.\r\n",
119  opennsl_errmsg(rv));
120  }
121  else {
122  printf("\r\nPort %d is mirrored to port %d successfully.\r\n",
123  srcPort, probePort);
124  }
125 
126  while(1) {
127  printf("\r\nUser menu: Select one of the following options\r\n");
128 #ifdef INCLUDE_DIAG_SHELL
129  printf("9. Launch diagnostic shell\n");
130 #endif
131  printf("0. Quit the application.\r\n");
132 
134  {
135  printf("Invalid option entered. Please re-enter.\n");
136  continue;
137  }
138 
139  switch(choice)
140  {
141 #ifdef INCLUDE_DIAG_SHELL
142  case 9:
143  {
144  opennsl_driver_shell();
145  break;
146  }
147 #endif
148 
149  case 0:
150  {
151  printf("Exiting the application.\n");
152  rv = opennsl_driver_exit();
153  return rv;
154  }
155  default:
156  break;
157  } /* End of switch */
158  }
159 
160  return rv;
161 }
162 /* __doxy_func_body_end__ */
163 
164 /**************************************************************************/
174 static int example_mirror_setup(int unit, int mode, int srcPort, int probePort)
175 {
176  int rv;
177  opennsl_gport_t gport;
178  unsigned int flags = 0;
179  opennsl_mirror_destination_t mirror_dest;
180 
181  rv = opennsl_mirror_init(unit);
182  if(rv != OPENNSL_E_NONE) {
183  printf("\r\nFailed to init mirror module. Error: %s\r\n",
184  opennsl_errmsg(rv));
185  }
186 
187  opennsl_mirror_destination_t_init(&mirror_dest);
188  OPENNSL_GPORT_MODPORT_SET(gport, 0, probePort);
189  mirror_dest.gport = gport;
190 
191  rv = opennsl_mirror_destination_create(unit, &mirror_dest);
192  if(rv != OPENNSL_E_NONE) {
193  printf("\r\nFailed to create mirror dest. Error: %s\r\n",
194  opennsl_errmsg(rv));
195  }
196 
197  /* Configure the source port and mirror to port along with mirror mode */
198  switch (mode)
199  {
200  case 1:
201  /* Ingress mirroring only */
203  break;
204 
205  case 2:
206  /* Egress mirroring only */
208  break;
209 
210  case 3:
211  /* Both Ingress and Egress mirroring */
213  break;
214 
215  default:
216  break;
217  }
218 
219  rv = opennsl_mirror_port_dest_add(unit, srcPort, flags, mirror_dest.mirror_dest_id);
220  if(rv != OPENNSL_E_NONE) {
221  printf("\r\nFailed to enable mirroring on the port. Error: %s\r\n",
222  opennsl_errmsg(rv));
223  }
224 
225  return rv;
226 }
227 /* __doxy_func_body_end__ */