Merge pull request #137 from mrdeep1/session-retries

Require support for configuring session recovery parameters
This commit is contained in:
obgm authored and GitHub committed 2018-03-06 16:43:51 +01:00
commit bf43c3af6c
6 files changed
+214 -57

No files matched your search

+122
View File
@@ -18,6 +18,16 @@ struct coap_endpoint_t;
struct coap_contex_t;
struct coap_queue_t;
/**
* Abstraction of a fixed point number that can be used where necessary instead
* of a float. 1,000 fractional bits equals one integer
*/
typedef struct coap_fixed_point_t {
uint16_t integer_part; /**< Integer part of fixed point variable */
uint16_t fractional_part; /**< Fractional part of fixed point variable
1/1000 (3 points) precision */
} coap_fixed_point_t;
#define COAP_DEFAULT_SESSION_TIMEOUT 300
#define COAP_PROTO_NOT_RELIABLE(p) ((p)==COAP_PROTO_UDP || (p)==COAP_PROTO_DTLS)
@@ -69,6 +79,9 @@ typedef struct coap_session_t {
uint8_t *psk_key;
size_t psk_key_len;
void *app; /**< application-specific data */
unsigned int max_retransmit; /**< maximum re-transmit count (default 4) */
coap_fixed_point_t ack_timeout; /**< timeout waiting for ack (default 2 secs) */
coap_fixed_point_t ack_random_factor; /**< ack random factor backoff (default 1.5) */
} coap_session_t;
/**
@@ -342,5 +355,114 @@ coap_session_t *coap_session_get_by_peer(struct coap_context_t *ctx,
void coap_session_free(coap_session_t *session);
/**
* @defgroup cc Rate Control
* The transmission parameters for CoAP rate control ("Congestion
* Control" in stream-oriented protocols) are defined in
* https://tools.ietf.org/html/rfc7252#section-4.8
* @{
*/
/**
* Number of seconds when to expect an ACK or a response to an
* outstanding CON message.
* RFC 7252, Section 4.8 Default value of ACK_TIMEOUT is 2
*/
#define COAP_DEFAULT_ACK_TIMEOUT ((coap_fixed_point_t){2,0})
/**
* A factor that is used to randomize the wait time before a message
* is retransmitted to prevent synchronization effects.
* RFC 7252, Section 4.8 Default value of ACK_RANDOM_FACTOR is 1.5
*/
#define COAP_DEFAULT_ACK_RANDOM_FACTOR ((coap_fixed_point_t){1,500})
/**
* Number of message retransmissions before message sending is stopped
* RFC 7252, Section 4.8 Default value of MAX_RETRANSMIT is 4
*/
#define COAP_DEFAULT_MAX_RETRANSMIT 4
/**
* The number of simultaneous outstanding interactions that a client
* maintains to a given server.
* RFC 7252, Section 4.8 Default value of NSTART is 1
*/
#define COAP_DEFAULT_NSTART 1
/** @} */
/**
* Set the CoAP maximum retransmit count before failure
*
* Number of message retransmissions before message sending is stopped
*
* @param session The CoAP session.
* @param value The value to set to. The default is 4 and should not normally
* get changed.
*/
void coap_session_set_max_retransmit(coap_session_t *session,
unsigned int value);
/**
* Set the CoAP initial ack response timeout before the next re-transmit
*
* Number of seconds when to expect an ACK or a response to an
* outstanding CON message.
*
* @param session The CoAP session.
* @param value The value to set to. The default is 2 and should not normally
* get changed.
*/
void coap_session_set_ack_timeout(coap_session_t *session,
coap_fixed_point_t value);
/**
* Set the CoAP ack randomize factor
*
* A factor that is used to randomize the wait time before a message
* is retransmitted to prevent synchronization effects.
*
* @param session The CoAP session.
* @param value The value to set to. The default is 1.5 and should not normally
* get changed.
*/
void coap_session_set_ack_random_factor(coap_session_t *session,
coap_fixed_point_t value);
/**
* Get the CoAP maximum retransmit before failure
*
* Number of message retransmissions before message sending is stopped
*
* @param session The CoAP session.
*
* @return Current maximum retransmit value
*/
unsigned int coap_session_get_max_transmit(coap_session_t *session);
/**
* Get the CoAP initial ack response timeout before the next re-transmit
*
* Number of seconds when to expect an ACK or a response to an
* outstanding CON message.
*
* @param session The CoAP session.
*
* @return Current ack response timeout value
*/
coap_fixed_point_t coap_session_get_ack_timeout(coap_session_t *session);
/**
* Get the CoAP ack randomize factor
*
* A factor that is used to randomize the wait time before a message
* is retransmitted to prevent synchronization effects.
*
* @param session The CoAP session.
*
* @return Current ack randomize value
*/
coap_fixed_point_t coap_session_get_ack_random_factor(coap_session_t *session);
#endif /* _SESSION_H */
+14 -1
View File
@@ -619,6 +619,19 @@ coap_pdu_t *coap_wellknown_response(coap_context_t *context,
coap_session_t *session,
coap_pdu_t *request);
unsigned int calc_timeout(unsigned char r);
/**
* Calculates the initial timeout based on the session CoAP transmission
* parameters 'ack_timeout', 'ack_random_factor', and COAP_TICKS_PER_SECOND.
* The calculation requires 'ack_timeout' and 'ack_random_factor' to be in
* Qx.FRAC_BITS fixed point notation, whereas the passed parameter @p r
* is interpreted as the fractional part of a Q0.MAX_BITS random value.
*
* @param session session timeout is associated with
* @param r random value as fractional part of a Q0.MAX_BITS fixed point
* value
* @return COAP_TICKS_PER_SECOND * 'ack_timeout' *
* (1 + ('ack_random_factor' - 1) * r)
*/
unsigned int coap_calc_timeout(coap_session_t *session, unsigned char r);
#endif /* _COAP_NET_H_ */
+6
View File
@@ -154,8 +154,11 @@ global:
coap_session_delay_pdu;
coap_session_disconnected;
coap_session_free;
coap_session_get_ack_random_factor;
coap_session_get_ack_timeout;
coap_session_get_app_data;
coap_session_get_by_peer;
coap_session_get_max_transmit;
coap_session_max_pdu_size;
coap_session_reference;
coap_session_release;
@@ -163,7 +166,10 @@ global:
coap_session_send;
coap_session_send_csm;
coap_session_send_pdu;
coap_session_set_ack_random_factor;
coap_session_set_ack_timeout;
coap_session_set_app_data;
coap_session_set_max_retransmit;
coap_session_set_mtu;
coap_session_str;
coap_session_write;
+6
View File
@@ -152,8 +152,11 @@ coap_session_connected
coap_session_delay_pdu
coap_session_disconnected
coap_session_free
coap_session_get_ack_random_factor
coap_session_get_ack_timeout
coap_session_get_app_data
coap_session_get_by_peer
coap_session_get_max_transmit
coap_session_max_pdu_size
coap_session_reference
coap_session_release
@@ -161,7 +164,10 @@ coap_session_reset
coap_session_send
coap_session_send_csm
coap_session_send_pdu
coap_session_set_ack_random_factor
coap_session_set_ack_timeout
coap_session_set_app_data
coap_session_set_max_retransmit
coap_session_set_mtu
coap_session_str
coap_session_write
+49 -1
View File
@@ -21,6 +21,51 @@
#include "encode.h"
#include <stdio.h>
void
coap_session_set_max_retransmit (coap_session_t *session, unsigned int value) {
if (value > 0)
session->max_retransmit = value;
coap_log(LOG_DEBUG, "*** %s: session max_retransmit set to %d\n",
coap_session_str(session), session->max_retransmit);
return;
}
void
coap_session_set_ack_timeout (coap_session_t *session, coap_fixed_point_t value) {
if (value.integer_part > 0 && value.fractional_part < 1000)
session->ack_timeout = value;
coap_log(LOG_DEBUG, "*** %s: session ack_timeout set to %d.%03d\n",
coap_session_str(session), session->ack_timeout.integer_part,
session->ack_timeout.fractional_part);
return;
}
void
coap_session_set_ack_random_factor (coap_session_t *session,
coap_fixed_point_t value) {
if (value.integer_part > 0 && value.fractional_part < 1000)
session->ack_random_factor = value;
coap_log(LOG_DEBUG, "*** %s: session ack_random_factor set to %d.%03d\n",
coap_session_str(session), session->ack_random_factor.integer_part,
session->ack_random_factor.fractional_part);
return;
}
unsigned int
coap_session_get_max_transmit (coap_session_t *session) {
return session->max_retransmit;
}
coap_fixed_point_t
coap_session_get_ack_timeout (coap_session_t *session) {
return session->ack_timeout;
}
coap_fixed_point_t
coap_session_get_ack_random_factor (coap_session_t *session) {
return session->ack_random_factor;
}
coap_session_t *
coap_session_reference(coap_session_t *session) {
++session->ref;
@@ -87,6 +132,9 @@ coap_make_session(coap_proto_t proto, coap_session_type_t type,
coap_log(LOG_ERR, "DTLS overhead exceeds MTU\n");
}
}
session->max_retransmit = COAP_DEFAULT_MAX_RETRANSMIT;
session->ack_timeout = COAP_DEFAULT_ACK_TIMEOUT;
session->ack_random_factor = COAP_DEFAULT_ACK_RANDOM_FACTOR;
/* initialize message id */
prng((unsigned char *)&session->tx_mid, sizeof(session->tx_mid));
@@ -213,7 +261,7 @@ coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu,
uint8_t r;
prng(&r, sizeof(r));
/* add timeout in range [ACK_TIMEOUT...ACK_TIMEOUT * ACK_RANDOM_FACTOR] */
node->timeout = calc_timeout(r);
node->timeout = coap_calc_timeout(session, r);
}
}
LL_APPEND(session->sendqueue, node);
+17 -55
View File
@@ -58,47 +58,6 @@
#define min(a,b) ((a) < (b) ? (a) : (b))
#endif
/**
* @defgroup cc Rate Control
* The transmission parameters for CoAP rate control ("Congestion
* Control" in stream-oriented protocols) are defined in
* https://tools.ietf.org/html/rfc7252#section-4.8
* @{
*/
#ifndef COAP_DEFAULT_ACK_TIMEOUT
/**
* Number of seconds when to expect an ACK or a response to an
* outstanding CON message.
*/
#define COAP_DEFAULT_ACK_TIMEOUT 2 /* see RFC 7252, Section 4.8 */
#endif
#ifndef COAP_DEFAULT_ACK_RANDOM_FACTOR
/**
* A factor that is used to randomize the wait time before a message
* is retransmitted to prevent synchronization effects.
*/
#define COAP_DEFAULT_ACK_RANDOM_FACTOR 1.5 /* see RFC 7252, Section 4.8 */
#endif
#ifndef COAP_DEFAULT_MAX_RETRANSMIT
/**
* Number of message retransmissions before message sending is stopped
*/
#define COAP_DEFAULT_MAX_RETRANSMIT 4 /* see RFC 7252, Section 4.8 */
#endif
#ifndef COAP_DEFAULT_NSTART
/**
* The number of simultaneous outstanding interactions that a client
* maintains to a given server.
*/
#define COAP_DEFAULT_NSTART 1 /* see RFC 7252, Section 4.8 */
#endif
/** @} */
/**
* The number of bits for the fractional part of ACK_TIMEOUT and
* ACK_RANDOM_FACTOR. Must be less or equal 8.
@@ -115,15 +74,16 @@
#error FRAC_BITS must be less or equal 8
#endif
/** creates a Qx.frac from fval */
#define Q(frac,fval) ((uint16_t)(((1 << (frac)) * (fval))))
/** creates a Qx.frac from fval in coap_fixed_point_t */
#define Q(frac,fval) ((uint16_t)(((1 << (frac)) * fval.integer_part) + \
((1 << (frac)) * fval.fractional_part + 500)/1000))
/** creates a Qx.FRAC_BITS from COAP_DEFAULT_ACK_RANDOM_FACTOR */
/** creates a Qx.FRAC_BITS from session's 'ack_random_factor' */
#define ACK_RANDOM_FACTOR \
Q(FRAC_BITS, COAP_DEFAULT_ACK_RANDOM_FACTOR)
Q(FRAC_BITS, session->ack_random_factor)
/** creates a Qx.FRAC_BITS from COAP_DEFAULT_ACK_TIMEOUT */
#define ACK_TIMEOUT Q(FRAC_BITS, COAP_DEFAULT_ACK_TIMEOUT)
/** creates a Qx.FRAC_BITS from session's 'ack_timeout' */
#define ACK_TIMEOUT Q(FRAC_BITS, session->ack_timeout)
#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
@@ -782,22 +742,24 @@ coap_send_message_type(coap_session_t *session, coap_pdu_t *request, unsigned ch
}
/**
* Calculates the initial timeout based on the global CoAP transmission
* parameters ACK_TIMEOUT, ACK_RANDOM_FACTOR, and COAP_TICKS_PER_SECOND.
* The calculation requires ACK_TIMEOUT and ACK_RANDOM_FACTOR to be in
* Calculates the initial timeout based on the session CoAP transmission
* parameters 'ack_timeout', 'ack_random_factor', and COAP_TICKS_PER_SECOND.
* The calculation requires 'ack_timeout' and 'ack_random_factor' to be in
* Qx.FRAC_BITS fixed point notation, whereas the passed parameter @p r
* is interpreted as the fractional part of a Q0.MAX_BITS random value.
*
* @param session session timeout is associated with
* @param r random value as fractional part of a Q0.MAX_BITS fixed point
* value
* @return COAP_TICKS_PER_SECOND * ACK_TIMEOUT * (1 + (ACK_RANDOM_FACTOR - 1) * r)
* @return COAP_TICKS_PER_SECOND * 'ack_timeout' *
* (1 + ('ack_random_factor' - 1) * r)
*/
unsigned int
calc_timeout(unsigned char r) {
coap_calc_timeout(coap_session_t *session, unsigned char r) {
unsigned int result;
/* The integer 1.0 as a Qx.FRAC_BITS */
#define FP1 Q(FRAC_BITS, 1)
#define FP1 Q(FRAC_BITS, ((coap_fixed_point_t){1,0}))
/* rounds val up and right shifts by frac positions */
#define SHR_FP(val,frac) (((val) + (1 << ((frac) - 1))) >> (frac))
@@ -918,7 +880,7 @@ coap_send(coap_session_t *session, coap_pdu_t *pdu) {
node->pdu = pdu;
prng(&r, sizeof(r));
/* add timeout in range [ACK_TIMEOUT...ACK_TIMEOUT * ACK_RANDOM_FACTOR] */
node->timeout = calc_timeout(r);
node->timeout = coap_calc_timeout(session, r);
return coap_wait_ack(session->context, session, node);
error:
coap_delete_pdu(pdu);
@@ -931,7 +893,7 @@ coap_retransmit(coap_context_t *context, coap_queue_t *node) {
return COAP_INVALID_TID;
/* re-initialize timeout when maximum number of retransmissions are not reached yet */
if (node->retransmit_cnt < COAP_DEFAULT_MAX_RETRANSMIT) {
if (node->retransmit_cnt < node->session->max_retransmit) {
ssize_t bytes_written;
coap_tick_t now;