2 * Copyright (c) 2009-2010
3 * Swinburne University of Technology, Melbourne, Australia
4 * Copyright (c) 2010 Lawrence Stewart <lstewart@freebsd.org>
5 * Copyright (c) 2010-2011 The FreeBSD Foundation
8 * This software was developed at the Centre for Advanced Internet
9 * Architectures, Swinburne University of Technology, by David Hayes, made
10 * possible in part by a grant from the Cisco University Research Program Fund
11 * at Community Foundation Silicon Valley.
13 * Portions of this software were developed at the Centre for Advanced
14 * Internet Architectures, Swinburne University of Technology, Melbourne,
15 * Australia by David Hayes under sponsorship from the FreeBSD Foundation.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/kernel.h>
45 #include <sys/module.h>
46 #include <sys/hhook.h>
47 #include <sys/khelp.h>
48 #include <sys/module_khelp.h>
49 #include <sys/socket.h>
50 #include <sys/sockopt.h>
54 #include <netinet/in.h>
55 #include <netinet/in_pcb.h>
56 #include <netinet/tcp_seq.h>
57 #include <netinet/tcp_var.h>
59 #include <netinet/khelp/h_ertt.h>
63 uma_zone_t txseginfo_zone;
65 /* Smoothing factor for delayed ack guess. */
66 #define DLYACK_SMOOTH 5
68 /* Max number of time stamp errors allowed in a session. */
71 static int ertt_packet_measurement_hook(int hhook_type, int hhook_id,
72 void *udata, void *ctx_data, void *hdata, struct osd *hosd);
73 static int ertt_add_tx_segment_info_hook(int hhook_type, int hhook_id,
74 void *udata, void *ctx_data, void *hdata, struct osd *hosd);
75 static int ertt_mod_init(void);
76 static int ertt_mod_destroy(void);
77 static int ertt_uma_ctor(void *mem, int size, void *arg, int flags);
78 static void ertt_uma_dtor(void *mem, int size, void *arg);
81 * Contains information about the sent segment for comparison with the
87 /* Segment sequence number. */
89 /* Time stamp indicating when the packet was sent. */
91 /* Last received receiver ts (if the TCP option is used). */
94 TAILQ_ENTRY (txseginfo) txsegi_lnk;
97 /* Flags for struct txseginfo. */
98 #define TXSI_TSO 0x01 /* TSO was used for this entry. */
99 #define TXSI_RTT_MEASURE_START 0x02 /* Start a per RTT measurement. */
100 #define TXSI_RX_MEASURE_END 0x04 /* Measure the rx rate until this txsi. */
102 struct helper ertt_helper = {
103 .mod_init = ertt_mod_init,
104 .mod_destroy = ertt_mod_destroy,
105 .h_flags = HELPER_NEEDS_OSD,
106 .h_classes = HELPER_CLASS_TCP
109 /* Define the helper hook info required by ERTT. */
110 struct hookinfo ertt_hooks[] = {
112 .hook_type = HHOOK_TYPE_TCP,
113 .hook_id = HHOOK_TCP_EST_IN,
115 .hook_func = &ertt_packet_measurement_hook
118 .hook_type = HHOOK_TYPE_TCP,
119 .hook_id = HHOOK_TCP_EST_OUT,
121 .hook_func = &ertt_add_tx_segment_info_hook
125 /* Flags to indicate how marked_packet_rtt should handle this txsi. */
126 #define MULTI_ACK 0x01 /* More than this txsi is acked. */
127 #define OLD_TXSI 0x02 /* TXSI is old according to timestamps. */
128 #define CORRECT_ACK 0X04 /* Acks this TXSI. */
129 #define FORCED_MEASUREMENT 0X08 /* Force an RTT measurement. */
132 * This fuction measures the RTT of a particular segment/ack pair, or the next
133 * closest if this will yield an inaccurate result due to delayed acking or
137 marked_packet_rtt(struct txseginfo *txsi, struct ertt *e_t, struct tcpcb *tp,
138 uint32_t *pmeasurenext, int *pmeasurenext_len, int *prtt_bytes_adjust,
143 * If we can't measure this one properly due to delayed acking adjust
144 * byte counters and flag to measure next txsi. Note that since the
145 * marked packet's transmitted bytes are measured we need to subtract the
146 * transmitted bytes. Then pretend the next txsi was marked.
148 if (mflag & (MULTI_ACK|OLD_TXSI)) {
149 *pmeasurenext = txsi->tx_ts;
150 *pmeasurenext_len = txsi->len;
151 *prtt_bytes_adjust += *pmeasurenext_len;
153 if (mflag & FORCED_MEASUREMENT) {
154 e_t->markedpkt_rtt = ticks - *pmeasurenext + 1;
155 e_t->bytes_tx_in_marked_rtt = e_t->bytes_tx_in_rtt +
156 *pmeasurenext_len - *prtt_bytes_adjust;
158 e_t->markedpkt_rtt = ticks - txsi->tx_ts + 1;
159 e_t->bytes_tx_in_marked_rtt = e_t->bytes_tx_in_rtt -
162 e_t->marked_snd_cwnd = tp->snd_cwnd;
165 * Reset the ERTT_MEASUREMENT_IN_PROGRESS flag to indicate to
166 * add_tx_segment_info that a new measurement should be started.
168 e_t->flags &= ~ERTT_MEASUREMENT_IN_PROGRESS;
170 * Set ERTT_NEW_MEASUREMENT to tell the congestion control
171 * algorithm that a new marked RTT measurement has has been made
172 * and is available for use.
174 e_t->flags |= ERTT_NEW_MEASUREMENT;
176 if (tp->t_flags & TF_TSO) {
177 /* Temporarily disable TSO to aid a new measurment. */
178 tp->t_flags &= ~TF_TSO;
179 /* Keep track that we've disabled it. */
180 e_t->flags |= ERTT_TSO_DISABLED;
186 * Ertt_packet_measurements uses a small amount of state kept on each packet
187 * sent to match incoming acknowledgements. This enables more accurate and
188 * secure round trip time measurements. The resulting measurement is used for
189 * congestion control algorithms which require a more accurate time.
190 * Ertt_packet_measurements is called via the helper hook in tcp_input.c
193 ertt_packet_measurement_hook(int hhook_type, int hhook_id, void *udata,
194 void *ctx_data, void *hdata, struct osd *hosd)
200 struct tcp_hhook_data *thdp;
201 struct txseginfo *txsi;
202 int acked, measurenext_len, multiack, new_sacked_bytes, rtt_bytes_adjust;
203 uint32_t measurenext, rts;
206 KASSERT(ctx_data != NULL, ("%s: ctx_data is NULL!", __func__));
207 KASSERT(hdata != NULL, ("%s: hdata is NULL!", __func__));
209 e_t = (struct ertt *)hdata;
214 new_sacked_bytes = (tp->sackhint.last_sack_ack != 0);
215 measurenext = measurenext_len = multiack = rts = rtt_bytes_adjust = 0;
216 acked = th->th_ack - tp->snd_una;
218 INP_WLOCK_ASSERT(tp->t_inpcb);
220 /* Packet has provided new acknowledgements. */
221 if (acked > 0 || new_sacked_bytes) {
222 if (acked == 0 && new_sacked_bytes) {
223 /* Use last sacked data. */
224 ack = tp->sackhint.last_sack_ack;
228 txsi = TAILQ_FIRST(&e_t->txsegi_q);
229 while (txsi != NULL) {
232 /* Acknowledgement is acking more than this txsi. */
233 if (SEQ_GT(ack, txsi->seq + txsi->len)) {
234 if (txsi->flags & TXSI_RTT_MEASURE_START ||
236 marked_packet_rtt(txsi, e_t, tp,
237 &measurenext, &measurenext_len,
238 &rtt_bytes_adjust, MULTI_ACK);
240 TAILQ_REMOVE(&e_t->txsegi_q, txsi, txsegi_lnk);
241 uma_zfree(txseginfo_zone, txsi);
242 txsi = TAILQ_FIRST(&e_t->txsegi_q);
247 * Guess if delayed acks are being used by the receiver.
249 * XXXDH: A simple heuristic that could be improved
251 if (!new_sacked_bytes) {
252 if (acked > tp->t_maxseg) {
254 (e_t->dlyack_rx < DLYACK_SMOOTH) ?
257 } else if (acked > txsi->len) {
260 (e_t->dlyack_rx < DLYACK_SMOOTH) ?
262 } else if (acked == tp->t_maxseg ||
263 acked == txsi->len) {
265 (e_t->dlyack_rx > 0) ? 1 : 0;
267 /* Otherwise leave dlyack_rx the way it was. */
271 * Time stamps are only to help match the txsi with the
272 * received acknowledgements.
274 if (e_t->timestamp_errors < MAX_TS_ERR &&
275 (to->to_flags & TOF_TS) != 0 && to->to_tsecr) {
277 * Note: All packets sent with the offload will
278 * have the same time stamp. If we are sending
279 * on a fast interface and the t_maxseg is much
280 * smaller than one tick, this will be fine. The
281 * time stamp would be the same whether we were
282 * using tso or not. However, if the interface
283 * is slow, this will cause problems with the
284 * calculations. If the interface is slow, there
285 * is not reason to be using tso, and it should
289 * If there are too many time stamp errors, time
290 * stamps won't be trusted
293 /* Before this packet. */
294 if (!e_t->dlyack_rx && TSTMP_LT(rts, txsi->tx_ts))
295 /* When delayed acking is used, the
296 * reflected time stamp is of the first
297 * packet and thus may be before
301 if (TSTMP_GT(rts, txsi->tx_ts)) {
303 * If reflected time stamp is later than
304 * tx_tsi, then this txsi is old.
306 if (txsi->flags & TXSI_RTT_MEASURE_START
308 marked_packet_rtt(txsi, e_t, tp,
309 &measurenext, &measurenext_len,
310 &rtt_bytes_adjust, OLD_TXSI);
312 TAILQ_REMOVE(&e_t->txsegi_q, txsi,
314 uma_zfree(txseginfo_zone, txsi);
315 txsi = TAILQ_FIRST(&e_t->txsegi_q);
318 if (rts == txsi->tx_ts &&
319 TSTMP_LT(to->to_tsval, txsi->rx_ts)) {
321 * Segment received before sent!
322 * Something is wrong with the received
323 * timestamps so increment errors. If
324 * this keeps up we will ignore
327 e_t->timestamp_errors++;
331 * Acknowledging a sequence number before this txsi.
332 * If it is an old txsi that may have had the same seq
333 * numbers, it should have been removed if time stamps
336 if (SEQ_LEQ(ack, txsi->seq))
337 break; /* Before first packet in txsi. */
340 * Only ack > txsi->seq and ack <= txsi->seq+txsi->len
343 * If delayed acks are being used, an acknowledgement
344 * for a single segment will have been delayed by the
345 * receiver and will yield an inaccurate measurement. In
346 * this case, we only make the measurement if more than
347 * one segment is being acknowledged or sack is
348 * currently being used.
350 if (!e_t->dlyack_rx || multiack || new_sacked_bytes) {
351 /* Make an accurate new measurement. */
352 e_t->rtt = ticks - txsi->tx_ts + 1;
354 if (e_t->rtt < e_t->minrtt || e_t->minrtt == 0)
355 e_t->minrtt = e_t->rtt;
357 if (e_t->rtt > e_t->maxrtt || e_t->maxrtt == 0)
358 e_t->maxrtt = e_t->rtt;
361 if (txsi->flags & TXSI_RTT_MEASURE_START || measurenext)
362 marked_packet_rtt(txsi, e_t, tp,
363 &measurenext, &measurenext_len,
364 &rtt_bytes_adjust, CORRECT_ACK);
366 if (txsi->flags & TXSI_TSO) {
370 * This presumes ack for first bytes in
371 * txsi, this may not be true but it
372 * shouldn't cause problems for the
375 * We remeasure RTT even though we only
376 * have a single txsi. The rationale
377 * behind this is that it is better to
378 * have a slightly inaccurate
379 * measurement than no additional
380 * measurement for the rest of the bulk
381 * transfer. Since TSO is only used on
382 * high speed interface cards, so the
383 * packets should be transmitted at line
384 * rate back to back with little
385 * difference in transmission times (in
390 * Reset txsi measure flag so we don't
391 * use it for another RTT measurement.
393 txsi->flags &= ~TXSI_RTT_MEASURE_START;
395 * There is still more data to be acked
396 * from tso bulk transmission, so we
397 * won't remove it from the TAILQ yet.
403 TAILQ_REMOVE(&e_t->txsegi_q, txsi, txsegi_lnk);
404 uma_zfree(txseginfo_zone, txsi);
410 * We need to do a RTT measurement. It won't be the best
413 marked_packet_rtt(txsi, e_t, tp,
414 &measurenext, &measurenext_len,
415 &rtt_bytes_adjust, FORCED_MEASUREMENT);
423 * Add information about a transmitted segment to a list.
424 * This is called via the helper hook in tcp_output.c
427 ertt_add_tx_segment_info_hook(int hhook_type, int hhook_id, void *udata,
428 void *ctx_data, void *hdata, struct osd *hosd)
434 struct tcp_hhook_data *thdp;
435 struct txseginfo *txsi;
439 KASSERT(ctx_data != NULL, ("%s: ctx_data is NULL!", __func__));
440 KASSERT(hdata != NULL, ("%s: hdata is NULL!", __func__));
442 e_t = (struct ertt *)hdata;
450 INP_WLOCK_ASSERT(tp->t_inpcb);
453 txsi = uma_zalloc(txseginfo_zone, M_NOWAIT);
455 /* Construct txsi setting the necessary flags. */
456 txsi->flags = 0; /* Needs to be initialised. */
457 txsi->seq = ntohl(th->th_seq);
460 txsi->flags |= TXSI_TSO;
461 else if (e_t->flags & ERTT_TSO_DISABLED) {
462 tp->t_flags |= TF_TSO;
463 e_t->flags &= ~ERTT_TSO_DISABLED;
466 if (e_t->flags & ERTT_MEASUREMENT_IN_PROGRESS) {
467 e_t->bytes_tx_in_rtt += len;
469 txsi->flags |= TXSI_RTT_MEASURE_START;
470 e_t->flags |= ERTT_MEASUREMENT_IN_PROGRESS;
471 e_t->bytes_tx_in_rtt = len;
474 if (((tp->t_flags & TF_NOOPT) == 0) &&
475 (to->to_flags & TOF_TS)) {
476 txsi->tx_ts = ntohl(to->to_tsval) -
478 txsi->rx_ts = ntohl(to->to_tsecr);
481 txsi->rx_ts = 0; /* No received time stamp. */
483 TAILQ_INSERT_TAIL(&e_t->txsegi_q, txsi, txsegi_lnk);
494 txseginfo_zone = uma_zcreate("ertt_txseginfo", sizeof(struct txseginfo),
495 NULL, NULL, NULL, NULL, 0, 0);
501 ertt_mod_destroy(void)
504 uma_zdestroy(txseginfo_zone);
510 ertt_uma_ctor(void *mem, int size, void *arg, int flags)
516 TAILQ_INIT(&e_t->txsegi_q);
517 e_t->timestamp_errors = 0;
523 e_t->bytes_tx_in_rtt = 0;
524 e_t->markedpkt_rtt = 0;
530 ertt_uma_dtor(void *mem, int size, void *arg)
533 struct txseginfo *n_txsi, *txsi;
536 txsi = TAILQ_FIRST(&e_t->txsegi_q);
537 while (txsi != NULL) {
538 n_txsi = TAILQ_NEXT(txsi, txsegi_lnk);
539 uma_zfree(txseginfo_zone, txsi);
544 KHELP_DECLARE_MOD_UMA(ertt, &ertt_helper, ertt_hooks, 1, sizeof(struct ertt),
545 ertt_uma_ctor, ertt_uma_dtor);