1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #ifndef _VNET_H 28 #define _VNET_H 29 30 #ifdef __cplusplus 31 extern "C" { 32 #endif 33 34 #include <sys/vnet_res.h> 35 #include <sys/vnet_mailbox.h> 36 #include <sys/modhash.h> 37 #include <net/if.h> 38 #include <sys/mac_client.h> 39 40 #define VNET_SUCCESS (0) /* successful return */ 41 #define VNET_FAILURE (-1) /* unsuccessful return */ 42 43 #define KMEM_FREE(_p) kmem_free((_p), sizeof (*(_p))) 44 45 #define VNET_NTXDS 512 /* power of 2 tx descriptors */ 46 #define VNET_LDCWD_INTERVAL 1000 /* watchdog freq in msec */ 47 #define VNET_LDCWD_TXTIMEOUT 1000 /* tx timeout in msec */ 48 #define VNET_LDC_MTU 64 /* ldc mtu */ 49 50 51 #define IS_BROADCAST(ehp) \ 52 (ether_cmp(&ehp->ether_dhost, ðerbroadcastaddr) == 0) 53 #define IS_MULTICAST(ehp) \ 54 ((ehp->ether_dhost.ether_addr_octet[0] & 01) == 1) 55 56 #define VNET_MATCH_RES(vresp, vnetp) \ 57 (ether_cmp(vresp->local_macaddr, vnetp->curr_macaddr) == 0) 58 59 /* 60 * Flags used to indicate the state of the vnet device and its associated 61 * resources. 62 */ 63 typedef enum vnet_flags { 64 VNET_STOPPED = 0x0, 65 VNET_STARTED = 0x1, 66 VNET_STOPPING = 0x2 67 } vnet_flags_t; 68 69 typedef struct vnet_hio_stats { 70 /* Link Input/Output stats */ 71 uint64_t ipackets; /* # rx packets */ 72 uint64_t ierrors; /* # rx error */ 73 uint64_t opackets; /* # tx packets */ 74 uint64_t oerrors; /* # tx error */ 75 76 /* MIB II variables */ 77 uint64_t rbytes; /* # bytes received */ 78 uint64_t obytes; /* # bytes transmitted */ 79 uint32_t multircv; /* # multicast packets received */ 80 uint32_t multixmt; /* # multicast packets for xmit */ 81 uint32_t brdcstrcv; /* # broadcast packets received */ 82 uint32_t brdcstxmt; /* # broadcast packets for xmit */ 83 uint32_t norcvbuf; /* # rcv packets discarded */ 84 uint32_t noxmtbuf; /* # xmit packets discarded */ 85 } vnet_hio_stats_t; 86 87 typedef struct vnet_hio_kstats { 88 /* Link Input/Output stats */ 89 kstat_named_t ipackets; 90 kstat_named_t ipackets64; 91 kstat_named_t ierrors; 92 kstat_named_t opackets; 93 kstat_named_t opackets64; 94 kstat_named_t oerrors; 95 96 /* required by kstat for MIB II objects(RFC 1213) */ 97 kstat_named_t rbytes; /* MIB - ifInOctets */ 98 kstat_named_t rbytes64; 99 kstat_named_t obytes; /* MIB - ifOutOctets */ 100 kstat_named_t obytes64; 101 kstat_named_t multircv; /* MIB - ifInNUcastPkts */ 102 kstat_named_t multixmt; /* MIB - ifOutNUcastPkts */ 103 kstat_named_t brdcstrcv; /* MIB - ifInNUcastPkts */ 104 kstat_named_t brdcstxmt; /* MIB - ifOutNUcastPkts */ 105 kstat_named_t norcvbuf; /* MIB - ifInDiscards */ 106 kstat_named_t noxmtbuf; /* MIB - ifOutDiscards */ 107 } vnet_hio_kstats_t; 108 109 typedef struct vnet_tx_ring_stats { 110 uint64_t opackets; /* # tx packets */ 111 uint64_t obytes; /* # bytes transmitted */ 112 } vnet_tx_ring_stats_t; 113 114 /* 115 * A vnet resource structure. 116 */ 117 typedef struct vnet_res { 118 struct vnet_res *nextp; /* next resource in the list */ 119 mac_register_t macreg; /* resource's mac_reg */ 120 vio_net_res_type_t type; /* resource type */ 121 ether_addr_t local_macaddr; /* resource's macaddr */ 122 ether_addr_t rem_macaddr; /* resource's remote macaddr */ 123 vnet_flags_t flags; /* resource flags */ 124 uint32_t refcnt; /* reference count */ 125 struct vnet *vnetp; /* back pointer to vnet */ 126 kstat_t *ksp; /* hio kstats */ 127 void *rx_ringp; /* assoc pseudo rx ring */ 128 } vnet_res_t; 129 130 #define VNET_DDS_TASK_ADD_SHARE 0x01 131 #define VNET_DDS_TASK_DEL_SHARE 0x02 132 #define VNET_DDS_TASK_REL_SHARE 0x04 133 134 /* An instance specific DDS structure */ 135 typedef struct vnet_dds_info { 136 kmutex_t lock; /* lock for this structure */ 137 uint8_t task_flags; /* flags for taskq */ 138 uint8_t dds_req_id; /* DDS message request id */ 139 vio_dds_msg_t dmsg; /* Pending DDS message */ 140 dev_info_t *hio_dip; /* Hybrid device's dip */ 141 uint64_t hio_cookie; /* Hybrid device's cookie */ 142 char hio_ifname[LIFNAMSIZ]; /* Hybrid interface name */ 143 ddi_taskq_t *dds_taskqp; /* Taskq's used for DDS */ 144 struct vnet *vnetp; /* Back pointer to vnetp */ 145 } vnet_dds_info_t; 146 147 #define VNET_NFDB_HASH 64 148 149 #define KEY_HASH(key, addr) \ 150 (key = (((uint64_t)(addr[0])) << 40) | \ 151 (((uint64_t)(addr[1])) << 32) | \ 152 (((uint64_t)(addr[2])) << 24) | \ 153 (((uint64_t)(addr[3])) << 16) | \ 154 (((uint64_t)(addr[4])) << 8) | \ 155 ((uint64_t)(addr[5]))); 156 157 158 /* rwlock macros */ 159 #define READ_ENTER(x) rw_enter(x, RW_READER) 160 #define WRITE_ENTER(x) rw_enter(x, RW_WRITER) 161 #define RW_EXIT(x) rw_exit(x) 162 163 #define VLAN_ID_KEY(key) ((mod_hash_key_t)(uintptr_t)(key)) 164 165 typedef enum { 166 AST_init = 0x0, AST_vnet_alloc = 0x1, 167 AST_ring_init = 0x2, AST_vdds_init = 0x4, 168 AST_read_macaddr = 0x8, AST_fdbh_alloc = 0x10, 169 AST_taskq_create = 0x20, AST_vnet_list = 0x40, 170 AST_vgen_init = 0x80, AST_macreg = 0x100, 171 AST_init_mdeg = 0x200 172 } vnet_attach_progress_t; 173 174 #define VNET_NUM_PSEUDO_GROUPS 1 /* # of pseudo ring grps */ 175 #define VNET_NUM_HYBRID_RINGS 2 /* # of Hybrid tx/rx rings */ 176 #define VNET_HYBRID_RXRING_INDEX 1 /* Hybrid rx ring start index */ 177 178 /* 179 * # of Pseudo TX Rings is defined based on the possible 180 * # of TX Hardware Rings from a Hybrid resource. 181 */ 182 #define VNET_NUM_PSEUDO_TXRINGS VNET_NUM_HYBRID_RINGS 183 184 /* 185 * # of Pseudo RX Rings that are reserved and exposed by default. 186 * 1 for LDC resource to vsw + 2 for RX rings of Hybrid resource. 187 */ 188 #define VNET_NUM_PSEUDO_RXRINGS_DEFAULT (VNET_NUM_HYBRID_RINGS + 1) 189 190 /* Pseudo RX Ring States */ 191 typedef enum { 192 VNET_RXRING_FREE = 0x0, /* Free */ 193 VNET_RXRING_INUSE = 0x1, /* In use */ 194 VNET_RXRING_LDC_SERVICE = 0x2, /* Mapped to vswitch */ 195 VNET_RXRING_LDC_GUEST = 0x4, /* Mapped to a peer vnet */ 196 VNET_RXRING_HYBRID = 0x8, /* Mapped to Hybrid resource */ 197 VNET_RXRING_STARTED = 0x10 /* Started */ 198 } vnet_rxring_state_t; 199 200 /* Pseudo TX Ring States */ 201 typedef enum { 202 VNET_TXRING_FREE = 0x0, /* Free */ 203 VNET_TXRING_INUSE = 0x1, /* In use */ 204 VNET_TXRING_SHARED = 0x2, /* Shared among LDCs */ 205 VNET_TXRING_HYBRID = 0x4, /* Shared among LDCs, Hybrid resource */ 206 VNET_TXRING_STARTED = 0x8 /* Started */ 207 } vnet_txring_state_t; 208 209 /* 210 * Psuedo TX Ring 211 */ 212 typedef struct vnet_pseudo_tx_ring { 213 uint_t index; /* ring index */ 214 vnet_txring_state_t state; /* ring state */ 215 void *grp; /* grp associated */ 216 void *vnetp; /* vnet associated */ 217 mac_ring_handle_t handle; /* ring handle in mac layer */ 218 mac_ring_handle_t hw_rh; /* Resource type dependent, internal */ 219 /* ring handle. Hybrid res: ring hdl */ 220 /* of hardware rx ring; LDC res: hdl */ 221 /* to the res itself (vnet_res_t) */ 222 boolean_t woken_up; 223 vnet_tx_ring_stats_t tx_ring_stats; /* ring statistics */ 224 } vnet_pseudo_tx_ring_t; 225 226 /* 227 * Psuedo RX Ring 228 */ 229 typedef struct vnet_pseudo_rx_ring { 230 uint_t index; /* ring index */ 231 vnet_rxring_state_t state; /* ring state */ 232 void *grp; /* grp associated */ 233 void *vnetp; /* vnet associated */ 234 mac_ring_handle_t handle; /* ring handle in mac layer */ 235 mac_ring_handle_t hw_rh; /* Resource type dependent, internal */ 236 /* ring handle. Hybrid res: ring hdl */ 237 /* of hardware tx ring; otherwise */ 238 /* NULL */ 239 uint64_t gen_num; /* Mac layer gen_num */ 240 } vnet_pseudo_rx_ring_t; 241 242 /* 243 * Psuedo TX Ring Group 244 */ 245 typedef struct vnet_pseudo_tx_group { 246 uint_t index; /* group index */ 247 void *vnetp; /* vnet associated */ 248 mac_group_handle_t handle; /* grp handle in mac layer */ 249 uint_t ring_cnt; /* total # of rings in grp */ 250 vnet_pseudo_tx_ring_t *rings; /* array of rings */ 251 kmutex_t flowctl_lock; /* flow control lock */ 252 kcondvar_t flowctl_cv; 253 kthread_t *flowctl_thread; 254 boolean_t flowctl_done; 255 void *tx_notify_handle; /* Tx ring notification */ 256 } vnet_pseudo_tx_group_t; 257 258 /* 259 * Psuedo RX Ring Group 260 */ 261 typedef struct vnet_pseudo_rx_group { 262 krwlock_t lock; /* sync rings access in grp */ 263 int index; /* group index */ 264 void *vnetp; /* vnet this grp belongs to */ 265 mac_group_handle_t handle; /* grp handle in mac layer */ 266 uint_t max_ring_cnt; /* total # of rings in grp */ 267 uint_t ring_cnt; /* # of rings in use */ 268 vnet_pseudo_rx_ring_t *rings; /* array of rings */ 269 } vnet_pseudo_rx_group_t; 270 271 /* 272 * vnet instance state information 273 */ 274 typedef struct vnet { 275 int instance; /* instance # */ 276 dev_info_t *dip; /* dev_info */ 277 uint64_t reg; /* reg prop value */ 278 vnet_attach_progress_t attach_progress; /* attach progress flags */ 279 struct vnet *nextp; /* next in list */ 280 mac_handle_t mh; /* handle to GLDv3 mac module */ 281 uchar_t vendor_addr[ETHERADDRL]; /* orig macadr */ 282 uchar_t curr_macaddr[ETHERADDRL]; /* current macadr */ 283 void *vgenhdl; /* Handle for vgen */ 284 285 uint32_t fdb_nchains; /* # of hash chains in fdbtbl */ 286 mod_hash_t *fdb_hashp; /* forwarding database */ 287 vnet_res_t *vsw_fp; /* cached fdb entry of vsw */ 288 krwlock_t vsw_fp_rw; /* lock to protect vsw_fp */ 289 uint32_t mtu; /* mtu of the device */ 290 291 uint16_t default_vlan_id; /* default vlan id */ 292 uint16_t pvid; /* port vlan id (untagged) */ 293 uint16_t *vids; /* vlan ids (tagged) */ 294 uint16_t nvids; /* # of vids */ 295 296 link_state_t link_state; /* link status */ 297 boolean_t pls_update; /* phys link state update ? */ 298 vnet_flags_t flags; /* interface flags */ 299 vnet_res_t *hio_fp; /* Hybrid IO resource */ 300 vnet_res_t *vres_list; /* Resource list */ 301 vnet_dds_info_t vdds_info; /* DDS related info */ 302 krwlock_t vrwlock; /* Resource list lock */ 303 ddi_taskq_t *taskqp; /* Resource taskq */ 304 305 /* pseudo ring groups */ 306 vnet_pseudo_rx_group_t rx_grp[VNET_NUM_PSEUDO_GROUPS]; 307 vnet_pseudo_tx_group_t tx_grp[VNET_NUM_PSEUDO_GROUPS]; 308 309 vio_net_handle_t hio_vhp; /* HIO resource hdl */ 310 mac_handle_t hio_mh; /* HIO mac hdl */ 311 mac_client_handle_t hio_mch; /* HIO mac client hdl */ 312 mac_unicast_handle_t hio_muh; /* HIO mac unicst hdl */ 313 mac_group_handle_t rx_hwgh; /* HIO rx ring-group hdl */ 314 mac_group_handle_t tx_hwgh; /* HIO tx ring-group hdl */ 315 } vnet_t; 316 317 #ifdef DEBUG 318 /* 319 * debug levels: 320 * DBG_LEVEL1: Function entry/exit tracing 321 * DBG_LEVEL2: Info messages 322 * DBG_LEVEL3: Warning messages 323 * DBG_LEVEL4: Error messages 324 */ 325 326 enum { DBG_LEVEL1 = 0x01, DBG_LEVEL2 = 0x02, DBG_WARN = 0x04, 327 DBG_ERR = 0x08 }; 328 329 #define DBG1(...) do { \ 330 if ((vnet_dbglevel & DBG_LEVEL1) != 0) { \ 331 debug_printf(__func__, __VA_ARGS__); \ 332 } \ 333 _NOTE(CONSTCOND) } while (0) 334 335 #define DBG2(...) do { \ 336 if ((vnet_dbglevel & DBG_LEVEL2) != 0) { \ 337 debug_printf(__func__, __VA_ARGS__); \ 338 } \ 339 _NOTE(CONSTCOND) } while (0) 340 341 #define DWARN(...) do { \ 342 if ((vnet_dbglevel & DBG_WARN) != 0) { \ 343 debug_printf(__func__, __VA_ARGS__); \ 344 } \ 345 _NOTE(CONSTCOND) } while (0) 346 347 #define DERR(...) do { \ 348 if ((vnet_dbglevel & DBG_ERR) != 0) { \ 349 debug_printf(__func__, __VA_ARGS__); \ 350 } \ 351 _NOTE(CONSTCOND) } while (0) 352 353 #else 354 355 #define DBG1(...) if (0) do { } while (0) 356 #define DBG2(...) if (0) do { } while (0) 357 #define DWARN(...) if (0) do { } while (0) 358 #define DERR(...) if (0) do { } while (0) 359 360 #endif 361 362 #ifdef VNET_IOC_DEBUG /* Debug ioctls */ 363 364 #define VNET_FORCE_LINK_DOWN 0x1 365 #define VNET_FORCE_LINK_UP 0x2 366 367 #endif 368 369 #ifdef __cplusplus 370 } 371 #endif 372 373 #endif /* _VNET_H */ 374