general_vlan.c (7216B)
1 /* 2 * Feature : VLAN 3 * 4 * Usage : BCM.0> cint general_vlan.c 5 * 6 * config : vlan_config.bcm 7 * 8 * Log file : general_vlan_log.txt 9 * 10 * Test Topology : 11 * +------------------------+ 12 * | | 13 * ingress_port1 | | 14 * +-------------+ | 15 * | SWITCH | 16 * ingress_port2 | | 17 * +-------------+ | 18 * | | 19 * | | 20 * +------------------------+ 21 * 22 * Summary: 23 * ======== 24 * This cint example is to demonstrate L2 VLAN management activities like 25 * add/create/delete vlan using BCM APIs. This script also demonstrates how 26 * to set up default VLAN on the device. 27 * 28 * Detailed steps done in the CINT script: 29 * ======================================= 30 * 1) Step1 - Test Setup (Done in test_setup()): 31 * ============================================= 32 * a) Selects two ingress ports. 33 * 34 * 2) Step2 - Configuration (Done in config_vlan()): 35 * ================================================= 36 * a) Create a vlan 20 and add two ports to the vlan. 37 * 38 * b) Create a vlan 10 and make it new default vlan. 39 * 40 * 3) Step3 - Verification (Done in verify()): 41 * =========================================== 42 * a) Display the vlan configuration using "vlan show" 43 * and display default vlan using "vlan default". 44 * b) Expected Result: 45 * =================== 46 * Default vlan should be 10. 47 * Default Vlan 10 should have all ports as it members. 48 * Vlan 20 should have ingress_port1 and ingressport2 as it members. 49 * 50 * "vlan show" output 51 * ================== 52 * vlan 10 ports cpu,cd,xe (0x0000000000000000000000000000f0000f4000f0000f0000f0000f4000f0001f), untagged cd,xe (0x0000000000000000000000000000f0000f4000f0000f0000f0000f4000f0001e) MCAST_FLOOD_UNKNOWN 53 * vlan 20 ports cd0-cd1 (0x0000000000000000000000000000000000000000000000000000000000000006), untagged none (0x0000000000000000000000000000000000000000000000000000000000000000) MCAST_FLOOD_UNKNOWN 54 * 55 * "vlan default" output 56 * ===================== 57 * Default VLAN ID is 10 58 */ 59 /* Reset c interpreter*/ 60 cint_reset(); 61 bcm_port_t ingress_port1, ingress_port2; 62 63 uint16 defVlan=1; 64 65 /* This function is written so that hardcoding of port 66 numbers in Cint scripts is removed. This function gives 67 required number of ports 68 */ 69 bcm_error_t portNumbersGet(int unit, int *port_list, int num_ports) 70 { 71 72 int i=0,port=0,rv=0; 73 bcm_port_config_t configP; 74 bcm_pbmp_t ports_pbmp; 75 76 rv = bcm_port_config_get(unit,&configP); 77 if(BCM_FAILURE(rv)) { 78 printf("\nError in retrieving port configuration: %s.\n",bcm_errmsg(rv)); 79 return rv; 80 } 81 82 ports_pbmp = configP.e; 83 for (i= 1; i < BCM_PBMP_PORT_MAX; i++) { 84 if (BCM_PBMP_MEMBER(&ports_pbmp,i)&& (port < num_ports)) { 85 port_list[port]=i; 86 port++; 87 } 88 } 89 90 if (( port == 0 ) || ( port != num_ports )) { 91 printf("portNumbersGet() failed \n"); 92 return -1; 93 } 94 95 return BCM_E_NONE; 96 97 } 98 99 /* 100 * This functions gets the port numbers. 101 */ 102 bcm_error_t test_setup(int unit) 103 { 104 int port_list[2], i; 105 106 if (BCM_E_NONE != portNumbersGet(unit, port_list, 2)) { 107 printf("portNumbersGet() failed\n"); 108 return -1; 109 } 110 111 ingress_port1 = port_list[0]; 112 ingress_port2 = port_list[1]; 113 114 return BCM_E_NONE; 115 } 116 117 /* Add a port to tagged and/or untagged vlan bitmap */ 118 bcm_error_t vlan_add_port(int unit, int vid, bcm_port_t tport, bcm_port_t utport) 119 { 120 bcm_pbmp_t pbmp, upbmp; 121 BCM_PBMP_CLEAR(pbmp); 122 BCM_PBMP_CLEAR(upbmp); 123 BCM_PBMP_PORT_ADD(pbmp, tport); 124 BCM_PBMP_PORT_ADD(upbmp, utport); 125 return bcm_vlan_port_add(unit, vid, pbmp, upbmp); 126 } 127 /* Add a gport to vlan */ 128 bcm_error_t vlan_add_gport(int unit, int vid, bcm_port_t port, unsigned int flag) 129 { 130 bcm_gport_t gport; 131 BCM_GPORT_MODPORT_SET(gport, unit, port); 132 return bcm_vlan_gport_add(unit, vid, gport, flag); 133 } 134 /* Make VLAN vlanId1 as a default vlan and delete older default vlan */ 135 bcm_error_t new_default_vlan_setup(int unit, int vid) 136 { 137 bcm_pbmp_t pbmp; 138 bcm_pbmp_t upbmp; 139 int rv; 140 /* Get the port members for default VLAN */ 141 rv = bcm_vlan_port_get(unit,defVlan,pbmp,upbmp); 142 if (BCM_FAILURE(rv)) { 143 printf("Error executing bcm_vlan_port_get(): %s.\n", bcm_errmsg(rv)); 144 return rv; 145 } 146 147 /* Create a vlan with all the valid port bitmap */ 148 rv = bcm_vlan_create(unit, vid); 149 if (BCM_FAILURE(rv)) { 150 printf("Error executing bcm_vlan_create(): %s.\n", bcm_errmsg(rv)); 151 return rv; 152 } 153 154 /* Add ports into VLAN bitmap*/ 155 rv = bcm_vlan_port_add(unit, vid, pbmp, upbmp); 156 if (BCM_FAILURE(rv)) { 157 printf("Error executing bcm_vlan_port_add(): %s.\n", bcm_errmsg(rv)); 158 return rv; 159 } 160 161 /* Make VLAN 10 as a default vlan and delete older default vlan*/ 162 rv = bcm_vlan_default_set(unit,vid); 163 if (BCM_FAILURE(rv)) { 164 printf("Error executing bcm_vlan_default_set(): %s.\n", bcm_errmsg(rv)); 165 return rv; 166 } 167 rv = bcm_vlan_destroy(unit,defVlan); 168 if (BCM_FAILURE(rv)) { 169 printf("Error executing bcm_vlan_destroy(): %s.\n", bcm_errmsg(rv)); 170 return rv; 171 } 172 return BCM_E_NONE; 173 } 174 175 void verify(int unit) 176 { 177 bshell(unit, "vlan show"); 178 bshell(unit, "vlan default"); 179 } 180 181 bcm_error_t config_vlan(int unit) 182 { 183 uint16 vlanId1=10, vlanId2=20; 184 /* Create the vlan with specific id if it is already present then error will be returned */ 185 int rv = bcm_vlan_create(unit, vlanId2); 186 if(BCM_FAILURE(rv)) 187 { 188 printf("\nError in creating VLAN %d : %s.\n", vlanId2, bcm_errmsg(rv)); 189 } 190 191 rv = vlan_add_gport(unit, vlanId2, ingress_port2, 0); 192 if(BCM_FAILURE(rv)) 193 { 194 printf("\nError in function vlan_add_gport: %s.\n", bcm_errmsg(rv)); 195 } 196 197 rv = vlan_add_port(unit, vlanId2, ingress_port1, 0); 198 if(BCM_FAILURE(rv)) 199 { 200 printf("\nError in function vlan_add_port: %s.\n", bcm_errmsg(rv)); 201 } 202 /*Setup vlanId1(10) as default VLAN*/ 203 rv = new_default_vlan_setup(unit, vlanId1); 204 if(BCM_FAILURE(rv)) 205 { 206 printf("\nError while setting new default vlan: %s.\n", bcm_errmsg(rv)); 207 } 208 209 return BCM_E_NONE; 210 } 211 /* 212 * This functions does the following 213 * a)test setup 214 * b)actual configuration (Done in config_vlan()) 215 * c)Displays the configuration (Done in verify()). 216 */ 217 bcm_error_t execute() 218 { 219 bcm_error_t rv; 220 int unit =0; 221 222 bshell(unit, "config show; a ; version"); 223 224 if (BCM_FAILURE((rv = test_setup(unit)))) { 225 printf("test_setup() failed.\n"); 226 return -1; 227 } 228 printf("Completed test setup successfully.\n"); 229 230 if (BCM_FAILURE((rv = config_vlan(unit)))) { 231 printf("vlan_port_flex_ctr() failed.\n"); 232 return -1; 233 } 234 printf("Completed configuration(i.e executing config_vlan()) successfully.\n"); 235 236 verify(unit); 237 return BCM_E_NONE; 238 } 239 240 const char *auto_execute = (ARGC == 1) ? ARGV[0] : "YES"; 241 if (!sal_strcmp(auto_execute, "YES")) { 242 print execute(); 243 } 244