rmnet_vnd.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2013-2018, The Linux Foundation. All rights reserved.
  3. *
  4. * RMNET Data virtual network driver
  5. */
  6. #include <linux/etherdevice.h>
  7. #include <linux/if_arp.h>
  8. #include <net/pkt_sched.h>
  9. #include "rmnet_config.h"
  10. #include "rmnet_handlers.h"
  11. #include "rmnet_private.h"
  12. #include "rmnet_map.h"
  13. #include "rmnet_vnd.h"
  14. /* RX/TX Fixup */
  15. void rmnet_vnd_rx_fixup(struct sk_buff *skb, struct net_device *dev)
  16. {
  17. struct rmnet_priv *priv = netdev_priv(dev);
  18. struct rmnet_pcpu_stats *pcpu_ptr;
  19. pcpu_ptr = this_cpu_ptr(priv->pcpu_stats);
  20. u64_stats_update_begin(&pcpu_ptr->syncp);
  21. pcpu_ptr->stats.rx_pkts++;
  22. pcpu_ptr->stats.rx_bytes += skb->len;
  23. u64_stats_update_end(&pcpu_ptr->syncp);
  24. }
  25. void rmnet_vnd_tx_fixup(struct sk_buff *skb, struct net_device *dev)
  26. {
  27. struct rmnet_priv *priv = netdev_priv(dev);
  28. struct rmnet_pcpu_stats *pcpu_ptr;
  29. pcpu_ptr = this_cpu_ptr(priv->pcpu_stats);
  30. u64_stats_update_begin(&pcpu_ptr->syncp);
  31. pcpu_ptr->stats.tx_pkts++;
  32. pcpu_ptr->stats.tx_bytes += skb->len;
  33. u64_stats_update_end(&pcpu_ptr->syncp);
  34. }
  35. /* Network Device Operations */
  36. static netdev_tx_t rmnet_vnd_start_xmit(struct sk_buff *skb,
  37. struct net_device *dev)
  38. {
  39. struct rmnet_priv *priv;
  40. priv = netdev_priv(dev);
  41. if (priv->real_dev) {
  42. rmnet_egress_handler(skb);
  43. } else {
  44. this_cpu_inc(priv->pcpu_stats->stats.tx_drops);
  45. kfree_skb(skb);
  46. }
  47. return NETDEV_TX_OK;
  48. }
  49. static int rmnet_vnd_change_mtu(struct net_device *rmnet_dev, int new_mtu)
  50. {
  51. if (new_mtu < 0 || new_mtu > RMNET_MAX_PACKET_SIZE)
  52. return -EINVAL;
  53. rmnet_dev->mtu = new_mtu;
  54. return 0;
  55. }
  56. static int rmnet_vnd_get_iflink(const struct net_device *dev)
  57. {
  58. struct rmnet_priv *priv = netdev_priv(dev);
  59. return priv->real_dev->ifindex;
  60. }
  61. static int rmnet_vnd_init(struct net_device *dev)
  62. {
  63. struct rmnet_priv *priv = netdev_priv(dev);
  64. int err;
  65. priv->pcpu_stats = alloc_percpu(struct rmnet_pcpu_stats);
  66. if (!priv->pcpu_stats)
  67. return -ENOMEM;
  68. err = gro_cells_init(&priv->gro_cells, dev);
  69. if (err) {
  70. free_percpu(priv->pcpu_stats);
  71. return err;
  72. }
  73. return 0;
  74. }
  75. static void rmnet_vnd_uninit(struct net_device *dev)
  76. {
  77. struct rmnet_priv *priv = netdev_priv(dev);
  78. gro_cells_destroy(&priv->gro_cells);
  79. free_percpu(priv->pcpu_stats);
  80. }
  81. static void rmnet_get_stats64(struct net_device *dev,
  82. struct rtnl_link_stats64 *s)
  83. {
  84. struct rmnet_priv *priv = netdev_priv(dev);
  85. struct rmnet_vnd_stats total_stats;
  86. struct rmnet_pcpu_stats *pcpu_ptr;
  87. unsigned int cpu, start;
  88. memset(&total_stats, 0, sizeof(struct rmnet_vnd_stats));
  89. for_each_possible_cpu(cpu) {
  90. pcpu_ptr = per_cpu_ptr(priv->pcpu_stats, cpu);
  91. do {
  92. start = u64_stats_fetch_begin_irq(&pcpu_ptr->syncp);
  93. total_stats.rx_pkts += pcpu_ptr->stats.rx_pkts;
  94. total_stats.rx_bytes += pcpu_ptr->stats.rx_bytes;
  95. total_stats.tx_pkts += pcpu_ptr->stats.tx_pkts;
  96. total_stats.tx_bytes += pcpu_ptr->stats.tx_bytes;
  97. } while (u64_stats_fetch_retry_irq(&pcpu_ptr->syncp, start));
  98. total_stats.tx_drops += pcpu_ptr->stats.tx_drops;
  99. }
  100. s->rx_packets = total_stats.rx_pkts;
  101. s->rx_bytes = total_stats.rx_bytes;
  102. s->tx_packets = total_stats.tx_pkts;
  103. s->tx_bytes = total_stats.tx_bytes;
  104. s->tx_dropped = total_stats.tx_drops;
  105. }
  106. static const struct net_device_ops rmnet_vnd_ops = {
  107. .ndo_start_xmit = rmnet_vnd_start_xmit,
  108. .ndo_change_mtu = rmnet_vnd_change_mtu,
  109. .ndo_get_iflink = rmnet_vnd_get_iflink,
  110. .ndo_add_slave = rmnet_add_bridge,
  111. .ndo_del_slave = rmnet_del_bridge,
  112. .ndo_init = rmnet_vnd_init,
  113. .ndo_uninit = rmnet_vnd_uninit,
  114. .ndo_get_stats64 = rmnet_get_stats64,
  115. };
  116. static const char rmnet_gstrings_stats[][ETH_GSTRING_LEN] = {
  117. "Checksum ok",
  118. "Checksum valid bit not set",
  119. "Checksum validation failed",
  120. "Checksum error bad buffer",
  121. "Checksum error bad ip version",
  122. "Checksum error bad transport",
  123. "Checksum skipped on ip fragment",
  124. "Checksum skipped",
  125. "Checksum computed in software",
  126. };
  127. static void rmnet_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
  128. {
  129. switch (stringset) {
  130. case ETH_SS_STATS:
  131. memcpy(buf, &rmnet_gstrings_stats,
  132. sizeof(rmnet_gstrings_stats));
  133. break;
  134. }
  135. }
  136. static int rmnet_get_sset_count(struct net_device *dev, int sset)
  137. {
  138. switch (sset) {
  139. case ETH_SS_STATS:
  140. return ARRAY_SIZE(rmnet_gstrings_stats);
  141. default:
  142. return -EOPNOTSUPP;
  143. }
  144. }
  145. static void rmnet_get_ethtool_stats(struct net_device *dev,
  146. struct ethtool_stats *stats, u64 *data)
  147. {
  148. struct rmnet_priv *priv = netdev_priv(dev);
  149. struct rmnet_priv_stats *st = &priv->stats;
  150. if (!data)
  151. return;
  152. memcpy(data, st, ARRAY_SIZE(rmnet_gstrings_stats) * sizeof(u64));
  153. }
  154. static const struct ethtool_ops rmnet_ethtool_ops = {
  155. .get_ethtool_stats = rmnet_get_ethtool_stats,
  156. .get_strings = rmnet_get_strings,
  157. .get_sset_count = rmnet_get_sset_count,
  158. };
  159. /* Called by kernel whenever a new rmnet<n> device is created. Sets MTU,
  160. * flags, ARP type, needed headroom, etc...
  161. */
  162. void rmnet_vnd_setup(struct net_device *rmnet_dev)
  163. {
  164. rmnet_dev->netdev_ops = &rmnet_vnd_ops;
  165. rmnet_dev->mtu = RMNET_DFLT_PACKET_SIZE;
  166. rmnet_dev->needed_headroom = RMNET_NEEDED_HEADROOM;
  167. eth_random_addr(rmnet_dev->dev_addr);
  168. rmnet_dev->tx_queue_len = RMNET_TX_QUEUE_LEN;
  169. /* Raw IP mode */
  170. rmnet_dev->header_ops = NULL; /* No header */
  171. rmnet_dev->type = ARPHRD_RAWIP;
  172. rmnet_dev->hard_header_len = 0;
  173. rmnet_dev->flags &= ~(IFF_BROADCAST | IFF_MULTICAST);
  174. rmnet_dev->needs_free_netdev = true;
  175. rmnet_dev->ethtool_ops = &rmnet_ethtool_ops;
  176. /* This perm addr will be used as interface identifier by IPv6 */
  177. rmnet_dev->addr_assign_type = NET_ADDR_RANDOM;
  178. eth_random_addr(rmnet_dev->perm_addr);
  179. }
  180. /* Exposed API */
  181. int rmnet_vnd_newlink(u8 id, struct net_device *rmnet_dev,
  182. struct rmnet_port *port,
  183. struct net_device *real_dev,
  184. struct rmnet_endpoint *ep)
  185. {
  186. struct rmnet_priv *priv = netdev_priv(rmnet_dev);
  187. int rc;
  188. if (ep->egress_dev)
  189. return -EINVAL;
  190. if (rmnet_get_endpoint(port, id))
  191. return -EBUSY;
  192. rmnet_dev->hw_features = NETIF_F_RXCSUM;
  193. rmnet_dev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
  194. rmnet_dev->hw_features |= NETIF_F_SG;
  195. priv->real_dev = real_dev;
  196. rc = register_netdevice(rmnet_dev);
  197. if (!rc) {
  198. ep->egress_dev = rmnet_dev;
  199. ep->mux_id = id;
  200. port->nr_rmnet_devs++;
  201. rmnet_dev->rtnl_link_ops = &rmnet_link_ops;
  202. priv->mux_id = id;
  203. netdev_dbg(rmnet_dev, "rmnet dev created\n");
  204. }
  205. return rc;
  206. }
  207. int rmnet_vnd_dellink(u8 id, struct rmnet_port *port,
  208. struct rmnet_endpoint *ep)
  209. {
  210. if (id >= RMNET_MAX_LOGICAL_EP || !ep->egress_dev)
  211. return -EINVAL;
  212. ep->egress_dev = NULL;
  213. port->nr_rmnet_devs--;
  214. return 0;
  215. }
  216. int rmnet_vnd_do_flow_control(struct net_device *rmnet_dev, int enable)
  217. {
  218. netdev_dbg(rmnet_dev, "Setting VND TX queue state to %d\n", enable);
  219. /* Although we expect similar number of enable/disable
  220. * commands, optimize for the disable. That is more
  221. * latency sensitive than enable
  222. */
  223. if (unlikely(enable))
  224. netif_wake_queue(rmnet_dev);
  225. else
  226. netif_stop_queue(rmnet_dev);
  227. return 0;
  228. }