vlan.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. }
  109. }
  110. static inline struct net_device *__vlan_group_get_device(struct vlan_group *vg,
  111. unsigned int pidx,
  112. u16 vlan_id)
  113. {
  114. struct net_device **array;
  115. array = vg->vlan_devices_arrays[pidx]
  116. [vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
  117. return array ? array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN] : NULL;
  118. }
  119. static inline struct net_device *vlan_group_get_device(struct vlan_group *vg,
  120. __be16 vlan_proto,
  121. u16 vlan_id)
  122. {
  123. return __vlan_group_get_device(vg, vlan_proto_idx(vlan_proto), vlan_id);
  124. }
  125. static inline void vlan_group_set_device(struct vlan_group *vg,
  126. __be16 vlan_proto, u16 vlan_id,
  127. struct net_device *dev)
  128. {
  129. struct net_device **array;
  130. if (!vg)
  131. return;
  132. array = vg->vlan_devices_arrays[vlan_proto_idx(vlan_proto)]
  133. [vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
  134. array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN] = dev;
  135. }
  136. /* Must be invoked with rcu_read_lock or with RTNL. */
  137. static inline struct net_device *vlan_find_dev(struct net_device *real_dev,
  138. __be16 vlan_proto, u16 vlan_id)
  139. {
  140. struct vlan_info *vlan_info = rcu_dereference_rtnl(real_dev->vlan_info);
  141. if (vlan_info)
  142. return vlan_group_get_device(&vlan_info->grp,
  143. vlan_proto, vlan_id);
  144. return NULL;
  145. }
  146. #define vlan_group_for_each_dev(grp, i, dev) \
  147. for ((i) = 0; i < VLAN_PROTO_NUM * VLAN_N_VID; i++) \
  148. if (((dev) = __vlan_group_get_device((grp), (i) / VLAN_N_VID, \
  149. (i) % VLAN_N_VID)))
  150. /* found in vlan_dev.c */
  151. void vlan_dev_set_ingress_priority(const struct net_device *dev,
  152. u32 skb_prio, u16 vlan_prio);
  153. int vlan_dev_set_egress_priority(const struct net_device *dev,
  154. u32 skb_prio, u16 vlan_prio);
  155. int vlan_dev_change_flags(const struct net_device *dev, u32 flag, u32 mask);
  156. void vlan_dev_get_realdev_name(const struct net_device *dev, char *result);
  157. int vlan_check_real_dev(struct net_device *real_dev,
  158. __be16 protocol, u16 vlan_id);
  159. void vlan_setup(struct net_device *dev);
  160. int register_vlan_dev(struct net_device *dev);
  161. void unregister_vlan_dev(struct net_device *dev, struct list_head *head);
  162. static inline u32 vlan_get_ingress_priority(struct net_device *dev,
  163. u16 vlan_tci)
  164. {
  165. struct vlan_dev_priv *vip = vlan_dev_priv(dev);
  166. return vip->ingress_priority_map[(vlan_tci >> VLAN_PRIO_SHIFT) & 0x7];
  167. }
  168. #ifdef CONFIG_VLAN_8021Q_GVRP
  169. extern int vlan_gvrp_request_join(const struct net_device *dev);
  170. extern void vlan_gvrp_request_leave(const struct net_device *dev);
  171. extern int vlan_gvrp_init_applicant(struct net_device *dev);
  172. extern void vlan_gvrp_uninit_applicant(struct net_device *dev);
  173. extern int vlan_gvrp_init(void);
  174. extern void vlan_gvrp_uninit(void);
  175. #else
  176. static inline int vlan_gvrp_request_join(const struct net_device *dev) { return 0; }
  177. static inline void vlan_gvrp_request_leave(const struct net_device *dev) {}
  178. static inline int vlan_gvrp_init_applicant(struct net_device *dev) { return 0; }
  179. static inline void vlan_gvrp_uninit_applicant(struct net_device *dev) {}
  180. static inline int vlan_gvrp_init(void) { return 0; }
  181. static inline void vlan_gvrp_uninit(void) {}
  182. #endif
  183. #ifdef CONFIG_VLAN_8021Q_MVRP
  184. extern int vlan_mvrp_request_join(const struct net_device *dev);
  185. extern void vlan_mvrp_request_leave(const struct net_device *dev);
  186. extern int vlan_mvrp_init_applicant(struct net_device *dev);
  187. extern void vlan_mvrp_uninit_applicant(struct net_device *dev);
  188. extern int vlan_mvrp_init(void);
  189. extern void vlan_mvrp_uninit(void);
  190. #else
  191. static inline int vlan_mvrp_request_join(const struct net_device *dev) { return 0; }
  192. static inline void vlan_mvrp_request_leave(const struct net_device *dev) {}
  193. static inline int vlan_mvrp_init_applicant(struct net_device *dev) { return 0; }
  194. static inline void vlan_mvrp_uninit_applicant(struct net_device *dev) {}
  195. static inline int vlan_mvrp_init(void) { return 0; }
  196. static inline void vlan_mvrp_uninit(void) {}
  197. #endif
  198. extern const char vlan_fullname[];
  199. extern const char vlan_version[];
  200. extern int vlan_netlink_init(void);
  201. extern void vlan_netlink_fini(void);
  202. extern struct rtnl_link_ops vlan_link_ops;
  203. extern int vlan_net_id;
  204. struct proc_dir_entry;
  205. struct vlan_net {
  206. /* /proc/net/vlan */
  207. struct proc_dir_entry *proc_vlan_dir;
  208. /* /proc/net/vlan/config */
  209. struct proc_dir_entry *proc_vlan_conf;
  210. /* Determines interface naming scheme. */
  211. unsigned short name_type;
  212. };
  213. #endif /* !(__BEN_VLAN_802_1Q_INC__) */