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