vlan.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #ifndef __BEN_VLAN_802_1Q_INC__
  2. #define __BEN_VLAN_802_1Q_INC__
  3. #include <linux/if_vlan.h>
  4. #include <linux/u64_stats_sync.h>
  5. #include <linux/list.h>
  6. /**
  7. * struct vlan_priority_tci_mapping - vlan egress priority mappings
  8. * @priority: skb priority
  9. * @vlan_qos: vlan priority: (skb->priority << 13) & 0xE000
  10. * @next: pointer to next struct
  11. */
  12. struct vlan_priority_tci_mapping {
  13. u32 priority;
  14. u16 vlan_qos;
  15. struct vlan_priority_tci_mapping *next;
  16. };
  17. /**
  18. * struct vlan_pcpu_stats - VLAN percpu rx/tx stats
  19. * @rx_packets: number of received packets
  20. * @rx_bytes: number of received bytes
  21. * @rx_multicast: number of received multicast packets
  22. * @tx_packets: number of transmitted packets
  23. * @tx_bytes: number of transmitted bytes
  24. * @syncp: synchronization point for 64bit counters
  25. * @rx_errors: number of rx errors
  26. * @tx_dropped: number of tx drops
  27. */
  28. struct vlan_pcpu_stats {
  29. u64 rx_packets;
  30. u64 rx_bytes;
  31. u64 rx_multicast;
  32. u64 tx_packets;
  33. u64 tx_bytes;
  34. struct u64_stats_sync syncp;
  35. u32 rx_errors;
  36. u32 tx_dropped;
  37. };
  38. struct netpoll;
  39. /**
  40. * struct vlan_dev_priv - VLAN private device data
  41. * @nr_ingress_mappings: number of ingress priority mappings
  42. * @ingress_priority_map: ingress priority mappings
  43. * @nr_egress_mappings: number of egress priority mappings
  44. * @egress_priority_map: hash of egress priority mappings
  45. * @vlan_proto: VLAN encapsulation protocol
  46. * @vlan_id: VLAN identifier
  47. * @flags: device flags
  48. * @real_dev: underlying netdevice
  49. * @real_dev_addr: address of underlying netdevice
  50. * @dent: proc dir entry
  51. * @vlan_pcpu_stats: ptr to percpu rx stats
  52. */
  53. struct vlan_dev_priv {
  54. unsigned int nr_ingress_mappings;
  55. u32 ingress_priority_map[8];
  56. unsigned int nr_egress_mappings;
  57. struct vlan_priority_tci_mapping *egress_priority_map[16];
  58. __be16 vlan_proto;
  59. u16 vlan_id;
  60. u16 flags;
  61. struct net_device *real_dev;
  62. unsigned char real_dev_addr[ETH_ALEN];
  63. struct proc_dir_entry *dent;
  64. struct vlan_pcpu_stats __percpu *vlan_pcpu_stats;
  65. #ifdef CONFIG_NET_POLL_CONTROLLER
  66. struct netpoll *netpoll;
  67. #endif
  68. };
  69. static inline struct vlan_dev_priv *vlan_dev_priv(const struct net_device *dev)
  70. {
  71. return netdev_priv(dev);
  72. }
  73. /* if this changes, algorithm will have to be reworked because this
  74. * depends on completely exhausting the VLAN identifier space. Thus
  75. * it gives constant time look-up, but in many cases it wastes memory.
  76. */
  77. #define VLAN_GROUP_ARRAY_SPLIT_PARTS 8
  78. #define VLAN_GROUP_ARRAY_PART_LEN (VLAN_N_VID/VLAN_GROUP_ARRAY_SPLIT_PARTS)
  79. enum vlan_protos {
  80. VLAN_PROTO_8021Q = 0,
  81. VLAN_PROTO_8021AD,
  82. VLAN_PROTO_NUM,
  83. };
  84. struct vlan_group {
  85. unsigned int nr_vlan_devs;
  86. struct hlist_node hlist; /* linked list */
  87. struct net_device **vlan_devices_arrays[VLAN_PROTO_NUM]
  88. [VLAN_GROUP_ARRAY_SPLIT_PARTS];
  89. };
  90. struct vlan_info {
  91. struct net_device *real_dev; /* The ethernet(like) device
  92. * the vlan is attached to.
  93. */
  94. struct vlan_group grp;
  95. struct list_head vid_list;
  96. unsigned int nr_vids;
  97. struct rcu_head rcu;
  98. };
  99. static inline unsigned int vlan_proto_idx(__be16 proto)
  100. {
  101. switch (proto) {
  102. case __constant_htons(ETH_P_8021Q):
  103. return VLAN_PROTO_8021Q;
  104. case __constant_htons(ETH_P_8021AD):
  105. return VLAN_PROTO_8021AD;
  106. default:
  107. BUG();
  108. return 0;
  109. }
  110. }
  111. static inline struct net_device *__vlan_group_get_device(struct vlan_group *vg,
  112. unsigned int pidx,
  113. u16 vlan_id)
  114. {
  115. struct net_device **array;
  116. array = vg->vlan_devices_arrays[pidx]
  117. [vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
  118. return array ? array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN] : NULL;
  119. }
  120. static inline struct net_device *vlan_group_get_device(struct vlan_group *vg,
  121. __be16 vlan_proto,
  122. u16 vlan_id)
  123. {
  124. return __vlan_group_get_device(vg, vlan_proto_idx(vlan_proto), vlan_id);
  125. }
  126. static inline void vlan_group_set_device(struct vlan_group *vg,
  127. __be16 vlan_proto, u16 vlan_id,
  128. struct net_device *dev)
  129. {
  130. struct net_device **array;
  131. if (!vg)
  132. return;
  133. array = vg->vlan_devices_arrays[vlan_proto_idx(vlan_proto)]
  134. [vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
  135. array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN] = dev;
  136. }
  137. /* Must be invoked with rcu_read_lock or with RTNL. */
  138. static inline struct net_device *vlan_find_dev(struct net_device *real_dev,
  139. __be16 vlan_proto, u16 vlan_id)
  140. {
  141. struct vlan_info *vlan_info = rcu_dereference_rtnl(real_dev->vlan_info);
  142. if (vlan_info)
  143. return vlan_group_get_device(&vlan_info->grp,
  144. vlan_proto, vlan_id);
  145. return NULL;
  146. }
  147. #define vlan_group_for_each_dev(grp, i, dev) \
  148. for ((i) = 0; i < VLAN_PROTO_NUM * VLAN_N_VID; i++) \
  149. if (((dev) = __vlan_group_get_device((grp), (i) / VLAN_N_VID, \
  150. (i) % VLAN_N_VID)))
  151. /* found in vlan_dev.c */
  152. void vlan_dev_set_ingress_priority(const struct net_device *dev,
  153. u32 skb_prio, u16 vlan_prio);
  154. int vlan_dev_set_egress_priority(const struct net_device *dev,
  155. u32 skb_prio, u16 vlan_prio);
  156. int vlan_dev_change_flags(const struct net_device *dev, u32 flag, u32 mask);
  157. void vlan_dev_get_realdev_name(const struct net_device *dev, char *result);
  158. int vlan_check_real_dev(struct net_device *real_dev,
  159. __be16 protocol, u16 vlan_id);
  160. void vlan_setup(struct net_device *dev);
  161. int register_vlan_dev(struct net_device *dev);
  162. void unregister_vlan_dev(struct net_device *dev, struct list_head *head);
  163. static inline u32 vlan_get_ingress_priority(struct net_device *dev,
  164. u16 vlan_tci)
  165. {
  166. struct vlan_dev_priv *vip = vlan_dev_priv(dev);
  167. return vip->ingress_priority_map[(vlan_tci >> VLAN_PRIO_SHIFT) & 0x7];
  168. }
  169. #ifdef CONFIG_VLAN_8021Q_GVRP
  170. extern int vlan_gvrp_request_join(const struct net_device *dev);
  171. extern void vlan_gvrp_request_leave(const struct net_device *dev);
  172. extern int vlan_gvrp_init_applicant(struct net_device *dev);
  173. extern void vlan_gvrp_uninit_applicant(struct net_device *dev);
  174. extern int vlan_gvrp_init(void);
  175. extern void vlan_gvrp_uninit(void);
  176. #else
  177. static inline int vlan_gvrp_request_join(const struct net_device *dev) { return 0; }
  178. static inline void vlan_gvrp_request_leave(const struct net_device *dev) {}
  179. static inline int vlan_gvrp_init_applicant(struct net_device *dev) { return 0; }
  180. static inline void vlan_gvrp_uninit_applicant(struct net_device *dev) {}
  181. static inline int vlan_gvrp_init(void) { return 0; }
  182. static inline void vlan_gvrp_uninit(void) {}
  183. #endif
  184. #ifdef CONFIG_VLAN_8021Q_MVRP
  185. extern int vlan_mvrp_request_join(const struct net_device *dev);
  186. extern void vlan_mvrp_request_leave(const struct net_device *dev);
  187. extern int vlan_mvrp_init_applicant(struct net_device *dev);
  188. extern void vlan_mvrp_uninit_applicant(struct net_device *dev);
  189. extern int vlan_mvrp_init(void);
  190. extern void vlan_mvrp_uninit(void);
  191. #else
  192. static inline int vlan_mvrp_request_join(const struct net_device *dev) { return 0; }
  193. static inline void vlan_mvrp_request_leave(const struct net_device *dev) {}
  194. static inline int vlan_mvrp_init_applicant(struct net_device *dev) { return 0; }
  195. static inline void vlan_mvrp_uninit_applicant(struct net_device *dev) {}
  196. static inline int vlan_mvrp_init(void) { return 0; }
  197. static inline void vlan_mvrp_uninit(void) {}
  198. #endif
  199. extern const char vlan_fullname[];
  200. extern const char vlan_version[];
  201. extern int vlan_netlink_init(void);
  202. extern void vlan_netlink_fini(void);
  203. extern struct rtnl_link_ops vlan_link_ops;
  204. extern int vlan_net_id;
  205. struct proc_dir_entry;
  206. struct vlan_net {
  207. /* /proc/net/vlan */
  208. struct proc_dir_entry *proc_vlan_dir;
  209. /* /proc/net/vlan/config */
  210. struct proc_dir_entry *proc_vlan_conf;
  211. /* Determines interface naming scheme. */
  212. unsigned short name_type;
  213. };
  214. #endif /* !(__BEN_VLAN_802_1Q_INC__) */