From b105a7dea2e971ffcbcc94cedb610734f2e6c186 Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Sun, 11 Feb 2018 15:51:33 +0000 Subject: [PATCH 1/5] coap_session.h: Add definitions/functions to configure session retransmissions Support is required (RFC7252: 4.8.1. Changing the Parameters) to support changing max_retransmit, ack_timeout and ack_random_factor values per session instead of just using the default values. Define struct coap_fixed_point_t to handle fixed point parameters Added functions coap_session_set_max_retransmit() coap_session_set_ack_timeout() coap_session_set_ack_random_factor() coap_session_get_max_transmit() coap_session_get_ack_timeout() coap_session_get_ack_random_factor() 'cc Rate Control' moved from net.c into coap_session.h and updated to use coap_fixed_point_t values where appropriate. --- include/coap/coap_session.h | 122 ++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git a/include/coap/coap_session.h b/include/coap/coap_session.h index 9662a634..27248b4b 100644 --- a/include/coap/coap_session.h +++ b/include/coap/coap_session.h @@ -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; /** @@ -317,5 +330,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 */ From ecf3321c676e7073bcef025d7e7750b43f8010ba Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Sun, 11 Feb 2018 16:06:02 +0000 Subject: [PATCH 2/5] net.h: Update and document coap_calc_timeout() function Include session as one of the parameters to coap_calc_timeout() as it is now per session, which is renamed from calc_timeout() as it is now a public function. --- include/coap/net.h | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/include/coap/net.h b/include/coap/net.h index 4ee99540..2ba18735 100644 --- a/include/coap/net.h +++ b/include/coap/net.h @@ -561,6 +561,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_ */ From fb626df651f12974f7ba0c311a400393524acd02 Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Sun, 11 Feb 2018 16:07:48 +0000 Subject: [PATCH 3/5] coap_session.c: Add in new functions, set the defaults and update calc_timeout Add in the new functions coap_session_set_max_retransmit() coap_session_set_ack_timeout() coap_session_set_ack_random_factor() coap_session_get_max_transmit() coap_session_get_ack_timeout() coap_session_get_ack_random_factor() Add in the default values in coap_make_session() Update coap_calc_timeout() in coap_session_delay_pdu() --- src/coap_session.c | 50 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/src/coap_session.c b/src/coap_session.c index d73424d3..432484f0 100644 --- a/src/coap_session.c +++ b/src/coap_session.c @@ -22,6 +22,51 @@ #include "encode.h" #include +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; @@ -88,6 +133,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)); @@ -214,7 +262,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); From 9c6c72565e62a739887bb9491fd09d9adb4cb85a Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Sun, 11 Feb 2018 16:10:47 +0000 Subject: [PATCH 4/5] net.c: Remove 'cc Rate Control', update code to use session recovery parameters Remove 'cc Rate Control' which is now in coap_session.h. Update Q, ACK_TIMEOUT and ACK_RANDOM_FACTOR #defines to include session's recovery parameters, now in coap_fixed_point_t. Update calc_timeout() to include session (now called coap_calc_timeout()). Update coap_retransmit() to use session's max_retransmit. --- src/net.c | 72 +++++++++++++------------------------------------------ 1 file changed, 17 insertions(+), 55 deletions(-) diff --git a/src/net.c b/src/net.c index bebbf3a1..03a6270c 100644 --- a/src/net.c +++ b/src/net.c @@ -59,47 +59,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. @@ -116,15 +75,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) @@ -768,22 +728,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)) @@ -904,7 +866,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); @@ -917,7 +879,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; From 7b2a7bca36ddc732e1f99746c773a0a8438af8da Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Sun, 11 Feb 2018 16:26:02 +0000 Subject: [PATCH 5/5] libcoap-1.map & libcoap-1.sym: Expose the new functions Add in the new functions coap_session_set_max_retransmit() coap_session_set_ack_timeout() coap_session_set_ack_random_factor() coap_session_get_max_transmit() coap_session_get_ack_timeout() coap_session_get_ack_random_factor() --- libcoap-1.map | 6 ++++++ libcoap-1.sym | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/libcoap-1.map b/libcoap-1.map index 18eee976..017a9d6c 100644 --- a/libcoap-1.map +++ b/libcoap-1.map @@ -148,8 +148,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; @@ -157,7 +160,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; diff --git a/libcoap-1.sym b/libcoap-1.sym index 1ad76f30..42ce6232 100644 --- a/libcoap-1.sym +++ b/libcoap-1.sym @@ -146,8 +146,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 @@ -155,7 +158,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