History

batman-adv: throughput meter implementation

The throughput meter module is a simple, kernel-space replacement for throughtput measurements tool like iperf and netperf. It is intended to approximate TCP behaviour. It is invoked through batctl: the protocol is connection oriented, with cumulative acknowledgment and a dynamic-size sliding window. The test *can* be interrupted by batctl. A receiver side timeout avoids unlimited waitings for sender packets: after one second of inactivity, the receiver abort the ongoing test. Based on a prototype from Edo Monticelli <montik@autistici.org>

Antonio Quartulli committed 10 years ago

Commit 33a3bb41 parent f50ca9513 files · +2021 −10Browse files

Unified · Split

13 files
  1. include/uapi/linux/batman_adv.h +43 −0
  2. net/batman-adv/Makefile +1 −0
  3. net/batman-adv/log.h +10 −8
  4. net/batman-adv/main.c +4 −0
  5. net/batman-adv/main.h +8 −0
  6. net/batman-adv/netlink.c +232 −2
  7. net/batman-adv/netlink.h +6 −0
  8. net/batman-adv/packet.h +54 −0
  9. net/batman-adv/routing.c +8 −0
  10. net/batman-adv/soft-interface.c +2 −0
  11. net/batman-adv/tp_meter.c +1507 −0
  12. net/batman-adv/tp_meter.h +34 −0
  13. net/batman-adv/types.h +112 −0
include/uapi/linux/batman_adv.h+43−0
@@ -20,6 +20,8 @@ · 19 unmodified lines
2020
2121 #define BATADV_NL_NAME "batadv"
2222
23+#define BATADV_NL_MCAST_GROUP_TPMETER "tpmeter"
24+
2325 /**
2426 * enum batadv_nl_attrs - batman-adv netlink attributes
2527 *
@@ -32,6 +34,12 @@ · 6 unmodified lines
3234 * @BATADV_ATTR_HARD_IFINDEX: index of the non-batman-adv interface
3335 * @BATADV_ATTR_HARD_IFNAME: name of the non-batman-adv interface
3436 * @BATADV_ATTR_HARD_ADDRESS: mac address of the non-batman-adv interface
37+ * @BATADV_ATTR_ORIG_ADDRESS: originator mac address
38+ * @BATADV_ATTR_TPMETER_RESULT: result of run (see batadv_tp_meter_status)
39+ * @BATADV_ATTR_TPMETER_TEST_TIME: time (msec) the run took
40+ * @BATADV_ATTR_TPMETER_BYTES: amount of acked bytes during run
41+ * @BATADV_ATTR_TPMETER_COOKIE: session cookie to match tp_meter session
42+ * @BATADV_ATTR_PAD: attribute used for padding for 64-bit alignment
3543 * @__BATADV_ATTR_AFTER_LAST: internal use
3644 * @NUM_BATADV_ATTR: total number of batadv_nl_attrs available
3745 * @BATADV_ATTR_MAX: highest attribute number currently defined
@@ -46,6 +54,12 @@ enum batadv_nl_attrs { · 8 unmodified lines
4654 BATADV_ATTR_HARD_IFINDEX,
4755 BATADV_ATTR_HARD_IFNAME,
4856 BATADV_ATTR_HARD_ADDRESS,
57+ BATADV_ATTR_ORIG_ADDRESS,
58+ BATADV_ATTR_TPMETER_RESULT,
59+ BATADV_ATTR_TPMETER_TEST_TIME,
60+ BATADV_ATTR_TPMETER_BYTES,
61+ BATADV_ATTR_TPMETER_COOKIE,
62+ BATADV_ATTR_PAD,
4963 /* add attributes above here, update the policy in netlink.c */
5064 __BATADV_ATTR_AFTER_LAST,
5165 NUM_BATADV_ATTR = __BATADV_ATTR_AFTER_LAST,
@@ -57,15 +71,44 @@ enum batadv_nl_attrs { · 5 unmodified lines
5771 *
5872 * @BATADV_CMD_UNSPEC: unspecified command to catch errors
5973 * @BATADV_CMD_GET_MESH_INFO: Query basic information about batman-adv device
74+ * @BATADV_CMD_TP_METER: Start a tp meter session
75+ * @BATADV_CMD_TP_METER_CANCEL: Cancel a tp meter session
6076 * @__BATADV_CMD_AFTER_LAST: internal use
6177 * @BATADV_CMD_MAX: highest used command number
6278 */
6379 enum batadv_nl_commands {
6480 BATADV_CMD_UNSPEC,
6581 BATADV_CMD_GET_MESH_INFO,
82+ BATADV_CMD_TP_METER,
83+ BATADV_CMD_TP_METER_CANCEL,
6684 /* add new commands above here */
6785 __BATADV_CMD_AFTER_LAST,
6886 BATADV_CMD_MAX = __BATADV_CMD_AFTER_LAST - 1
6987 };
7088
89+/**
90+ * enum batadv_tp_meter_reason - reason of a tp meter test run stop
91+ * @BATADV_TP_REASON_COMPLETE: sender finished tp run
92+ * @BATADV_TP_REASON_CANCEL: sender was stopped during run
93+ * @BATADV_TP_REASON_DST_UNREACHABLE: receiver could not be reached or didn't
94+ * answer
95+ * @BATADV_TP_REASON_RESEND_LIMIT: (unused) sender retry reached limit
96+ * @BATADV_TP_REASON_ALREADY_ONGOING: test to or from the same node already
97+ * ongoing
98+ * @BATADV_TP_REASON_MEMORY_ERROR: test was stopped due to low memory
99+ * @BATADV_TP_REASON_CANT_SEND: failed to send via outgoing interface
100+ * @BATADV_TP_REASON_TOO_MANY: too many ongoing sessions
101+ */
102+enum batadv_tp_meter_reason {
103+ BATADV_TP_REASON_COMPLETE = 3,
104+ BATADV_TP_REASON_CANCEL = 4,
105+ /* error status >= 128 */
106+ BATADV_TP_REASON_DST_UNREACHABLE = 128,
107+ BATADV_TP_REASON_RESEND_LIMIT = 129,
108+ BATADV_TP_REASON_ALREADY_ONGOING = 130,
109+ BATADV_TP_REASON_MEMORY_ERROR = 131,
110+ BATADV_TP_REASON_CANT_SEND = 132,
111+ BATADV_TP_REASON_TOO_MANY = 133,
112+};
113+
71114 #endif /* _UAPI_LINUX_BATMAN_ADV_H_ */
net/batman-adv/Makefile+1−0
@@ -42,5 +42,6 @@ batman-adv-y += routing.o · 41 unmodified lines
4242 batman-adv-y += send.o
4343 batman-adv-y += soft-interface.o
4444 batman-adv-y += sysfs.o
45+batman-adv-y += tp_meter.o
4546 batman-adv-y += translation-table.o
4647 batman-adv-y += tvlv.o
net/batman-adv/log.h+10−8
@@ -51,17 +51,19 @@ static inline void batadv_debug_log_cleanup(struct batadv_priv *bat_priv) · 50 unmodified lines
5151 * @BATADV_DBG_DAT: ARP snooping and DAT related messages
5252 * @BATADV_DBG_NC: network coding related messages
5353 * @BATADV_DBG_MCAST: multicast related messages
54+ * @BATADV_DBG_TP_METER: throughput meter messages
5455 * @BATADV_DBG_ALL: the union of all the above log levels
5556 */
5657 enum batadv_dbg_level {
57− BATADV_DBG_BATMAN = BIT(0),
58− BATADV_DBG_ROUTES = BIT(1),
59− BATADV_DBG_TT = BIT(2),
60− BATADV_DBG_BLA = BIT(3),
61− BATADV_DBG_DAT = BIT(4),
62− BATADV_DBG_NC = BIT(5),
63− BATADV_DBG_MCAST = BIT(6),
64− BATADV_DBG_ALL = 127,
58+ BATADV_DBG_BATMAN = BIT(0),
59+ BATADV_DBG_ROUTES = BIT(1),
60+ BATADV_DBG_TT = BIT(2),
61+ BATADV_DBG_BLA = BIT(3),
62+ BATADV_DBG_DAT = BIT(4),
63+ BATADV_DBG_NC = BIT(5),
64+ BATADV_DBG_MCAST = BIT(6),
65+ BATADV_DBG_TP_METER = BIT(7),
66+ BATADV_DBG_ALL = 127,
6567 };
6668
6769 #ifdef CONFIG_BATMAN_ADV_DEBUG
net/batman-adv/main.c+4−0
@@ -64,6 +64,7 @@ · 63 unmodified lines
6464 #include "routing.h"
6565 #include "send.h"
6666 #include "soft-interface.h"
67+#include "tp_meter.h"
6768 #include "translation-table.h"
6869
6970 /* List manipulations on hardif_list have to be rtnl_lock()'ed,
@@ -89,6 +90,7 @@ static int __init batadv_init(void) · 19 unmodified lines
8990 batadv_v_init();
9091 batadv_iv_init();
9192 batadv_nc_init();
93+ batadv_tp_meter_init();
9294
9395 batadv_event_workqueue = create_singlethread_workqueue("bat_events");
9496
@@ -142,6 +144,7 @@ int batadv_mesh_init(struct net_device *soft_iface) · 47 unmodified lines
142144 spin_lock_init(&bat_priv->tvlv.container_list_lock);
143145 spin_lock_init(&bat_priv->tvlv.handler_list_lock);
144146 spin_lock_init(&bat_priv->softif_vlan_list_lock);
147+ spin_lock_init(&bat_priv->tp_list_lock);
145148
146149 INIT_HLIST_HEAD(&bat_priv->forw_bat_list);
147150 INIT_HLIST_HEAD(&bat_priv->forw_bcast_list);
@@ -160,6 +163,7 @@ int batadv_mesh_init(struct net_device *soft_iface) · 12 unmodified lines
160163 INIT_HLIST_HEAD(&bat_priv->tvlv.container_list);
161164 INIT_HLIST_HEAD(&bat_priv->tvlv.handler_list);
162165 INIT_HLIST_HEAD(&bat_priv->softif_vlan_list);
166+ INIT_HLIST_HEAD(&bat_priv->tp_list);
163167
164168 ret = batadv_v_mesh_init(bat_priv);
165169 if (ret < 0)
net/batman-adv/main.h+8−0
@@ -100,6 +100,9 @@ · 99 unmodified lines
100100 #define BATADV_NUM_BCASTS_WIRELESS 3
101101 #define BATADV_NUM_BCASTS_MAX 3
102102
103+/* length of the single packet used by the TP meter */
104+#define BATADV_TP_PACKET_LEN ETH_DATA_LEN
105+
103106 /* msecs after which an ARP_REQUEST is sent in broadcast as fallback */
104107 #define ARP_REQ_DELAY 250
105108 /* numbers of originator to contact for any PUT/GET DHT operation */
@@ -131,6 +134,11 @@ · 25 unmodified lines
131134
132135 #define BATADV_NC_NODE_TIMEOUT 10000 /* Milliseconds */
133136
137+/**
138+ * BATADV_TP_MAX_NUM - maximum number of simultaneously active tp sessions
139+ */
140+#define BATADV_TP_MAX_NUM 5
141+
134142 enum batadv_mesh_state {
135143 BATADV_MESH_INACTIVE,
136144 BATADV_MESH_ACTIVE,
net/batman-adv/netlink.c+232−2
@@ -27,12 +27,14 @@ · 26 unmodified lines
2727 #include <linux/netlink.h>
2828 #include <linux/printk.h>
2929 #include <linux/stddef.h>
30+#include <linux/types.h>
3031 #include <net/genetlink.h>
3132 #include <net/netlink.h>
3233 #include <uapi/linux/batman_adv.h>
3334
3435 #include "hard-interface.h"
3536 #include "soft-interface.h"
37+#include "tp_meter.h"
3638
3739 struct sk_buff;
3840
@@ -44,6 +46,15 @@ static struct genl_family batadv_netlink_family = { · 5 unmodified lines
4446 .maxattr = BATADV_ATTR_MAX,
4547 };
4648
49+/* multicast groups */
50+enum batadv_netlink_multicast_groups {
51+ BATADV_NL_MCGRP_TPMETER,
52+};
53+
54+static struct genl_multicast_group batadv_netlink_mcgrps[] = {
55+ [BATADV_NL_MCGRP_TPMETER] = { .name = BATADV_NL_MCAST_GROUP_TPMETER },
56+};
57+
4758 static struct nla_policy batadv_netlink_policy[NUM_BATADV_ATTR] = {
4859 [BATADV_ATTR_VERSION] = { .type = NLA_STRING },
4960 [BATADV_ATTR_ALGO_NAME] = { .type = NLA_STRING },
@@ -53,6 +64,11 @@ static struct nla_policy batadv_netlink_policy[NUM_BATADV_ATTR] = { · 3 unmodified lines
5364 [BATADV_ATTR_HARD_IFINDEX] = { .type = NLA_U32 },
5465 [BATADV_ATTR_HARD_IFNAME] = { .type = NLA_STRING },
5566 [BATADV_ATTR_HARD_ADDRESS] = { .len = ETH_ALEN },
67+ [BATADV_ATTR_ORIG_ADDRESS] = { .len = ETH_ALEN },
68+ [BATADV_ATTR_TPMETER_RESULT] = { .type = NLA_U8 },
69+ [BATADV_ATTR_TPMETER_TEST_TIME] = { .type = NLA_U32 },
70+ [BATADV_ATTR_TPMETER_BYTES] = { .type = NLA_U64 },
71+ [BATADV_ATTR_TPMETER_COOKIE] = { .type = NLA_U32 },
5672 };
5773
5874 /**
@@ -163,6 +179,207 @@ batadv_netlink_get_mesh_info(struct sk_buff *skb, struct genl_info *info) · 104 unmodified lines
163179 return genlmsg_reply(msg, info);
164180 }
165181
182+/**
183+ * batadv_netlink_tp_meter_put - Fill information of started tp_meter session
184+ * @msg: netlink message to be sent back
185+ * @cookie: tp meter session cookie
186+ *
187+ * Return: 0 on success, < 0 on error
188+ */
189+static int
190+batadv_netlink_tp_meter_put(struct sk_buff *msg, u32 cookie)
191+{
192+ if (nla_put_u32(msg, BATADV_ATTR_TPMETER_COOKIE, cookie))
193+ return -ENOBUFS;
194+
195+ return 0;
196+}
197+
198+/**
199+ * batadv_netlink_tpmeter_notify - send tp_meter result via netlink to client
200+ * @bat_priv: the bat priv with all the soft interface information
201+ * @dst: destination of tp_meter session
202+ * @result: reason for tp meter session stop
203+ * @test_time: total time ot the tp_meter session
204+ * @total_bytes: bytes acked to the receiver
205+ * @cookie: cookie of tp_meter session
206+ *
207+ * Return: 0 on success, < 0 on error
208+ */
209+int batadv_netlink_tpmeter_notify(struct batadv_priv *bat_priv, const u8 *dst,
210+ u8 result, u32 test_time, u64 total_bytes,
211+ u32 cookie)
212+{
213+ struct sk_buff *msg;
214+ void *hdr;
215+ int ret;
216+
217+ msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
218+ if (!msg)
219+ return -ENOMEM;
220+
221+ hdr = genlmsg_put(msg, 0, 0, &batadv_netlink_family, 0,
222+ BATADV_CMD_TP_METER);
223+ if (!hdr) {
224+ ret = -ENOBUFS;
225+ goto err_genlmsg;
226+ }
227+
228+ if (nla_put_u32(msg, BATADV_ATTR_TPMETER_COOKIE, cookie))
229+ goto nla_put_failure;
230+
231+ if (nla_put_u32(msg, BATADV_ATTR_TPMETER_TEST_TIME, test_time))
232+ goto nla_put_failure;
233+
234+ if (nla_put_u64_64bit(msg, BATADV_ATTR_TPMETER_BYTES, total_bytes,
235+ BATADV_ATTR_PAD))
236+ goto nla_put_failure;
237+
238+ if (nla_put_u8(msg, BATADV_ATTR_TPMETER_RESULT, result))
239+ goto nla_put_failure;
240+
241+ if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN, dst))
242+ goto nla_put_failure;
243+
244+ genlmsg_end(msg, hdr);
245+
246+ genlmsg_multicast_netns(&batadv_netlink_family,
247+ dev_net(bat_priv->soft_iface), msg, 0,
248+ BATADV_NL_MCGRP_TPMETER, GFP_KERNEL);
249+
250+ return 0;
251+
252+nla_put_failure:
253+ genlmsg_cancel(msg, hdr);
254+ ret = -EMSGSIZE;
255+
256+err_genlmsg:
257+ nlmsg_free(msg);
258+ return ret;
259+}
260+
261+/**
262+ * batadv_netlink_tp_meter_start - Start a new tp_meter session
263+ * @skb: received netlink message
264+ * @info: receiver information
265+ *
266+ * Return: 0 on success, < 0 on error
267+ */
268+static int
269+batadv_netlink_tp_meter_start(struct sk_buff *skb, struct genl_info *info)
270+{
271+ struct net *net = genl_info_net(info);
272+ struct net_device *soft_iface;
273+ struct batadv_priv *bat_priv;
274+ struct sk_buff *msg = NULL;
275+ u32 test_length;
276+ void *msg_head;
277+ int ifindex;
278+ u32 cookie;
279+ u8 *dst;
280+ int ret;
281+
282+ if (!info->attrs[BATADV_ATTR_MESH_IFINDEX])
283+ return -EINVAL;
284+
285+ if (!info->attrs[BATADV_ATTR_ORIG_ADDRESS])
286+ return -EINVAL;
287+
288+ if (!info->attrs[BATADV_ATTR_TPMETER_TEST_TIME])
289+ return -EINVAL;
290+
291+ ifindex = nla_get_u32(info->attrs[BATADV_ATTR_MESH_IFINDEX]);
292+ if (!ifindex)
293+ return -EINVAL;
294+
295+ dst = nla_data(info->attrs[BATADV_ATTR_ORIG_ADDRESS]);
296+
297+ test_length = nla_get_u32(info->attrs[BATADV_ATTR_TPMETER_TEST_TIME]);
298+
299+ soft_iface = dev_get_by_index(net, ifindex);
300+ if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
301+ ret = -ENODEV;
302+ goto out;
303+ }
304+
305+ msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
306+ if (!msg) {
307+ ret = -ENOMEM;
308+ goto out;
309+ }
310+
311+ msg_head = genlmsg_put(msg, info->snd_portid, info->snd_seq,
312+ &batadv_netlink_family, 0,
313+ BATADV_CMD_TP_METER);
314+ if (!msg_head) {
315+ ret = -ENOBUFS;
316+ goto out;
317+ }
318+
319+ bat_priv = netdev_priv(soft_iface);
320+ batadv_tp_start(bat_priv, dst, test_length, &cookie);
321+
322+ ret = batadv_netlink_tp_meter_put(msg, cookie);
323+
324+ out:
325+ if (soft_iface)
326+ dev_put(soft_iface);
327+
328+ if (ret) {
329+ if (msg)
330+ nlmsg_free(msg);
331+ return ret;
332+ }
333+
334+ genlmsg_end(msg, msg_head);
335+ return genlmsg_reply(msg, info);
336+}
337+
338+/**
339+ * batadv_netlink_tp_meter_start - Cancel a running tp_meter session
340+ * @skb: received netlink message
341+ * @info: receiver information
342+ *
343+ * Return: 0 on success, < 0 on error
344+ */
345+static int
346+batadv_netlink_tp_meter_cancel(struct sk_buff *skb, struct genl_info *info)
347+{
348+ struct net *net = genl_info_net(info);
349+ struct net_device *soft_iface;
350+ struct batadv_priv *bat_priv;
351+ int ifindex;
352+ u8 *dst;
353+ int ret = 0;
354+
355+ if (!info->attrs[BATADV_ATTR_MESH_IFINDEX])
356+ return -EINVAL;
357+
358+ if (!info->attrs[BATADV_ATTR_ORIG_ADDRESS])
359+ return -EINVAL;
360+
361+ ifindex = nla_get_u32(info->attrs[BATADV_ATTR_MESH_IFINDEX]);
362+ if (!ifindex)
363+ return -EINVAL;
364+
365+ dst = nla_data(info->attrs[BATADV_ATTR_ORIG_ADDRESS]);
366+
367+ soft_iface = dev_get_by_index(net, ifindex);
368+ if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
369+ ret = -ENODEV;
370+ goto out;
371+ }
372+
373+ bat_priv = netdev_priv(soft_iface);
374+ batadv_tp_stop(bat_priv, dst, BATADV_TP_REASON_CANCEL);
375+
376+out:
377+ if (soft_iface)
378+ dev_put(soft_iface);
379+
380+ return ret;
381+}
382+
166383 static struct genl_ops batadv_netlink_ops[] = {
167384 {
168385 .cmd = BATADV_CMD_GET_MESH_INFO,
@@ -170,6 +387,18 @@ static struct genl_ops batadv_netlink_ops[] = { · 1 unmodified line
170387 .policy = batadv_netlink_policy,
171388 .doit = batadv_netlink_get_mesh_info,
172389 },
390+ {
391+ .cmd = BATADV_CMD_TP_METER,
392+ .flags = GENL_ADMIN_PERM,
393+ .policy = batadv_netlink_policy,
394+ .doit = batadv_netlink_tp_meter_start,
395+ },
396+ {
397+ .cmd = BATADV_CMD_TP_METER_CANCEL,
398+ .flags = GENL_ADMIN_PERM,
399+ .policy = batadv_netlink_policy,
400+ .doit = batadv_netlink_tp_meter_cancel,
401+ },
173402 };
174403
175404 /**
@@ -179,8 +408,9 @@ void __init batadv_netlink_register(void) · 3 unmodified lines
179408 {
180409 int ret;
181410
182− ret = genl_register_family_with_ops(&batadv_netlink_family,
183− batadv_netlink_ops);
411+ ret = genl_register_family_with_ops_groups(&batadv_netlink_family,
412+ batadv_netlink_ops,
413+ batadv_netlink_mcgrps);
184414 if (ret)
185415 pr_warn("unable to register netlink family");
186416 }
net/batman-adv/netlink.h+6−0
@@ -20,7 +20,13 @@ · 19 unmodified lines
2020
2121 #include "main.h"
2222
23+#include <linux/types.h>
24+
2325 void batadv_netlink_register(void);
2426 void batadv_netlink_unregister(void);
2527
28+int batadv_netlink_tpmeter_notify(struct batadv_priv *bat_priv, const u8 *dst,
29+ u8 result, u32 test_time, u64 total_bytes,
30+ u32 cookie);
31+
2632 #endif /* _NET_BATMAN_ADV_NETLINK_H_ */
net/batman-adv/packet.h+54−0
@@ -21,6 +21,8 @@ · 20 unmodified lines
2121 #include <asm/byteorder.h>
2222 #include <linux/types.h>
2323
24+#define batadv_tp_is_error(n) ((u8)n > 127 ? 1 : 0)
25+
2426 /**
2527 * enum batadv_packettype - types for batman-adv encapsulated packets
2628 * @BATADV_IV_OGM: originator messages for B.A.T.M.A.N. IV
@@ -93,6 +95,7 @@ enum batadv_icmp_packettype { · 66 unmodified lines
9395 BATADV_ECHO_REQUEST = 8,
9496 BATADV_TTL_EXCEEDED = 11,
9597 BATADV_PARAMETER_PROBLEM = 12,
98+ BATADV_TP = 15,
9699 };
97100
98101 /**
@@ -284,6 +287,16 @@ struct batadv_elp_packet { · 185 unmodified lines
284287
285288 #define BATADV_ELP_HLEN sizeof(struct batadv_elp_packet)
286289
290+/**
291+ * enum batadv_icmp_user_cmd_type - types for batman-adv icmp cmd modes
292+ * @BATADV_TP_START: start a throughput meter run
293+ * @BATADV_TP_STOP: stop a throughput meter run
294+ */
295+enum batadv_icmp_user_cmd_type {
296+ BATADV_TP_START = 0,
297+ BATADV_TP_STOP = 2,
298+};
299+
287300 /**
288301 * struct batadv_icmp_header - common members among all the ICMP packets
289302 * @packet_type: batman-adv packet type, part of the general header
@@ -334,6 +347,47 @@ struct batadv_icmp_packet { · 44 unmodified lines
334347 __be16 seqno;
335348 };
336349
350+/**
351+ * struct batadv_icmp_tp_packet - ICMP TP Meter packet
352+ * @packet_type: batman-adv packet type, part of the general header
353+ * @version: batman-adv protocol version, part of the genereal header
354+ * @ttl: time to live for this packet, part of the genereal header
355+ * @msg_type: ICMP packet type
356+ * @dst: address of the destination node
357+ * @orig: address of the source node
358+ * @uid: local ICMP socket identifier
359+ * @subtype: TP packet subtype (see batadv_icmp_tp_subtype)
360+ * @session: TP session identifier
361+ * @seqno: the TP sequence number
362+ * @timestamp: time when the packet has been sent. This value is filled in a
363+ * TP_MSG and echoed back in the next TP_ACK so that the sender can compute the
364+ * RTT. Since it is read only by the host which wrote it, there is no need to
365+ * store it using network order
366+ */
367+struct batadv_icmp_tp_packet {
368+ u8 packet_type;
369+ u8 version;
370+ u8 ttl;
371+ u8 msg_type; /* see ICMP message types above */
372+ u8 dst[ETH_ALEN];
373+ u8 orig[ETH_ALEN];
374+ u8 uid;
375+ u8 subtype;
376+ u8 session[2];
377+ __be32 seqno;
378+ __be32 timestamp;
379+};
380+
381+/**
382+ * enum batadv_icmp_tp_subtype - ICMP TP Meter packet subtypes
383+ * @BATADV_TP_MSG: Msg from sender to receiver
384+ * @BATADV_TP_ACK: acknowledgment from receiver to sender
385+ */
386+enum batadv_icmp_tp_subtype {
387+ BATADV_TP_MSG = 0,
388+ BATADV_TP_ACK,
389+};
390+
337391 #define BATADV_RR_LEN 16
338392
339393 /**
net/batman-adv/routing.c+8−0
@@ -46,6 +46,7 @@ · 45 unmodified lines
4646 #include "packet.h"
4747 #include "send.h"
4848 #include "soft-interface.h"
49+#include "tp_meter.h"
4950 #include "translation-table.h"
5051 #include "tvlv.h"
5152
@@ -276,6 +277,13 @@ static int batadv_recv_my_icmp_packet(struct batadv_priv *bat_priv, · 224 unmodified lines
276277 ret = NET_RX_SUCCESS;
277278
278279 break;
280+ case BATADV_TP:
281+ if (!pskb_may_pull(skb, sizeof(struct batadv_icmp_tp_packet)))
282+ goto out;
283+
284+ batadv_tp_meter_recv(bat_priv, skb);
285+ ret = NET_RX_SUCCESS;
286+ goto out;
279287 default:
280288 /* drop unknown type */
281289 goto out;
net/batman-adv/soft-interface.c+2−0
@@ -842,6 +842,8 @@ static int batadv_softif_init_late(struct net_device *dev) · 841 unmodified lines
842842 #ifdef CONFIG_BATMAN_ADV_BLA
843843 atomic_set(&bat_priv->bla.num_requests, 0);
844844 #endif
845+ atomic_set(&bat_priv->tp_num, 0);
846+
845847 bat_priv->tt.last_changeset = NULL;
846848 bat_priv->tt.last_changeset_len = 0;
847849 bat_priv->isolation_mark = 0;
net/batman-adv/tp_meter.c+1507−0
@@ -0,0 +1,1507 @@
1+/* Copyright (C) 2012-2016 B.A.T.M.A.N. contributors:
2+ *
3+ * Edo Monticelli, Antonio Quartulli
4+ *
5+ * This program is free software; you can redistribute it and/or
6+ * modify it under the terms of version 2 of the GNU General Public
7+ * License as published by the Free Software Foundation.
8+ *
9+ * This program is distributed in the hope that it will be useful, but
10+ * WITHOUT ANY WARRANTY; without even the implied warranty of
11+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+ * General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU General Public License
15+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
16+ */
17+
18+#include "tp_meter.h"
19+#include "main.h"
20+
21+#include <linux/atomic.h>
22+#include <linux/bug.h>
23+#include <linux/byteorder/generic.h>
24+#include <linux/cache.h>
25+#include <linux/compiler.h>
26+#include <linux/device.h>
27+#include <linux/etherdevice.h>
28+#include <linux/fs.h>
29+#include <linux/if_ether.h>
30+#include <linux/jiffies.h>
31+#include <linux/kernel.h>
32+#include <linux/kref.h>
33+#include <linux/kthread.h>
34+#include <linux/list.h>
35+#include <linux/netdevice.h>
36+#include <linux/param.h>
37+#include <linux/printk.h>
38+#include <linux/random.h>
39+#include <linux/rculist.h>
40+#include <linux/rcupdate.h>
41+#include <linux/sched.h>
42+#include <linux/skbuff.h>
43+#include <linux/slab.h>
44+#include <linux/spinlock.h>
45+#include <linux/stddef.h>
46+#include <linux/string.h>
47+#include <linux/timer.h>
48+#include <linux/wait.h>
49+#include <linux/workqueue.h>
50+#include <uapi/linux/batman_adv.h>
51+
52+#include "hard-interface.h"
53+#include "log.h"
54+#include "netlink.h"
55+#include "originator.h"
56+#include "packet.h"
57+#include "send.h"
58+
59+/**
60+ * BATADV_TP_DEF_TEST_LENGTH - Default test length if not specified by the user
61+ * in milliseconds
62+ */
63+#define BATADV_TP_DEF_TEST_LENGTH 10000
64+
65+/**
66+ * BATADV_TP_AWND - Advertised window by the receiver (in bytes)
67+ */
68+#define BATADV_TP_AWND 0x20000000
69+
70+/**
71+ * BATADV_TP_RECV_TIMEOUT - Receiver activity timeout. If the receiver does not
72+ * get anything for such amount of milliseconds, the connection is killed
73+ */
74+#define BATADV_TP_RECV_TIMEOUT 1000
75+
76+/**
77+ * BATADV_TP_MAX_RTO - Maximum sender timeout. If the sender RTO gets beyond
78+ * such amound of milliseconds, the receiver is considered unreachable and the
79+ * connection is killed
80+ */
81+#define BATADV_TP_MAX_RTO 30000
82+
83+/**
84+ * BATADV_TP_FIRST_SEQ - First seqno of each session. The number is rather high
85+ * in order to immediately trigger a wrap around (test purposes)
86+ */
87+#define BATADV_TP_FIRST_SEQ ((u32)-1 - 2000)
88+
89+/**
90+ * BATADV_TP_PLEN - length of the payload (data after the batadv_unicast header)
91+ * to simulate
92+ */
93+#define BATADV_TP_PLEN (BATADV_TP_PACKET_LEN - ETH_HLEN - \
94+ sizeof(struct batadv_unicast_packet))
95+
96+static u8 batadv_tp_prerandom[4096] __read_mostly;
97+
98+/**
99+ * batadv_tp_session_cookie - generate session cookie based on session ids
100+ * @session: TP session identifier
101+ * @icmp_uid: icmp pseudo uid of the tp session
102+ *
103+ * Return: 32 bit tp_meter session cookie
104+ */
105+static u32 batadv_tp_session_cookie(const u8 session[2], u8 icmp_uid)
106+{
107+ u32 cookie;
108+
109+ cookie = icmp_uid << 16;
110+ cookie |= session[0] << 8;
111+ cookie |= session[1];
112+
113+ return cookie;
114+}
115+
116+/**
117+ * batadv_tp_cwnd - compute the new cwnd size
118+ * @base: base cwnd size value
119+ * @increment: the value to add to base to get the new size
120+ * @min: minumim cwnd value (usually MSS)
121+ *
122+ * Return the new cwnd size and ensures it does not exceed the Advertised
123+ * Receiver Window size. It is wrap around safe.
124+ * For details refer to Section 3.1 of RFC5681
125+ *
126+ * Return: new congestion window size in bytes
127+ */
128+static u32 batadv_tp_cwnd(u32 base, u32 increment, u32 min)
129+{
130+ u32 new_size = base + increment;
131+
132+ /* check for wrap-around */
133+ if (new_size < base)
134+ new_size = (u32)ULONG_MAX;
135+
136+ new_size = min_t(u32, new_size, BATADV_TP_AWND);
137+
138+ return max_t(u32, new_size, min);
139+}
140+
141+/**
142+ * batadv_tp_updated_cwnd - update the Congestion Windows
143+ * @tp_vars: the private data of the current TP meter session
144+ * @mss: maximum segment size of transmission
145+ *
146+ * 1) if the session is in Slow Start, the CWND has to be increased by 1
147+ * MSS every unique received ACK
148+ * 2) if the session is in Congestion Avoidance, the CWND has to be
149+ * increased by MSS * MSS / CWND for every unique received ACK
150+ */
151+static void batadv_tp_update_cwnd(struct batadv_tp_vars *tp_vars, u32 mss)
152+{
153+ spin_lock_bh(&tp_vars->cwnd_lock);
154+
155+ /* slow start... */
156+ if (tp_vars->cwnd <= tp_vars->ss_threshold) {
157+ tp_vars->dec_cwnd = 0;
158+ tp_vars->cwnd = batadv_tp_cwnd(tp_vars->cwnd, mss, mss);
159+ spin_unlock_bh(&tp_vars->cwnd_lock);
160+ return;
161+ }
162+
163+ /* increment CWND at least of 1 (section 3.1 of RFC5681) */
164+ tp_vars->dec_cwnd += max_t(u32, 1U << 3,
165+ ((mss * mss) << 6) / (tp_vars->cwnd << 3));
166+ if (tp_vars->dec_cwnd < (mss << 3)) {
167+ spin_unlock_bh(&tp_vars->cwnd_lock);
168+ return;
169+ }
170+
171+ tp_vars->cwnd = batadv_tp_cwnd(tp_vars->cwnd, mss, mss);
172+ tp_vars->dec_cwnd = 0;
173+
174+ spin_unlock_bh(&tp_vars->cwnd_lock);
175+}
176+
177+/**
178+ * batadv_tp_update_rto - calculate new retransmission timeout
179+ * @tp_vars: the private data of the current TP meter session
180+ * @new_rtt: new roundtrip time in msec
181+ */
182+static void batadv_tp_update_rto(struct batadv_tp_vars *tp_vars,
183+ u32 new_rtt)
184+{
185+ long m = new_rtt;
186+
187+ /* RTT update
188+ * Details in Section 2.2 and 2.3 of RFC6298
189+ *
190+ * It's tricky to understand. Don't lose hair please.
191+ * Inspired by tcp_rtt_estimator() tcp_input.c
192+ */
193+ if (tp_vars->srtt != 0) {
194+ m -= (tp_vars->srtt >> 3); /* m is now error in rtt est */
195+ tp_vars->srtt += m; /* rtt = 7/8 srtt + 1/8 new */
196+ if (m < 0)
197+ m = -m;
198+
199+ m -= (tp_vars->rttvar >> 2);
200+ tp_vars->rttvar += m; /* mdev ~= 3/4 rttvar + 1/4 new */
201+ } else {
202+ /* first measure getting in */
203+ tp_vars->srtt = m << 3; /* take the measured time to be srtt */
204+ tp_vars->rttvar = m << 1; /* new_rtt / 2 */
205+ }
206+
207+ /* rto = srtt + 4 * rttvar.
208+ * rttvar is scaled by 4, therefore doesn't need to be multiplied
209+ */
210+ tp_vars->rto = (tp_vars->srtt >> 3) + tp_vars->rttvar;
211+}
212+
213+/**
214+ * batadv_tp_batctl_notify - send client status result to client
215+ * @reason: reason for tp meter session stop
216+ * @dst: destination of tp_meter session
217+ * @bat_priv: the bat priv with all the soft interface information
218+ * @start_time: start of transmission in jiffies
219+ * @total_sent: bytes acked to the receiver
220+ * @cookie: cookie of tp_meter session
221+ */
222+static void batadv_tp_batctl_notify(enum batadv_tp_meter_reason reason,
223+ const u8 *dst, struct batadv_priv *bat_priv,
224+ unsigned long start_time, u64 total_sent,
225+ u32 cookie)
226+{
227+ u32 test_time;
228+ u8 result;
229+ u32 total_bytes;
230+
231+ if (!batadv_tp_is_error(reason)) {
232+ result = BATADV_TP_REASON_COMPLETE;
233+ test_time = jiffies_to_msecs(jiffies - start_time);
234+ total_bytes = total_sent;
235+ } else {
236+ result = reason;
237+ test_time = 0;
238+ total_bytes = 0;
239+ }
240+
241+ batadv_netlink_tpmeter_notify(bat_priv, dst, result, test_time,
242+ total_bytes, cookie);
243+}
244+
245+/**
246+ * batadv_tp_batctl_error_notify - send client error result to client
247+ * @reason: reason for tp meter session stop
248+ * @dst: destination of tp_meter session
249+ * @bat_priv: the bat priv with all the soft interface information
250+ * @cookie: cookie of tp_meter session
251+ */
252+static void batadv_tp_batctl_error_notify(enum batadv_tp_meter_reason reason,
253+ const u8 *dst,
254+ struct batadv_priv *bat_priv,
255+ u32 cookie)
256+{
257+ batadv_tp_batctl_notify(reason, dst, bat_priv, 0, 0, cookie);
258+}
259+
260+/**
261+ * batadv_tp_list_find - find a tp_vars object in the global list
262+ * @bat_priv: the bat priv with all the soft interface information
263+ * @dst: the other endpoint MAC address to look for
264+ *
265+ * Look for a tp_vars object matching dst as end_point and return it after
266+ * having incremented the refcounter. Return NULL is not found
267+ *
268+ * Return: matching tp_vars or NULL when no tp_vars with @dst was found
269+ */
270+static struct batadv_tp_vars *batadv_tp_list_find(struct batadv_priv *bat_priv,
271+ const u8 *dst)
272+{
273+ struct batadv_tp_vars *pos, *tp_vars = NULL;
274+
275+ rcu_read_lock();
276+ hlist_for_each_entry_rcu(pos, &bat_priv->tp_list, list) {
277+ if (!batadv_compare_eth(pos->other_end, dst))
278+ continue;
279+
280+ /* most of the time this function is invoked during the normal
281+ * process..it makes sens to pay more when the session is
282+ * finished and to speed the process up during the measurement
283+ */
284+ if (unlikely(!kref_get_unless_zero(&pos->refcount)))
285+ continue;
286+
287+ tp_vars = pos;
288+ break;
289+ }
290+ rcu_read_unlock();
291+
292+ return tp_vars;
293+}
294+
295+/**
296+ * batadv_tp_list_find_session - find tp_vars session object in the global list
297+ * @bat_priv: the bat priv with all the soft interface information
298+ * @dst: the other endpoint MAC address to look for
299+ * @session: session identifier
300+ *
301+ * Look for a tp_vars object matching dst as end_point, session as tp meter
302+ * session and return it after having incremented the refcounter. Return NULL
303+ * is not found
304+ *
305+ * Return: matching tp_vars or NULL when no tp_vars was found
306+ */
307+static struct batadv_tp_vars *
308+batadv_tp_list_find_session(struct batadv_priv *bat_priv, const u8 *dst,
309+ const u8 *session)
310+{
311+ struct batadv_tp_vars *pos, *tp_vars = NULL;
312+
313+ rcu_read_lock();
314+ hlist_for_each_entry_rcu(pos, &bat_priv->tp_list, list) {
315+ if (!batadv_compare_eth(pos->other_end, dst))
316+ continue;
317+
318+ if (memcmp(pos->session, session, sizeof(pos->session)) != 0)
319+ continue;
320+
321+ /* most of the time this function is invoked during the normal
322+ * process..it makes sense to pay more when the session is
323+ * finished and to speed the process up during the measurement
324+ */
325+ if (unlikely(!kref_get_unless_zero(&pos->refcount)))
326+ continue;
327+
328+ tp_vars = pos;
329+ break;
330+ }
331+ rcu_read_unlock();
332+
333+ return tp_vars;
334+}
335+
336+/**
337+ * batadv_tp_vars_release - release batadv_tp_vars from lists and queue for
338+ * free after rcu grace period
339+ * @ref: kref pointer of the batadv_tp_vars
340+ */
341+static void batadv_tp_vars_release(struct kref *ref)
342+{
343+ struct batadv_tp_vars *tp_vars;
344+ struct batadv_tp_unacked *un, *safe;
345+
346+ tp_vars = container_of(ref, struct batadv_tp_vars, refcount);
347+
348+ /* lock should not be needed because this object is now out of any
349+ * context!
350+ */
351+ spin_lock_bh(&tp_vars->unacked_lock);
352+ list_for_each_entry_safe(un, safe, &tp_vars->unacked_list, list) {
353+ list_del(&un->list);
354+ kfree(un);
355+ }
356+ spin_unlock_bh(&tp_vars->unacked_lock);
357+
358+ kfree_rcu(tp_vars, rcu);
359+}
360+
361+/**
362+ * batadv_tp_vars_put - decrement the batadv_tp_vars refcounter and possibly
363+ * release it
364+ * @tp_vars: the private data of the current TP meter session to be free'd
365+ */
366+static void batadv_tp_vars_put(struct batadv_tp_vars *tp_vars)
367+{
368+ kref_put(&tp_vars->refcount, batadv_tp_vars_release);
369+}
370+
371+/**
372+ * batadv_tp_sender_cleanup - cleanup sender data and drop and timer
373+ * @bat_priv: the bat priv with all the soft interface information
374+ * @tp_vars: the private data of the current TP meter session to cleanup
375+ */
376+static void batadv_tp_sender_cleanup(struct batadv_priv *bat_priv,
377+ struct batadv_tp_vars *tp_vars)
378+{
379+ cancel_delayed_work(&tp_vars->finish_work);
380+
381+ spin_lock_bh(&tp_vars->bat_priv->tp_list_lock);
382+ hlist_del_rcu(&tp_vars->list);
383+ spin_unlock_bh(&tp_vars->bat_priv->tp_list_lock);
384+
385+ /* drop list reference */
386+ batadv_tp_vars_put(tp_vars);
387+
388+ atomic_dec(&tp_vars->bat_priv->tp_num);
389+
390+ /* kill the timer and remove its reference */
391+ del_timer_sync(&tp_vars->timer);
392+ /* the worker might have rearmed itself therefore we kill it again. Note
393+ * that if the worker should run again before invoking the following
394+ * del_timer(), it would not re-arm itself once again because the status
395+ * is OFF now
396+ */
397+ del_timer(&tp_vars->timer);
398+ batadv_tp_vars_put(tp_vars);
399+}
400+
401+/**
402+ * batadv_tp_sender_end - print info about ended session and inform client
403+ * @bat_priv: the bat priv with all the soft interface information
404+ * @tp_vars: the private data of the current TP meter session
405+ */
406+static void batadv_tp_sender_end(struct batadv_priv *bat_priv,
407+ struct batadv_tp_vars *tp_vars)
408+{
409+ u32 session_cookie;
410+
411+ batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
412+ "Test towards %pM finished..shutting down (reason=%d)\n",
413+ tp_vars->other_end, tp_vars->reason);
414+
415+ batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
416+ "Last timing stats: SRTT=%ums RTTVAR=%ums RTO=%ums\n",
417+ tp_vars->srtt >> 3, tp_vars->rttvar >> 2, tp_vars->rto);
418+
419+ batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
420+ "Final values: cwnd=%u ss_threshold=%u\n",
421+ tp_vars->cwnd, tp_vars->ss_threshold);
422+
423+ session_cookie = batadv_tp_session_cookie(tp_vars->session,
424+ tp_vars->icmp_uid);
425+
426+ batadv_tp_batctl_notify(tp_vars->reason,
427+ tp_vars->other_end,
428+ bat_priv,
429+ tp_vars->start_time,
430+ atomic64_read(&tp_vars->tot_sent),
431+ session_cookie);
432+}
433+
434+/**
435+ * batadv_tp_sender_shutdown - let sender thread/timer stop gracefully
436+ * @tp_vars: the private data of the current TP meter session
437+ * @reason: reason for tp meter session stop
438+ */
439+static void batadv_tp_sender_shutdown(struct batadv_tp_vars *tp_vars,
440+ enum batadv_tp_meter_reason reason)
441+{
442+ if (!atomic_dec_and_test(&tp_vars->sending))
443+ return;
444+
445+ tp_vars->reason = reason;
446+}
447+
448+/**
449+ * batadv_tp_sender_finish - stop sender session after test_length was reached
450+ * @work: delayed work reference of the related tp_vars
451+ */
452+static void batadv_tp_sender_finish(struct work_struct *work)
453+{
454+ struct delayed_work *delayed_work;
455+ struct batadv_tp_vars *tp_vars;
456+
457+ delayed_work = to_delayed_work(work);
458+ tp_vars = container_of(delayed_work, struct batadv_tp_vars,
459+ finish_work);
460+
461+ batadv_tp_sender_shutdown(tp_vars, BATADV_TP_REASON_COMPLETE);
462+}
463+
464+/**
465+ * batadv_tp_reset_sender_timer - reschedule the sender timer
466+ * @tp_vars: the private TP meter data for this session
467+ *
468+ * Reschedule the timer using tp_vars->rto as delay
469+ */
470+static void batadv_tp_reset_sender_timer(struct batadv_tp_vars *tp_vars)
471+{
472+ /* most of the time this function is invoked while normal packet
473+ * reception...
474+ */
475+ if (unlikely(atomic_read(&tp_vars->sending) == 0))
476+ /* timer ref will be dropped in batadv_tp_sender_cleanup */
477+ return;
478+
479+ mod_timer(&tp_vars->timer, jiffies + msecs_to_jiffies(tp_vars->rto));
480+}
481+
482+/**
483+ * batadv_tp_sender_timeout - timer that fires in case of packet loss
484+ * @arg: address of the related tp_vars
485+ *
486+ * If fired it means that there was packet loss.
487+ * Switch to Slow Start, set the ss_threshold to half of the current cwnd and
488+ * reset the cwnd to 3*MSS
489+ */
490+static void batadv_tp_sender_timeout(unsigned long arg)
491+{
492+ struct batadv_tp_vars *tp_vars = (struct batadv_tp_vars *)arg;
493+ struct batadv_priv *bat_priv = tp_vars->bat_priv;
494+
495+ if (atomic_read(&tp_vars->sending) == 0)
496+ return;
497+
498+ /* if the user waited long enough...shutdown the test */
499+ if (unlikely(tp_vars->rto >= BATADV_TP_MAX_RTO)) {
500+ batadv_tp_sender_shutdown(tp_vars,
501+ BATADV_TP_REASON_DST_UNREACHABLE);
502+ return;
503+ }
504+
505+ /* RTO exponential backoff
506+ * Details in Section 5.5 of RFC6298
507+ */
508+ tp_vars->rto <<= 1;
509+
510+ spin_lock_bh(&tp_vars->cwnd_lock);
511+
512+ tp_vars->ss_threshold = tp_vars->cwnd >> 1;
513+ if (tp_vars->ss_threshold < BATADV_TP_PLEN * 2)
514+ tp_vars->ss_threshold = BATADV_TP_PLEN * 2;
515+
516+ batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
517+ "Meter: RTO fired during test towards %pM! cwnd=%u new ss_thr=%u, resetting last_sent to %u\n",
518+ tp_vars->other_end, tp_vars->cwnd, tp_vars->ss_threshold,
519+ atomic_read(&tp_vars->last_acked));
520+
521+ tp_vars->cwnd = BATADV_TP_PLEN * 3;
522+
523+ spin_unlock_bh(&tp_vars->cwnd_lock);
524+
525+ /* resend the non-ACKed packets.. */
526+ tp_vars->last_sent = atomic_read(&tp_vars->last_acked);
527+ wake_up(&tp_vars->more_bytes);
528+
529+ batadv_tp_reset_sender_timer(tp_vars);
530+}
531+
532+/**
533+ * batadv_tp_fill_prerandom - Fill buffer with prefetched random bytes
534+ * @tp_vars: the private TP meter data for this session
535+ * @buf: Buffer to fill with bytes
536+ * @nbytes: amount of pseudorandom bytes
537+ */
538+static void batadv_tp_fill_prerandom(struct batadv_tp_vars *tp_vars,
539+ u8 *buf, size_t nbytes)
540+{
541+ u32 local_offset;
542+ size_t bytes_inbuf;
543+ size_t to_copy;
544+ size_t pos = 0;
545+
546+ spin_lock_bh(&tp_vars->prerandom_lock);
547+ local_offset = tp_vars->prerandom_offset;
548+ tp_vars->prerandom_offset += nbytes;
549+ tp_vars->prerandom_offset %= sizeof(batadv_tp_prerandom);
550+ spin_unlock_bh(&tp_vars->prerandom_lock);
551+
552+ while (nbytes) {
553+ local_offset %= sizeof(batadv_tp_prerandom);
554+ bytes_inbuf = sizeof(batadv_tp_prerandom) - local_offset;
555+ to_copy = min(nbytes, bytes_inbuf);
556+
557+ memcpy(&buf[pos], &batadv_tp_prerandom[local_offset], to_copy);
558+ pos += to_copy;
559+ nbytes -= to_copy;
560+ local_offset = 0;
561+ }
562+}
563+
564+/**
565+ * batadv_tp_send_msg - send a single message
566+ * @tp_vars: the private TP meter data for this session
567+ * @src: source mac address
568+ * @orig_node: the originator of the destination
569+ * @seqno: sequence number of this packet
570+ * @len: length of the entire packet
571+ * @session: session identifier
572+ * @uid: local ICMP "socket" index
573+ * @timestamp: timestamp in jiffies which is replied in ack
574+ *
575+ * Create and send a single TP Meter message.
576+ *
577+ * Return: 0 on success, BATADV_TP_REASON_DST_UNREACHABLE if the destination is
578+ * not reachable, BATADV_TP_REASON_MEMORY_ERROR if the packet couldn't be
579+ * allocated
580+ */
581+static int batadv_tp_send_msg(struct batadv_tp_vars *tp_vars, const u8 *src,
582+ struct batadv_orig_node *orig_node,
583+ u32 seqno, size_t len, const u8 *session,
584+ int uid, u32 timestamp)
585+{
586+ struct batadv_icmp_tp_packet *icmp;
587+ struct sk_buff *skb;
588+ int r;
589+ u8 *data;
590+ size_t data_len;
591+
592+ skb = netdev_alloc_skb_ip_align(NULL, len + ETH_HLEN);
593+ if (unlikely(!skb))
594+ return BATADV_TP_REASON_MEMORY_ERROR;
595+
596+ skb_reserve(skb, ETH_HLEN);
597+ icmp = (struct batadv_icmp_tp_packet *)skb_put(skb, sizeof(*icmp));
598+
599+ /* fill the icmp header */
600+ ether_addr_copy(icmp->dst, orig_node->orig);
601+ ether_addr_copy(icmp->orig, src);
602+ icmp->version = BATADV_COMPAT_VERSION;
603+ icmp->packet_type = BATADV_ICMP;
604+ icmp->ttl = BATADV_TTL;
605+ icmp->msg_type = BATADV_TP;
606+ icmp->uid = uid;
607+
608+ icmp->subtype = BATADV_TP_MSG;
609+ memcpy(icmp->session, session, sizeof(icmp->session));
610+ icmp->seqno = htonl(seqno);
611+ icmp->timestamp = htonl(timestamp);
612+
613+ data_len = len - sizeof(*icmp);
614+ data = (u8 *)skb_put(skb, data_len);
615+ batadv_tp_fill_prerandom(tp_vars, data, data_len);
616+
617+ r = batadv_send_skb_to_orig(skb, orig_node, NULL);
618+ if (r == -1)
619+ kfree_skb(skb);
620+
621+ if (r == NET_XMIT_SUCCESS)
622+ return 0;
623+
624+ return BATADV_TP_REASON_CANT_SEND;
625+}
626+
627+/**
628+ * batadv_tp_recv_ack - ACK receiving function
629+ * @bat_priv: the bat priv with all the soft interface information
630+ * @skb: the buffer containing the received packet
631+ *
632+ * Process a received TP ACK packet
633+ */
634+static void batadv_tp_recv_ack(struct batadv_priv *bat_priv,
635+ const struct sk_buff *skb)
636+{
637+ struct batadv_hard_iface *primary_if = NULL;
638+ struct batadv_orig_node *orig_node = NULL;
639+ const struct batadv_icmp_tp_packet *icmp;
640+ struct batadv_tp_vars *tp_vars;
641+ size_t packet_len, mss;
642+ u32 rtt, recv_ack, cwnd;
643+ unsigned char *dev_addr;
644+
645+ packet_len = BATADV_TP_PLEN;
646+ mss = BATADV_TP_PLEN;
647+ packet_len += sizeof(struct batadv_unicast_packet);
648+
649+ icmp = (struct batadv_icmp_tp_packet *)skb->data;
650+
651+ /* find the tp_vars */
652+ tp_vars = batadv_tp_list_find_session(bat_priv, icmp->orig,
653+ icmp->session);
654+ if (unlikely(!tp_vars))
655+ return;
656+
657+ if (unlikely(atomic_read(&tp_vars->sending) == 0))
658+ goto out;
659+
660+ /* old ACK? silently drop it.. */
661+ if (batadv_seq_before(ntohl(icmp->seqno),
662+ (u32)atomic_read(&tp_vars->last_acked)))
663+ goto out;
664+
665+ primary_if = batadv_primary_if_get_selected(bat_priv);
666+ if (unlikely(!primary_if))
667+ goto out;
668+
669+ orig_node = batadv_orig_hash_find(bat_priv, icmp->orig);
670+ if (unlikely(!orig_node))
671+ goto out;
672+
673+ /* update RTO with the new sampled RTT, if any */
674+ rtt = jiffies_to_msecs(jiffies) - ntohl(icmp->timestamp);
675+ if (icmp->timestamp && rtt)
676+ batadv_tp_update_rto(tp_vars, rtt);
677+
678+ /* ACK for new data... reset the timer */
679+ batadv_tp_reset_sender_timer(tp_vars);
680+
681+ recv_ack = ntohl(icmp->seqno);
682+
683+ /* check if this ACK is a duplicate */
684+ if (atomic_read(&tp_vars->last_acked) == recv_ack) {
685+ atomic_inc(&tp_vars->dup_acks);
686+ if (atomic_read(&tp_vars->dup_acks) != 3)
687+ goto out;
688+
689+ if (recv_ack >= tp_vars->recover)
690+ goto out;
691+
692+ /* if this is the third duplicate ACK do Fast Retransmit */
693+ batadv_tp_send_msg(tp_vars, primary_if->net_dev->dev_addr,
694+ orig_node, recv_ack, packet_len,
695+ icmp->session, icmp->uid,
696+ jiffies_to_msecs(jiffies));
697+
698+ spin_lock_bh(&tp_vars->cwnd_lock);
699+
700+ /* Fast Recovery */
701+ tp_vars->fast_recovery = true;
702+ /* Set recover to the last outstanding seqno when Fast Recovery
703+ * is entered. RFC6582, Section 3.2, step 1
704+ */
705+ tp_vars->recover = tp_vars->last_sent;
706+ tp_vars->ss_threshold = tp_vars->cwnd >> 1;
707+ batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
708+ "Meter: Fast Recovery, (cur cwnd=%u) ss_thr=%u last_sent=%u recv_ack=%u\n",
709+ tp_vars->cwnd, tp_vars->ss_threshold,
710+ tp_vars->last_sent, recv_ack);
711+ tp_vars->cwnd = batadv_tp_cwnd(tp_vars->ss_threshold, 3 * mss,
712+ mss);
713+ tp_vars->dec_cwnd = 0;
714+ tp_vars->last_sent = recv_ack;
715+
716+ spin_unlock_bh(&tp_vars->cwnd_lock);
717+ } else {
718+ /* count the acked data */
719+ atomic64_add(recv_ack - atomic_read(&tp_vars->last_acked),
720+ &tp_vars->tot_sent);
721+ /* reset the duplicate ACKs counter */
722+ atomic_set(&tp_vars->dup_acks, 0);
723+
724+ if (tp_vars->fast_recovery) {
725+ /* partial ACK */
726+ if (batadv_seq_before(recv_ack, tp_vars->recover)) {
727+ /* this is another hole in the window. React
728+ * immediately as specified by NewReno (see
729+ * Section 3.2 of RFC6582 for details)
730+ */
731+ dev_addr = primary_if->net_dev->dev_addr;
732+ batadv_tp_send_msg(tp_vars, dev_addr,
733+ orig_node, recv_ack,
734+ packet_len, icmp->session,
735+ icmp->uid,
736+ jiffies_to_msecs(jiffies));
737+ tp_vars->cwnd = batadv_tp_cwnd(tp_vars->cwnd,
738+ mss, mss);
739+ } else {
740+ tp_vars->fast_recovery = false;
741+ /* set cwnd to the value of ss_threshold at the
742+ * moment that Fast Recovery was entered.
743+ * RFC6582, Section 3.2, step 3
744+ */
745+ cwnd = batadv_tp_cwnd(tp_vars->ss_threshold, 0,
746+ mss);
747+ tp_vars->cwnd = cwnd;
748+ }
749+ goto move_twnd;
750+ }
751+
752+ if (recv_ack - atomic_read(&tp_vars->last_acked) >= mss)
753+ batadv_tp_update_cwnd(tp_vars, mss);
754+move_twnd:
755+ /* move the Transmit Window */
756+ atomic_set(&tp_vars->last_acked, recv_ack);
757+ }
758+
759+ wake_up(&tp_vars->more_bytes);
760+out:
761+ if (likely(primary_if))
762+ batadv_hardif_put(primary_if);
763+ if (likely(orig_node))
764+ batadv_orig_node_put(orig_node);
765+ if (likely(tp_vars))
766+ batadv_tp_vars_put(tp_vars);
767+}
768+
769+/**
770+ * batadv_tp_avail - check if congestion window is not full
771+ * @tp_vars: the private data of the current TP meter session
772+ * @payload_len: size of the payload of a single message
773+ *
774+ * Return: true when congestion window is not full, false otherwise
775+ */
776+static bool batadv_tp_avail(struct batadv_tp_vars *tp_vars,
777+ size_t payload_len)
778+{
779+ u32 win_left, win_limit;
780+
781+ win_limit = atomic_read(&tp_vars->last_acked) + tp_vars->cwnd;
782+ win_left = win_limit - tp_vars->last_sent;
783+
784+ return win_left >= payload_len;
785+}
786+
787+/**
788+ * batadv_tp_wait_available - wait until congestion window becomes free or
789+ * timeout is reached
790+ * @tp_vars: the private data of the current TP meter session
791+ * @plen: size of the payload of a single message
792+ *
793+ * Return: 0 if the condition evaluated to false after the timeout elapsed,
794+ * 1 if the condition evaluated to true after the timeout elapsed, the
795+ * remaining jiffies (at least 1) if the condition evaluated to true before
796+ * the timeout elapsed, or -ERESTARTSYS if it was interrupted by a signal.
797+ */
798+static int batadv_tp_wait_available(struct batadv_tp_vars *tp_vars, size_t plen)
799+{
800+ int ret;
801+
802+ ret = wait_event_interruptible_timeout(tp_vars->more_bytes,
803+ batadv_tp_avail(tp_vars, plen),
804+ HZ / 10);
805+
806+ return ret;
807+}
808+
809+/**
810+ * batadv_tp_send - main sending thread of a tp meter session
811+ * @arg: address of the related tp_vars
812+ *
813+ * Return: nothing, this function never returns
814+ */
815+static int batadv_tp_send(void *arg)
816+{
817+ struct batadv_tp_vars *tp_vars = arg;
818+ struct batadv_priv *bat_priv = tp_vars->bat_priv;
819+ struct batadv_hard_iface *primary_if = NULL;
820+ struct batadv_orig_node *orig_node = NULL;
821+ size_t payload_len, packet_len;
822+ int err = 0;
823+
824+ if (unlikely(tp_vars->role != BATADV_TP_SENDER)) {
825+ err = BATADV_TP_REASON_DST_UNREACHABLE;
826+ tp_vars->reason = err;
827+ goto out;
828+ }
829+
830+ orig_node = batadv_orig_hash_find(bat_priv, tp_vars->other_end);
831+ if (unlikely(!orig_node)) {
832+ err = BATADV_TP_REASON_DST_UNREACHABLE;
833+ tp_vars->reason = err;
834+ goto out;
835+ }
836+
837+ primary_if = batadv_primary_if_get_selected(bat_priv);
838+ if (unlikely(!primary_if)) {
839+ err = BATADV_TP_REASON_DST_UNREACHABLE;
840+ goto out;
841+ }
842+
843+ /* assume that all the hard_interfaces have a correctly
844+ * configured MTU, so use the soft_iface MTU as MSS.
845+ * This might not be true and in that case the fragmentation
846+ * should be used.
847+ * Now, try to send the packet as it is
848+ */
849+ payload_len = BATADV_TP_PLEN;
850+ BUILD_BUG_ON(sizeof(struct batadv_icmp_tp_packet) > BATADV_TP_PLEN);
851+
852+ batadv_tp_reset_sender_timer(tp_vars);
853+
854+ /* queue the worker in charge of terminating the test */
855+ queue_delayed_work(batadv_event_workqueue, &tp_vars->finish_work,
856+ msecs_to_jiffies(tp_vars->test_length));
857+
858+ while (atomic_read(&tp_vars->sending) != 0) {
859+ if (unlikely(!batadv_tp_avail(tp_vars, payload_len))) {
860+ batadv_tp_wait_available(tp_vars, payload_len);
861+ continue;
862+ }
863+
864+ /* to emulate normal unicast traffic, add to the payload len
865+ * the size of the unicast header
866+ */
867+ packet_len = payload_len + sizeof(struct batadv_unicast_packet);
868+
869+ err = batadv_tp_send_msg(tp_vars, primary_if->net_dev->dev_addr,
870+ orig_node, tp_vars->last_sent,
871+ packet_len,
872+ tp_vars->session, tp_vars->icmp_uid,
873+ jiffies_to_msecs(jiffies));
874+
875+ /* something went wrong during the preparation/transmission */
876+ if (unlikely(err && err != BATADV_TP_REASON_CANT_SEND)) {
877+ batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
878+ "Meter: batadv_tp_send() cannot send packets (%d)\n",
879+ err);
880+ /* ensure nobody else tries to stop the thread now */
881+ if (atomic_dec_and_test(&tp_vars->sending))
882+ tp_vars->reason = err;
883+ break;
884+ }
885+
886+ /* right-shift the TWND */
887+ if (!err)
888+ tp_vars->last_sent += payload_len;
889+
890+ cond_resched();
891+ }
892+
893+out:
894+ if (likely(primary_if))
895+ batadv_hardif_put(primary_if);
896+ if (likely(orig_node))
897+ batadv_orig_node_put(orig_node);
898+
899+ batadv_tp_sender_end(bat_priv, tp_vars);
900+ batadv_tp_sender_cleanup(bat_priv, tp_vars);
901+
902+ batadv_tp_vars_put(tp_vars);
903+
904+ do_exit(0);
905+}
906+
907+/**
908+ * batadv_tp_start_kthread - start new thread which manages the tp meter sender
909+ * @tp_vars: the private data of the current TP meter session
910+ */
911+static void batadv_tp_start_kthread(struct batadv_tp_vars *tp_vars)
912+{
913+ struct task_struct *kthread;
914+ struct batadv_priv *bat_priv = tp_vars->bat_priv;
915+ u32 session_cookie;
916+
917+ kref_get(&tp_vars->refcount);
918+ kthread = kthread_create(batadv_tp_send, tp_vars, "kbatadv_tp_meter");
919+ if (IS_ERR(kthread)) {
920+ session_cookie = batadv_tp_session_cookie(tp_vars->session,
921+ tp_vars->icmp_uid);
922+ pr_err("batadv: cannot create tp meter kthread\n");
923+ batadv_tp_batctl_error_notify(BATADV_TP_REASON_MEMORY_ERROR,
924+ tp_vars->other_end,
925+ bat_priv, session_cookie);
926+
927+ /* drop reserved reference for kthread */
928+ batadv_tp_vars_put(tp_vars);
929+
930+ /* cleanup of failed tp meter variables */
931+ batadv_tp_sender_cleanup(bat_priv, tp_vars);
932+ return;
933+ }
934+
935+ wake_up_process(kthread);
936+}
937+
938+/**
939+ * batadv_tp_start - start a new tp meter session
940+ * @bat_priv: the bat priv with all the soft interface information
941+ * @dst: the receiver MAC address
942+ * @test_length: test length in milliseconds
943+ * @cookie: session cookie
944+ */
945+void batadv_tp_start(struct batadv_priv *bat_priv, const u8 *dst,
946+ u32 test_length, u32 *cookie)
947+{
948+ struct batadv_tp_vars *tp_vars;
949+ u8 session_id[2];
950+ u8 icmp_uid;
951+ u32 session_cookie;
952+
953+ get_random_bytes(session_id, sizeof(session_id));
954+ get_random_bytes(&icmp_uid, 1);
955+ session_cookie = batadv_tp_session_cookie(session_id, icmp_uid);
956+ *cookie = session_cookie;
957+
958+ /* look for an already existing test towards this node */
959+ spin_lock_bh(&bat_priv->tp_list_lock);
960+ tp_vars = batadv_tp_list_find(bat_priv, dst);
961+ if (tp_vars) {
962+ spin_unlock_bh(&bat_priv->tp_list_lock);
963+ batadv_tp_vars_put(tp_vars);
964+ batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
965+ "Meter: test to or from the same node already ongoing, aborting\n");
966+ batadv_tp_batctl_error_notify(BATADV_TP_REASON_ALREADY_ONGOING,
967+ dst, bat_priv, session_cookie);
968+ return;
969+ }
970+
971+ if (!atomic_add_unless(&bat_priv->tp_num, 1, BATADV_TP_MAX_NUM)) {
972+ spin_unlock_bh(&bat_priv->tp_list_lock);
973+ batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
974+ "Meter: too many ongoing sessions, aborting (SEND)\n");
975+ batadv_tp_batctl_error_notify(BATADV_TP_REASON_TOO_MANY, dst,
976+ bat_priv, session_cookie);
977+ return;
978+ }
979+
980+ tp_vars = kmalloc(sizeof(*tp_vars), GFP_ATOMIC);
981+ if (!tp_vars) {
982+ spin_unlock_bh(&bat_priv->tp_list_lock);
983+ batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
984+ "Meter: batadv_tp_start cannot allocate list elements\n");
985+ batadv_tp_batctl_error_notify(BATADV_TP_REASON_MEMORY_ERROR,
986+ dst, bat_priv, session_cookie);
987+ return;
988+ }
989+
990+ /* initialize tp_vars */
991+ ether_addr_copy(tp_vars->other_end, dst);
992+ kref_init(&tp_vars->refcount);
993+ tp_vars->role = BATADV_TP_SENDER;
994+ atomic_set(&tp_vars->sending, 1);
995+ memcpy(tp_vars->session, session_id, sizeof(session_id));
996+ tp_vars->icmp_uid = icmp_uid;
997+
998+ tp_vars->last_sent = BATADV_TP_FIRST_SEQ;
999+ atomic_set(&tp_vars->last_acked, BATADV_TP_FIRST_SEQ);
1000+ tp_vars->fast_recovery = false;
1001+ tp_vars->recover = BATADV_TP_FIRST_SEQ;
1002+
1003+ /* initialise the CWND to 3*MSS (Section 3.1 in RFC5681).
1004+ * For batman-adv the MSS is the size of the payload received by the
1005+ * soft_interface, hence its MTU
1006+ */
1007+ tp_vars->cwnd = BATADV_TP_PLEN * 3;
1008+ /* at the beginning initialise the SS threshold to the biggest possible
1009+ * window size, hence the AWND size
1010+ */
1011+ tp_vars->ss_threshold = BATADV_TP_AWND;
1012+
1013+ /* RTO initial value is 3 seconds.
1014+ * Details in Section 2.1 of RFC6298
1015+ */
1016+ tp_vars->rto = 1000;
1017+ tp_vars->srtt = 0;
1018+ tp_vars->rttvar = 0;
1019+
1020+ atomic64_set(&tp_vars->tot_sent, 0);
1021+
1022+ kref_get(&tp_vars->refcount);
1023+ setup_timer(&tp_vars->timer, batadv_tp_sender_timeout,
1024+ (unsigned long)tp_vars);
1025+
1026+ tp_vars->bat_priv = bat_priv;
1027+ tp_vars->start_time = jiffies;
1028+
1029+ init_waitqueue_head(&tp_vars->more_bytes);
1030+
1031+ spin_lock_init(&tp_vars->unacked_lock);
1032+ INIT_LIST_HEAD(&tp_vars->unacked_list);
1033+
1034+ spin_lock_init(&tp_vars->cwnd_lock);
1035+
1036+ tp_vars->prerandom_offset = 0;
1037+ spin_lock_init(&tp_vars->prerandom_lock);
1038+
1039+ kref_get(&tp_vars->refcount);
1040+ hlist_add_head_rcu(&tp_vars->list, &bat_priv->tp_list);
1041+ spin_unlock_bh(&bat_priv->tp_list_lock);
1042+
1043+ tp_vars->test_length = test_length;
1044+ if (!tp_vars->test_length)
1045+ tp_vars->test_length = BATADV_TP_DEF_TEST_LENGTH;
1046+
1047+ batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
1048+ "Meter: starting throughput meter towards %pM (length=%ums)\n",
1049+ dst, test_length);
1050+
1051+ /* init work item for finished tp tests */
1052+ INIT_DELAYED_WORK(&tp_vars->finish_work, batadv_tp_sender_finish);
1053+
1054+ /* start tp kthread. This way the write() call issued from userspace can
1055+ * happily return and avoid to block
1056+ */
1057+ batadv_tp_start_kthread(tp_vars);
1058+
1059+ /* don't return reference to new tp_vars */
1060+ batadv_tp_vars_put(tp_vars);
1061+}
1062+
1063+/**
1064+ * batadv_tp_stop - stop currently running tp meter session
1065+ * @bat_priv: the bat priv with all the soft interface information
1066+ * @dst: the receiver MAC address
1067+ * @return_value: reason for tp meter session stop
1068+ */
1069+void batadv_tp_stop(struct batadv_priv *bat_priv, const u8 *dst,
1070+ u8 return_value)
1071+{
1072+ struct batadv_orig_node *orig_node;
1073+ struct batadv_tp_vars *tp_vars;
1074+
1075+ batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
1076+ "Meter: stopping test towards %pM\n", dst);
1077+
1078+ orig_node = batadv_orig_hash_find(bat_priv, dst);
1079+ if (!orig_node)
1080+ return;
1081+
1082+ tp_vars = batadv_tp_list_find(bat_priv, orig_node->orig);
1083+ if (!tp_vars) {
1084+ batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
1085+ "Meter: trying to interrupt an already over connection\n");
1086+ goto out;
1087+ }
1088+
1089+ batadv_tp_sender_shutdown(tp_vars, return_value);
1090+ batadv_tp_vars_put(tp_vars);
1091+out:
1092+ batadv_orig_node_put(orig_node);
1093+}
1094+
1095+/**
1096+ * batadv_tp_reset_receiver_timer - reset the receiver shutdown timer
1097+ * @tp_vars: the private data of the current TP meter session
1098+ *
1099+ * start the receiver shutdown timer or reset it if already started
1100+ */
1101+static void batadv_tp_reset_receiver_timer(struct batadv_tp_vars *tp_vars)
1102+{
1103+ mod_timer(&tp_vars->timer,
1104+ jiffies + msecs_to_jiffies(BATADV_TP_RECV_TIMEOUT));
1105+}
1106+
1107+/**
1108+ * batadv_tp_receiver_shutdown - stop a tp meter receiver when timeout is
1109+ * reached without received ack
1110+ * @arg: address of the related tp_vars
1111+ */
1112+static void batadv_tp_receiver_shutdown(unsigned long arg)
1113+{
1114+ struct batadv_tp_vars *tp_vars = (struct batadv_tp_vars *)arg;
1115+ struct batadv_tp_unacked *un, *safe;
1116+ struct batadv_priv *bat_priv;
1117+
1118+ bat_priv = tp_vars->bat_priv;
1119+
1120+ /* if there is recent activity rearm the timer */
1121+ if (!batadv_has_timed_out(tp_vars->last_recv_time,
1122+ BATADV_TP_RECV_TIMEOUT)) {
1123+ /* reset the receiver shutdown timer */
1124+ batadv_tp_reset_receiver_timer(tp_vars);
1125+ return;
1126+ }
1127+
1128+ batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
1129+ "Shutting down for inactivity (more than %dms) from %pM\n",
1130+ BATADV_TP_RECV_TIMEOUT, tp_vars->other_end);
1131+
1132+ spin_lock_bh(&tp_vars->bat_priv->tp_list_lock);
1133+ hlist_del_rcu(&tp_vars->list);
1134+ spin_unlock_bh(&tp_vars->bat_priv->tp_list_lock);
1135+
1136+ /* drop list reference */
1137+ batadv_tp_vars_put(tp_vars);
1138+
1139+ atomic_dec(&bat_priv->tp_num);
1140+
1141+ spin_lock_bh(&tp_vars->unacked_lock);
1142+ list_for_each_entry_safe(un, safe, &tp_vars->unacked_list, list) {
1143+ list_del(&un->list);
1144+ kfree(un);
1145+ }
1146+ spin_unlock_bh(&tp_vars->unacked_lock);
1147+
1148+ /* drop reference of timer */
1149+ batadv_tp_vars_put(tp_vars);
1150+}
1151+
1152+/**
1153+ * batadv_tp_send_ack - send an ACK packet
1154+ * @bat_priv: the bat priv with all the soft interface information
1155+ * @dst: the mac address of the destination originator
1156+ * @seq: the sequence number to ACK
1157+ * @timestamp: the timestamp to echo back in the ACK
1158+ * @session: session identifier
1159+ * @socket_index: local ICMP socket identifier
1160+ *
1161+ * Return: 0 on success, a positive integer representing the reason of the
1162+ * failure otherwise
1163+ */
1164+static int batadv_tp_send_ack(struct batadv_priv *bat_priv, const u8 *dst,
1165+ u32 seq, __be32 timestamp, const u8 *session,
1166+ int socket_index)
1167+{
1168+ struct batadv_hard_iface *primary_if = NULL;
1169+ struct batadv_orig_node *orig_node;
1170+ struct batadv_icmp_tp_packet *icmp;
1171+ struct sk_buff *skb;
1172+ int r, ret;
1173+
1174+ orig_node = batadv_orig_hash_find(bat_priv, dst);
1175+ if (unlikely(!orig_node)) {
1176+ ret = BATADV_TP_REASON_DST_UNREACHABLE;
1177+ goto out;
1178+ }
1179+
1180+ primary_if = batadv_primary_if_get_selected(bat_priv);
1181+ if (unlikely(!primary_if)) {
1182+ ret = BATADV_TP_REASON_DST_UNREACHABLE;
1183+ goto out;
1184+ }
1185+
1186+ skb = netdev_alloc_skb_ip_align(NULL, sizeof(*icmp) + ETH_HLEN);
1187+ if (unlikely(!skb)) {
1188+ ret = BATADV_TP_REASON_MEMORY_ERROR;
1189+ goto out;
1190+ }
1191+
1192+ skb_reserve(skb, ETH_HLEN);
1193+ icmp = (struct batadv_icmp_tp_packet *)skb_put(skb, sizeof(*icmp));
1194+ icmp->packet_type = BATADV_ICMP;
1195+ icmp->version = BATADV_COMPAT_VERSION;
1196+ icmp->ttl = BATADV_TTL;
1197+ icmp->msg_type = BATADV_TP;
1198+ ether_addr_copy(icmp->dst, orig_node->orig);
1199+ ether_addr_copy(icmp->orig, primary_if->net_dev->dev_addr);
1200+ icmp->uid = socket_index;
1201+
1202+ icmp->subtype = BATADV_TP_ACK;
1203+ memcpy(icmp->session, session, sizeof(icmp->session));
1204+ icmp->seqno = htonl(seq);
1205+ icmp->timestamp = timestamp;
1206+
1207+ /* send the ack */
1208+ r = batadv_send_skb_to_orig(skb, orig_node, NULL);
1209+ if (r == -1)
1210+ kfree_skb(skb);
1211+
1212+ if (unlikely(r < 0) || (r == NET_XMIT_DROP)) {
1213+ ret = BATADV_TP_REASON_DST_UNREACHABLE;
1214+ goto out;
1215+ }
1216+ ret = 0;
1217+
1218+out:
1219+ if (likely(orig_node))
1220+ batadv_orig_node_put(orig_node);
1221+ if (likely(primary_if))
1222+ batadv_hardif_put(primary_if);
1223+
1224+ return ret;
1225+}
1226+
1227+/**
1228+ * batadv_tp_handle_out_of_order - store an out of order packet
1229+ * @tp_vars: the private data of the current TP meter session
1230+ * @skb: the buffer containing the received packet
1231+ *
1232+ * Store the out of order packet in the unacked list for late processing. This
1233+ * packets are kept in this list so that they can be ACKed at once as soon as
1234+ * all the previous packets have been received
1235+ *
1236+ * Return: true if the packed has been successfully processed, false otherwise
1237+ */
1238+static bool batadv_tp_handle_out_of_order(struct batadv_tp_vars *tp_vars,
1239+ const struct sk_buff *skb)
1240+{
1241+ const struct batadv_icmp_tp_packet *icmp;
1242+ struct batadv_tp_unacked *un, *new;
1243+ u32 payload_len;
1244+ bool added = false;
1245+
1246+ new = kmalloc(sizeof(*new), GFP_ATOMIC);
1247+ if (unlikely(!new))
1248+ return false;
1249+
1250+ icmp = (struct batadv_icmp_tp_packet *)skb->data;
1251+
1252+ new->seqno = ntohl(icmp->seqno);
1253+ payload_len = skb->len - sizeof(struct batadv_unicast_packet);
1254+ new->len = payload_len;
1255+
1256+ spin_lock_bh(&tp_vars->unacked_lock);
1257+ /* if the list is empty immediately attach this new object */
1258+ if (list_empty(&tp_vars->unacked_list)) {
1259+ list_add(&new->list, &tp_vars->unacked_list);
1260+ goto out;
1261+ }
1262+
1263+ /* otherwise loop over the list and either drop the packet because this
1264+ * is a duplicate or store it at the right position.
1265+ *
1266+ * The iteration is done in the reverse way because it is likely that
1267+ * the last received packet (the one being processed now) has a bigger
1268+ * seqno than all the others already stored.
1269+ */
1270+ list_for_each_entry_reverse(un, &tp_vars->unacked_list, list) {
1271+ /* check for duplicates */
1272+ if (new->seqno == un->seqno) {
1273+ if (new->len > un->len)
1274+ un->len = new->len;
1275+ kfree(new);
1276+ added = true;
1277+ break;
1278+ }
1279+
1280+ /* look for the right position */
1281+ if (batadv_seq_before(new->seqno, un->seqno))
1282+ continue;
1283+
1284+ /* as soon as an entry having a bigger seqno is found, the new
1285+ * one is attached _after_ it. In this way the list is kept in
1286+ * ascending order
1287+ */
1288+ list_add_tail(&new->list, &un->list);
1289+ added = true;
1290+ break;
1291+ }
1292+
1293+ /* received packet with smallest seqno out of order; add it to front */
1294+ if (!added)
1295+ list_add(&new->list, &tp_vars->unacked_list);
1296+
1297+out:
1298+ spin_unlock_bh(&tp_vars->unacked_lock);
1299+
1300+ return true;
1301+}
1302+
1303+/**
1304+ * batadv_tp_ack_unordered - update number received bytes in current stream
1305+ * without gaps
1306+ * @tp_vars: the private data of the current TP meter session
1307+ */
1308+static void batadv_tp_ack_unordered(struct batadv_tp_vars *tp_vars)
1309+{
1310+ struct batadv_tp_unacked *un, *safe;
1311+ u32 to_ack;
1312+
1313+ /* go through the unacked packet list and possibly ACK them as
1314+ * well
1315+ */
1316+ spin_lock_bh(&tp_vars->unacked_lock);
1317+ list_for_each_entry_safe(un, safe, &tp_vars->unacked_list, list) {
1318+ /* the list is ordered, therefore it is possible to stop as soon
1319+ * there is a gap between the last acked seqno and the seqno of
1320+ * the packet under inspection
1321+ */
1322+ if (batadv_seq_before(tp_vars->last_recv, un->seqno))
1323+ break;
1324+
1325+ to_ack = un->seqno + un->len - tp_vars->last_recv;
1326+
1327+ if (batadv_seq_before(tp_vars->last_recv, un->seqno + un->len))
1328+ tp_vars->last_recv += to_ack;
1329+
1330+ list_del(&un->list);
1331+ kfree(un);
1332+ }
1333+ spin_unlock_bh(&tp_vars->unacked_lock);
1334+}
1335+
1336+/**
1337+ * batadv_tp_init_recv - return matching or create new receiver tp_vars
1338+ * @bat_priv: the bat priv with all the soft interface information
1339+ * @icmp: received icmp tp msg
1340+ *
1341+ * Return: corresponding tp_vars or NULL on errors
1342+ */
1343+static struct batadv_tp_vars *
1344+batadv_tp_init_recv(struct batadv_priv *bat_priv,
1345+ const struct batadv_icmp_tp_packet *icmp)
1346+{
1347+ struct batadv_tp_vars *tp_vars;
1348+
1349+ spin_lock_bh(&bat_priv->tp_list_lock);
1350+ tp_vars = batadv_tp_list_find_session(bat_priv, icmp->orig,
1351+ icmp->session);
1352+ if (tp_vars)
1353+ goto out_unlock;
1354+
1355+ if (!atomic_add_unless(&bat_priv->tp_num, 1, BATADV_TP_MAX_NUM)) {
1356+ batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
1357+ "Meter: too many ongoing sessions, aborting (RECV)\n");
1358+ goto out_unlock;
1359+ }
1360+
1361+ tp_vars = kmalloc(sizeof(*tp_vars), GFP_ATOMIC);
1362+ if (!tp_vars)
1363+ goto out_unlock;
1364+
1365+ ether_addr_copy(tp_vars->other_end, icmp->orig);
1366+ tp_vars->role = BATADV_TP_RECEIVER;
1367+ memcpy(tp_vars->session, icmp->session, sizeof(tp_vars->session));
1368+ tp_vars->last_recv = BATADV_TP_FIRST_SEQ;
1369+ tp_vars->bat_priv = bat_priv;
1370+ kref_init(&tp_vars->refcount);
1371+
1372+ spin_lock_init(&tp_vars->unacked_lock);
1373+ INIT_LIST_HEAD(&tp_vars->unacked_list);
1374+
1375+ kref_get(&tp_vars->refcount);
1376+ hlist_add_head_rcu(&tp_vars->list, &bat_priv->tp_list);
1377+
1378+ kref_get(&tp_vars->refcount);
1379+ setup_timer(&tp_vars->timer, batadv_tp_receiver_shutdown,
1380+ (unsigned long)tp_vars);
1381+
1382+ batadv_tp_reset_receiver_timer(tp_vars);
1383+
1384+out_unlock:
1385+ spin_unlock_bh(&bat_priv->tp_list_lock);
1386+
1387+ return tp_vars;
1388+}
1389+
1390+/**
1391+ * batadv_tp_recv_msg - process a single data message
1392+ * @bat_priv: the bat priv with all the soft interface information
1393+ * @skb: the buffer containing the received packet
1394+ *
1395+ * Process a received TP MSG packet
1396+ */
1397+static void batadv_tp_recv_msg(struct batadv_priv *bat_priv,
1398+ const struct sk_buff *skb)
1399+{
1400+ const struct batadv_icmp_tp_packet *icmp;
1401+ struct batadv_tp_vars *tp_vars;
1402+ size_t packet_size;
1403+ u32 seqno;
1404+
1405+ icmp = (struct batadv_icmp_tp_packet *)skb->data;
1406+
1407+ seqno = ntohl(icmp->seqno);
1408+ /* check if this is the first seqno. This means that if the
1409+ * first packet is lost, the tp meter does not work anymore!
1410+ */
1411+ if (seqno == BATADV_TP_FIRST_SEQ) {
1412+ tp_vars = batadv_tp_init_recv(bat_priv, icmp);
1413+ if (!tp_vars) {
1414+ batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
1415+ "Meter: seqno != BATADV_TP_FIRST_SEQ cannot initiate connection\n");
1416+ goto out;
1417+ }
1418+ } else {
1419+ tp_vars = batadv_tp_list_find_session(bat_priv, icmp->orig,
1420+ icmp->session);
1421+ if (!tp_vars) {
1422+ batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
1423+ "Unexpected packet from %pM!\n",
1424+ icmp->orig);
1425+ goto out;
1426+ }
1427+ }
1428+
1429+ if (unlikely(tp_vars->role != BATADV_TP_RECEIVER)) {
1430+ batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
1431+ "Meter: dropping packet: not expected (role=%u)\n",
1432+ tp_vars->role);
1433+ goto out;
1434+ }
1435+
1436+ tp_vars->last_recv_time = jiffies;
1437+
1438+ /* if the packet is a duplicate, it may be the case that an ACK has been
1439+ * lost. Resend the ACK
1440+ */
1441+ if (batadv_seq_before(seqno, tp_vars->last_recv))
1442+ goto send_ack;
1443+
1444+ /* if the packet is out of order enqueue it */
1445+ if (ntohl(icmp->seqno) != tp_vars->last_recv) {
1446+ /* exit immediately (and do not send any ACK) if the packet has
1447+ * not been enqueued correctly
1448+ */
1449+ if (!batadv_tp_handle_out_of_order(tp_vars, skb))
1450+ goto out;
1451+
1452+ /* send a duplicate ACK */
1453+ goto send_ack;
1454+ }
1455+
1456+ /* if everything was fine count the ACKed bytes */
1457+ packet_size = skb->len - sizeof(struct batadv_unicast_packet);
1458+ tp_vars->last_recv += packet_size;
1459+
1460+ /* check if this ordered message filled a gap.... */
1461+ batadv_tp_ack_unordered(tp_vars);
1462+
1463+send_ack:
1464+ /* send the ACK. If the received packet was out of order, the ACK that
1465+ * is going to be sent is a duplicate (the sender will count them and
1466+ * possibly enter Fast Retransmit as soon as it has reached 3)
1467+ */
1468+ batadv_tp_send_ack(bat_priv, icmp->orig, tp_vars->last_recv,
1469+ icmp->timestamp, icmp->session, icmp->uid);
1470+out:
1471+ if (likely(tp_vars))
1472+ batadv_tp_vars_put(tp_vars);
1473+}
1474+
1475+/**
1476+ * batadv_tp_meter_recv - main TP Meter receiving function
1477+ * @bat_priv: the bat priv with all the soft interface information
1478+ * @skb: the buffer containing the received packet
1479+ */
1480+void batadv_tp_meter_recv(struct batadv_priv *bat_priv, struct sk_buff *skb)
1481+{
1482+ struct batadv_icmp_tp_packet *icmp;
1483+
1484+ icmp = (struct batadv_icmp_tp_packet *)skb->data;
1485+
1486+ switch (icmp->subtype) {
1487+ case BATADV_TP_MSG:
1488+ batadv_tp_recv_msg(bat_priv, skb);
1489+ break;
1490+ case BATADV_TP_ACK:
1491+ batadv_tp_recv_ack(bat_priv, skb);
1492+ break;
1493+ default:
1494+ batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
1495+ "Received unknown TP Metric packet type %u\n",
1496+ icmp->subtype);
1497+ }
1498+ consume_skb(skb);
1499+}
1500+
1501+/**
1502+ * batadv_tp_meter_init - initialize global tp_meter structures
1503+ */
1504+void batadv_tp_meter_init(void)
1505+{
1506+ get_random_bytes(batadv_tp_prerandom, sizeof(batadv_tp_prerandom));
1507+}
net/batman-adv/tp_meter.h+34−0
@@ -0,0 +1,34 @@
1+/* Copyright (C) 2012-2016 B.A.T.M.A.N. contributors:
2+ *
3+ * Edo Monticelli, Antonio Quartulli
4+ *
5+ * This program is free software; you can redistribute it and/or
6+ * modify it under the terms of version 2 of the GNU General Public
7+ * License as published by the Free Software Foundation.
8+ *
9+ * This program is distributed in the hope that it will be useful, but
10+ * WITHOUT ANY WARRANTY; without even the implied warranty of
11+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+ * General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU General Public License
15+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
16+ */
17+
18+#ifndef _NET_BATMAN_ADV_TP_METER_H_
19+#define _NET_BATMAN_ADV_TP_METER_H_
20+
21+#include "main.h"
22+
23+#include <linux/types.h>
24+
25+struct sk_buff;
26+
27+void batadv_tp_meter_init(void);
28+void batadv_tp_start(struct batadv_priv *bat_priv, const u8 *dst,
29+ u32 test_length, u32 *cookie);
30+void batadv_tp_stop(struct batadv_priv *bat_priv, const u8 *dst,
31+ u8 return_value);
32+void batadv_tp_meter_recv(struct batadv_priv *bat_priv, struct sk_buff *skb);
33+
34+#endif /* _NET_BATMAN_ADV_TP_METER_H_ */
net/batman-adv/types.h+112−0
@@ -33,6 +33,7 @@ · 32 unmodified lines
3333 #include <linux/types.h>
3434 #include <linux/wait.h>
3535 #include <linux/workqueue.h>
36+#include <uapi/linux/batman_adv.h>
3637
3738 #include "packet.h"
3839
@@ -832,6 +833,111 @@ struct batadv_priv_nc { · 793 unmodified lines
832833 struct batadv_hashtable *decoding_hash;
833834 };
834835
836+/**
837+ * struct batadv_tp_unacked - unacked packet meta-information
838+ * @seqno: seqno of the unacked packet
839+ * @len: length of the packet
840+ * @list: list node for batadv_tp_vars::unacked_list
841+ *
842+ * This struct is supposed to represent a buffer unacked packet. However, since
843+ * the purpose of the TP meter is to count the traffic only, there is no need to
844+ * store the entire sk_buff, the starting offset and the length are enough
845+ */
846+struct batadv_tp_unacked {
847+ u32 seqno;
848+ u16 len;
849+ struct list_head list;
850+};
851+
852+/**
853+ * enum batadv_tp_meter_role - Modus in tp meter session
854+ * @BATADV_TP_RECEIVER: Initialized as receiver
855+ * @BATADV_TP_SENDER: Initialized as sender
856+ */
857+enum batadv_tp_meter_role {
858+ BATADV_TP_RECEIVER,
859+ BATADV_TP_SENDER
860+};
861+
862+/**
863+ * struct batadv_tp_vars - tp meter private variables per session
864+ * @list: list node for bat_priv::tp_list
865+ * @timer: timer for ack (receiver) and retry (sender)
866+ * @bat_priv: pointer to the mesh object
867+ * @start_time: start time in jiffies
868+ * @other_end: mac address of remote
869+ * @role: receiver/sender modi
870+ * @sending: sending binary semaphore: 1 if sending, 0 is not
871+ * @reason: reason for a stopped session
872+ * @finish_work: work item for the finishing procedure
873+ * @test_length: test length in milliseconds
874+ * @session: TP session identifier
875+ * @icmp_uid: local ICMP "socket" index
876+ * @dec_cwnd: decimal part of the cwnd used during linear growth
877+ * @cwnd: current size of the congestion window
878+ * @cwnd_lock: lock do protect @cwnd & @dec_cwnd
879+ * @ss_threshold: Slow Start threshold. Once cwnd exceeds this value the
880+ * connection switches to the Congestion Avoidance state
881+ * @last_acked: last acked byte
882+ * @last_sent: last sent byte, not yet acked
883+ * @tot_sent: amount of data sent/ACKed so far
884+ * @dup_acks: duplicate ACKs counter
885+ * @fast_recovery: true if in Fast Recovery mode
886+ * @recover: last sent seqno when entering Fast Recovery
887+ * @rto: sender timeout
888+ * @srtt: smoothed RTT scaled by 2^3
889+ * @rttvar: RTT variation scaled by 2^2
890+ * @more_bytes: waiting queue anchor when waiting for more ack/retry timeout
891+ * @prerandom_offset: offset inside the prerandom buffer
892+ * @prerandom_lock: spinlock protecting access to prerandom_offset
893+ * @last_recv: last in-order received packet
894+ * @unacked_list: list of unacked packets (meta-info only)
895+ * @unacked_lock: protect unacked_list
896+ * @last_recv_time: time time (jiffies) a msg was received
897+ * @refcount: number of context where the object is used
898+ * @rcu: struct used for freeing in an RCU-safe manner
899+ */
900+struct batadv_tp_vars {
901+ struct hlist_node list;
902+ struct timer_list timer;
903+ struct batadv_priv *bat_priv;
904+ unsigned long start_time;
905+ u8 other_end[ETH_ALEN];
906+ enum batadv_tp_meter_role role;
907+ atomic_t sending;
908+ enum batadv_tp_meter_reason reason;
909+ struct delayed_work finish_work;
910+ u32 test_length;
911+ u8 session[2];
912+ u8 icmp_uid;
913+
914+ /* sender variables */
915+ u16 dec_cwnd;
916+ u32 cwnd;
917+ spinlock_t cwnd_lock; /* Protects cwnd & dec_cwnd */
918+ u32 ss_threshold;
919+ atomic_t last_acked;
920+ u32 last_sent;
921+ atomic64_t tot_sent;
922+ atomic_t dup_acks;
923+ bool fast_recovery;
924+ u32 recover;
925+ u32 rto;
926+ u32 srtt;
927+ u32 rttvar;
928+ wait_queue_head_t more_bytes;
929+ u32 prerandom_offset;
930+ spinlock_t prerandom_lock; /* Protects prerandom_offset */
931+
932+ /* receiver variables */
933+ u32 last_recv;
934+ struct list_head unacked_list;
935+ spinlock_t unacked_lock; /* Protects unacked_list */
936+ unsigned long last_recv_time;
937+ struct kref refcount;
938+ struct rcu_head rcu;
939+};
940+
835941 /**
836942 * struct batadv_softif_vlan - per VLAN attributes set
837943 * @bat_priv: pointer to the mesh object
@@ -900,9 +1006,12 @@ struct batadv_priv_bat_v { · 62 unmodified lines
9001006 * @debug_dir: dentry for debugfs batman-adv subdirectory
9011007 * @forw_bat_list: list of aggregated OGMs that will be forwarded
9021008 * @forw_bcast_list: list of broadcast packets that will be rebroadcasted
1009+ * @tp_list: list of tp sessions
1010+ * @tp_num: number of currently active tp sessions
9031011 * @orig_hash: hash table containing mesh participants (orig nodes)
9041012 * @forw_bat_list_lock: lock protecting forw_bat_list
9051013 * @forw_bcast_list_lock: lock protecting forw_bcast_list
1014+ * @tp_list_lock: spinlock protecting @tp_list
9061015 * @orig_work: work queue callback item for orig node purging
9071016 * @cleanup_work: work queue callback item for soft-interface deinit
9081017 * @primary_if: one of the hard-interfaces assigned to this mesh interface
@@ -956,9 +1065,12 @@ struct batadv_priv { · 47 unmodified lines
9561065 struct dentry *debug_dir;
9571066 struct hlist_head forw_bat_list;
9581067 struct hlist_head forw_bcast_list;
1068+ struct hlist_head tp_list;
9591069 struct batadv_hashtable *orig_hash;
9601070 spinlock_t forw_bat_list_lock; /* protects forw_bat_list */
9611071 spinlock_t forw_bcast_list_lock; /* protects forw_bcast_list */
1072+ spinlock_t tp_list_lock; /* protects tp_list */
1073+ atomic_t tp_num;
9621074 struct delayed_work orig_work;
9631075 struct work_struct cleanup_work;
9641076 struct batadv_hard_iface __rcu *primary_if; /* rcu protected pointer */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t