rmnet_map_command.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* Copyright (c) 2013-2017, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/rmnet_data.h>
  17. #include <linux/net_map.h>
  18. #include <net/pkt_sched.h>
  19. #include <net/rmnet_config.h>
  20. #include "rmnet_data_config.h"
  21. #include "rmnet_map.h"
  22. #include "rmnet_data_private.h"
  23. #include "rmnet_data_vnd.h"
  24. #include "rmnet_data_stats.h"
  25. RMNET_LOG_MODULE(RMNET_DATA_LOGMASK_MAPC);
  26. unsigned long int rmnet_map_command_stats[RMNET_MAP_COMMAND_ENUM_LENGTH];
  27. module_param_array(rmnet_map_command_stats, ulong, 0, 0444);
  28. MODULE_PARM_DESC(rmnet_map_command_stats, "MAP command statistics");
  29. /* rmnet_map_do_flow_control() - Process MAP flow control command
  30. * @skb: Socket buffer containing the MAP flow control message
  31. * @config: Physical end-point configuration of ingress device
  32. * @enable: boolean for enable/disable
  33. *
  34. * Process in-band MAP flow control messages. Assumes mux ID is mapped to a
  35. * RmNet Data vitrual network device.
  36. *
  37. * Return:
  38. * - RMNET_MAP_COMMAND_UNSUPPORTED on any error
  39. * - RMNET_MAP_COMMAND_ACK on success
  40. */
  41. static uint8_t rmnet_map_do_flow_control(struct sk_buff *skb,
  42. struct rmnet_phys_ep_config *config,
  43. int enable)
  44. {
  45. struct rmnet_map_control_command_s *cmd;
  46. struct net_device *vnd;
  47. struct rmnet_logical_ep_conf_s *ep;
  48. u8 mux_id;
  49. u16 ip_family;
  50. u16 fc_seq;
  51. u32 qos_id;
  52. int r;
  53. if (unlikely(!skb || !config))
  54. return RX_HANDLER_CONSUMED;
  55. mux_id = RMNET_MAP_GET_MUX_ID(skb);
  56. cmd = RMNET_MAP_GET_CMD_START(skb);
  57. if (mux_id >= RMNET_DATA_MAX_LOGICAL_EP) {
  58. LOGD("Got packet on %s with bad mux id %d",
  59. skb->dev->name, mux_id);
  60. rmnet_kfree_skb(skb, RMNET_STATS_SKBFREE_MAPC_BAD_MUX);
  61. return RX_HANDLER_CONSUMED;
  62. }
  63. ep = &config->muxed_ep[mux_id];
  64. if (!ep->refcount) {
  65. LOGD("Packet on %s:%d; has no logical endpoint config",
  66. skb->dev->name, mux_id);
  67. rmnet_kfree_skb(skb, RMNET_STATS_SKBFREE_MAPC_MUX_NO_EP);
  68. return RX_HANDLER_CONSUMED;
  69. }
  70. vnd = ep->egress_dev;
  71. ip_family = cmd->flow_control.ip_family;
  72. fc_seq = ntohs(cmd->flow_control.flow_control_seq_num);
  73. qos_id = ntohl(cmd->flow_control.qos_id);
  74. /* Ignore the ip family and pass the sequence number for both v4 and v6
  75. * sequence. User space does not support creating dedicated flows for
  76. * the 2 protocols
  77. */
  78. r = rmnet_vnd_do_flow_control(vnd, qos_id, fc_seq, fc_seq, enable);
  79. LOGD("dev:%s, qos_id:0x%08X, ip_family:%hd, fc_seq %hd, en:%d",
  80. skb->dev->name, qos_id, ip_family & 3, fc_seq, enable);
  81. if (r) {
  82. rmnet_kfree_skb(skb, RMNET_STATS_SKBFREE_MAPC_UNSUPPORTED);
  83. return RMNET_MAP_COMMAND_UNSUPPORTED;
  84. } else {
  85. return RMNET_MAP_COMMAND_ACK;
  86. }
  87. }
  88. /* rmnet_map_send_ack() - Send N/ACK message for MAP commands
  89. * @skb: Socket buffer containing the MAP command message
  90. * @type: N/ACK message selector
  91. * @config: Physical end-point configuration of ingress device
  92. *
  93. * skb is modified to contain the message type selector. The message is then
  94. * transmitted on skb->dev. Note that this function grabs global Tx lock on
  95. * skb->dev for latency reasons.
  96. *
  97. * Return:
  98. * - void
  99. */
  100. static void rmnet_map_send_ack(struct sk_buff *skb,
  101. unsigned char type,
  102. struct rmnet_phys_ep_config *config)
  103. {
  104. struct rmnet_map_control_command_s *cmd;
  105. int xmit_status;
  106. int rc;
  107. if (unlikely(!skb))
  108. return;
  109. skb->protocol = htons(ETH_P_MAP);
  110. if ((config->ingress_data_format & RMNET_INGRESS_FORMAT_MAP_CKSUMV3) ||
  111. (config->ingress_data_format & RMNET_INGRESS_FORMAT_MAP_CKSUMV4)) {
  112. if (unlikely(skb->len < (sizeof(struct rmnet_map_header_s) +
  113. + RMNET_MAP_GET_LENGTH(skb)
  114. + sizeof(struct rmnet_map_dl_checksum_trailer_s)))) {
  115. rmnet_stats_dl_checksum(
  116. RMNET_MAP_CHECKSUM_ERR_BAD_BUFFER);
  117. return;
  118. }
  119. skb_trim(skb, skb->len -
  120. sizeof(struct rmnet_map_dl_checksum_trailer_s));
  121. }
  122. cmd = RMNET_MAP_GET_CMD_START(skb);
  123. cmd->cmd_type = type & 0x03;
  124. netif_tx_lock(skb->dev);
  125. xmit_status = skb->dev->netdev_ops->ndo_start_xmit(skb, skb->dev);
  126. netif_tx_unlock(skb->dev);
  127. LOGD("MAP command ACK=%hhu sent with rc: %d", type & 0x03, xmit_status);
  128. if (xmit_status != NETDEV_TX_OK) {
  129. rc = dev_queue_xmit(skb);
  130. if (rc != 0) {
  131. LOGD("Failed to queue packet for transmission on [%s]",
  132. skb->dev->name);
  133. }
  134. }
  135. }
  136. /* rmnet_map_command() - Entry point for handling MAP commands
  137. * @skb: Socket buffer containing the MAP command message
  138. * @config: Physical end-point configuration of ingress device
  139. *
  140. * Process MAP command frame and send N/ACK message as appropriate. Message cmd
  141. * name is decoded here and appropriate handler is called.
  142. *
  143. * Return:
  144. * - RX_HANDLER_CONSUMED. Command frames are always consumed.
  145. */
  146. rx_handler_result_t rmnet_map_command(struct sk_buff *skb,
  147. struct rmnet_phys_ep_config *config)
  148. {
  149. struct rmnet_map_control_command_s *cmd;
  150. unsigned char command_name;
  151. unsigned char rc = 0;
  152. if (unlikely(!skb))
  153. return RX_HANDLER_CONSUMED;
  154. cmd = RMNET_MAP_GET_CMD_START(skb);
  155. command_name = cmd->command_name;
  156. if (command_name < RMNET_MAP_COMMAND_ENUM_LENGTH)
  157. rmnet_map_command_stats[command_name]++;
  158. switch (command_name) {
  159. case RMNET_MAP_COMMAND_FLOW_ENABLE:
  160. rc = rmnet_map_do_flow_control(skb, config, 1);
  161. break;
  162. case RMNET_MAP_COMMAND_FLOW_DISABLE:
  163. rc = rmnet_map_do_flow_control(skb, config, 0);
  164. break;
  165. default:
  166. rmnet_map_command_stats[RMNET_MAP_COMMAND_UNKNOWN]++;
  167. LOGM("Uknown MAP command: %d", command_name);
  168. rc = RMNET_MAP_COMMAND_UNSUPPORTED;
  169. rmnet_kfree_skb(skb, RMNET_STATS_SKBFREE_MAPC_UNSUPPORTED);
  170. break;
  171. }
  172. if (rc == RMNET_MAP_COMMAND_ACK)
  173. rmnet_map_send_ack(skb, rc, config);
  174. return RX_HANDLER_CONSUMED;
  175. }