ROHC compression/decompression library
rohc_comp_internals.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010,2011,2012,2013,2014 Didier Barvaux
3  * Copyright 2012,2013,2014 Viveris Technologies
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /**
21  * @file rohc_comp_internals.h
22  * @brief Internal structures for ROHC compression
23  * @author Didier Barvaux <didier.barvaux@toulouse.viveris.com>
24  * @author Didier Barvaux <didier@barvaux.org>
25  */
26 
27 #ifndef ROHC_COMP_INTERNALS_H
28 #define ROHC_COMP_INTERNALS_H
29 
30 #include "rohc_internal.h"
31 #include "rohc_traces_internal.h"
32 #include "rohc_packets.h"
33 #include "rohc_comp.h"
34 #include "schemes/comp_wlsb.h"
35 #include "net_pkt.h"
36 #include "feedback.h"
37 
38 #include <stdbool.h>
39 
40 
41 /*
42  * Constants and macros
43  */
44 
45 /** The number of ROHC profiles ready to be used */
46 #define C_NUM_PROFILES 10U
47 
48 /** The default maximal number of packets sent in > IR states (= FO and SO
49  * states) before changing back the state to IR (periodic refreshes) */
50 #define CHANGE_TO_IR_COUNT 1700
51 
52 /** The default maximal delay (in ms) spent in > IR states (= FO and SO states)
53  * before changing back the state to IR (periodic refreshes) */
54 #define CHANGE_TO_IR_TIME 1000U
55 
56 /** The default maximal number of packets sent in > FO states (= SO state)
57  * before changing back the state to FO (periodic refreshes) */
58 #define CHANGE_TO_FO_COUNT 700
59 
60 /** The default maximal delay (in ms) spent in > FO states (= SO state)
61  * before changing back the state to FO (periodic refreshes) */
62 #define CHANGE_TO_FO_TIME 500U
63 
64 /** The minimal number of packets that must be sent while in IR state before
65  * being able to switch to the FO state */
66 #define MAX_IR_COUNT 3U
67 
68 /** The minimal number of packets that must be sent while in CR state before
69  * being able to switch to the FO state */
70 #define MAX_CR_COUNT MAX_IR_COUNT
71 
72 /** The minimal number of packets that must be sent while in FO state before
73  * being able to switch to the SO state */
74 #define MAX_FO_COUNT 3U
75 
76 /** The minimal number of packets that must be sent while in INIT_STRIDE
77  * state before being able to switch to the SEND_SCALED state */
78 #define ROHC_INIT_TS_STRIDE_MIN 3U
79 
80 /**
81  * @brief Default number of transmission for lists to become a reference list
82  *
83  * The minimal number of times of compressed list shall be sent to become
84  * a reference list. L is the name specified in the RFC.
85  */
86 #define ROHC_LIST_DEFAULT_L 5U
87 
88 
89 /** Print a warning trace for the given compression context */
90 #define rohc_comp_warn(context, format, ...) \
91  rohc_warning((context)->compressor, ROHC_TRACE_COMP, \
92  (context)->profile->id, \
93  format, ##__VA_ARGS__)
94 
95 /** Print a debug trace for the given compression context */
96 #define rohc_comp_debug(context, format, ...) \
97  rohc_debug((context)->compressor, ROHC_TRACE_COMP, \
98  (context)->profile->id, \
99  format, ##__VA_ARGS__)
100 
101 /** Dump a buffer for the given compression context */
102 #define rohc_comp_dump_buf(context, descr, buf, buf_len) \
103  do { \
104  if(((context)->compressor->features & ROHC_COMP_FEATURE_DUMP_PACKETS) != 0) { \
105  rohc_dump_buf((context)->compressor->trace_callback, \
106  (context)->compressor->trace_callback_priv, \
107  ROHC_TRACE_COMP, ROHC_TRACE_DEBUG, \
108  descr, buf, buf_len); \
109  } \
110  } while(0)
111 
112 
113 /*
114  * Declare ROHC compression structures that are defined at the end of this
115  * file but used by other structures at the beginning of the file.
116  */
117 
118 struct rohc_comp_ctxt;
119 
120 
121 /*
122  * Definitions of ROHC compression structures
123  */
124 
125 
126 /**
127  * @brief The ROHC compressor
128  */
129 struct rohc_comp
130 {
131  /** The medium associated with the decompressor */
133 
134  /** Enabled/disabled features for the compressor */
136 
137  /** The array of compression contexts that use the compressor */
139  /** The number of compression contexts in use in the array */
141 
142  /** Which profiles are enabled and with one are not? */
144 
145 
146  /* CRC-related variables: */
147 
148  /** The table to enable fast CRC-3 computation */
149  uint8_t crc_table_3[256];
150  /** The table to enable fast CRC-7 computation */
151  uint8_t crc_table_7[256];
152  /** The table to enable fast CRC-8 computation */
153  uint8_t crc_table_8[256];
154 
155 
156  /* segment-related variables */
157 
158 /** The maximal value for MRRU */
159 #define ROHC_MAX_MRRU 65535
160  /** The remaining bytes of the Reconstructed Reception Unit (RRU) waiting
161  * to be split into segments */
162  uint8_t rru[ROHC_MAX_MRRU];
163  /** The offset of the remaining bytes in the RRU buffer */
164  size_t rru_off;
165  /** The number of the remaining bytes in the RRU buffer */
166  size_t rru_len;
167 
168 
169  /* variables related to RTP detection */
170 
171  /** The callback function used to detect RTP packet */
173  /** Pointer to an external memory area provided/used by the callback user */
174  void *rtp_private;
175 
176 
177  /* some statistics about the compression process: */
178 
179  /** The number of sent packets */
181  /** The size of all the received uncompressed IP packets */
183  /** The size of all the sent compressed ROHC packets */
185 
186  /** The last context used by the compressor */
188 
189 
190  /* random callback */
191 
192  /** The user-defined callback for random numbers */
194  /** Private data that will be given to the callback for random numbers */
196 
197 
198  /* user interaction variables: */
199 
200  /** The width of the W-LSB sliding window */
202  /** The reorder offset specifies how much reordering is handled by the
203  * W-LSB encoding of the MSN in ROHCv2 profiles */
205  /** The maximal number of packets sent in > IR states (= FO and SO
206  * states) before changing back the state to IR (periodic refreshes) */
208  /** The maximal delay spent in > IR states (= FO and SO states) before
209  * changing back the state to IR (periodic refreshes) */
211  /** The maximal number of packets sent in > FO states (= SO state)
212  * before changing back the state to FO (periodic refreshes) */
214  /** The maximal delay spent in > FO states (= SO state) before changing back
215  * the state to FO (periodic refreshes) */
217  /** Maximum Reconstructed Reception Unit */
218  size_t mrru;
219  /** The connection type (currently not used) */
221  /** The number of uncompressed transmissions for list compression (L) */
223 
224  /** The callback function used to manage traces */
226  /** The private context of the callback function used to manage traces */
228 };
229 
230 
231 /**
232  * @brief The ROHC compression profile
233  *
234  * The object defines a ROHC profile. Each field must be filled in
235  * for each new profile.
236  */
238 {
239  /** The profile ID as reserved by IANA */
241 
242  /**
243  * @brief The IP protocol ID used to find out which profile is able to
244  * compress an IP packet
245  */
246  const unsigned short protocol;
247 
248  /**
249  * @brief The handler used to create the profile-specific part of the
250  * compression context from a given packet
251  */
252  bool (*create)(struct rohc_comp_ctxt *const context,
253  const struct net_pkt *const packet)
254  __attribute__((warn_unused_result, nonnull(1, 2)));
255 
256  /**
257  * @brief The handler used to create the profile-specific part of the
258  * compression context from a given context
259  */
260  bool (*clone)(struct rohc_comp_ctxt *const ctxt,
261  const struct rohc_comp_ctxt *const base_ctxt)
262  __attribute__((warn_unused_result, nonnull(1, 2)));
263 
264  /**
265  * @brief The handler used to destroy the profile-specific part of the
266  * compression context
267  */
268  void (*destroy)(struct rohc_comp_ctxt *const context)
269  __attribute__((nonnull(1)));
270 
271  /**
272  * @brief The handler used to check whether an uncompressed IP packet
273  * fits the current profile or not
274  */
275  bool (*check_profile)(const struct rohc_comp *const comp,
276  const struct net_pkt *const packet)
277  __attribute__((warn_unused_result, nonnull(1, 2)));
278 
279  /**
280  * @brief The handler used to check whether an uncompressed IP packet
281  * belongs to a context or not
282  */
283  bool (*check_context)(const struct rohc_comp_ctxt *const context,
284  const struct net_pkt *const packet,
285  size_t *const cr_score)
286  __attribute__((warn_unused_result, nonnull(1, 2, 3)));
287 
288  /**
289  * @brief The handler used to encode uncompressed IP packets
290  *
291  * @param context The compression context
292  * @param ip The IP packet to encode
293  * @param packet_size The length of the IP packet to encode
294  * @param rohc_pkt OUT: The ROHC packet
295  * @param rohc_pkt_max_len The maximum length of the ROHC packet
296  * @param packet_type OUT: The type of ROHC packet that is created
297  * @param payload_offset OUT: The offset for the payload in the IP packet
298  * @return The length of the ROHC packet if successful,
299  * -1 otherwise
300  */
301  int (*encode)(struct rohc_comp_ctxt *const context,
302  const struct net_pkt *const uncomp_pkt,
303  uint8_t *const rohc_pkt,
304  const size_t rohc_pkt_max_len,
305  rohc_packet_t *const packet_type,
306  size_t *const payload_offset)
307  __attribute__((warn_unused_result, nonnull(1, 2, 3, 5, 6)));
308 
309  /**
310  * @brief The handler used to warn the profile-specific part of the
311  * context about the arrival of feedback data
312  */
313  bool (*feedback)(struct rohc_comp_ctxt *const context,
314  const enum rohc_feedback_type feedback_type,
315  const uint8_t *const packet,
316  const size_t packet_len,
317  const uint8_t *const feedback_data,
318  const size_t feedback_data_len)
319  __attribute__((warn_unused_result, nonnull(1, 3, 5)));
320 };
321 
322 
323 /**
324  * @brief The ROHC compression context
325  */
327 {
328  /** Whether the context is in use or not */
329  int used;
330  /** The time when the context was created (in seconds) */
331  uint64_t latest_used;
332  /** The time when the context was last used (in seconds) */
333  uint64_t first_used;
334 
335  /** The context unique ID (CID) */
337 
338  /** The associated compressor */
340 
341  /** The associated profile */
342  const struct rohc_comp_profile *profile;
343  /** Profile-specific data, defined by the profiles */
344  void *specific;
345 
346  /** Whether Context Replication (CR) may be used */
348  /** The base context for Context Replication (CR) */
350 
351  /** The operation mode in which the context operates among:
352  * ROHC_U_MODE, ROHC_O_MODE, ROHC_R_MODE */
354  /** The operation state in which the context operates: IR, FO, SO */
356 
357  /* below are some statistics */
358 
359  /* The type of ROHC packet created for the last compressed packet */
361 
362  /** The number of packets sent while in Initialization & Refresh (IR) state */
363  size_t ir_count;
364  /** The number of packets sent while in First Order (FO) state */
365  size_t fo_count;
366  /** The number of packets sent while in Second Order (SO) state */
367  size_t so_count;
368  /** The number of packets sent while in Context Replication (CR) state */
369  size_t cr_count;
370 
371  /**
372  * @brief The number of packet sent while in SO state, used for the periodic
373  * refreshes of the context
374  * @see rohc_comp_periodic_down_transition
375  */
377  /**
378  * @brief The last time that the context was in FO state, used for the
379  * periodic refreshes of the context
380  * @see rohc_comp_periodic_down_transition
381  */
382  struct rohc_ts go_back_fo_time;
383  /**
384  * @brief The number of packet sent while in FO or SO state, used for the
385  * periodic refreshes of the context
386  * @see rohc_comp_periodic_down_transition
387  */
389  /**
390  * @brief The last time that the context was in IR state, used for the
391  * periodic refreshes of the context
392  * @see rohc_comp_periodic_down_transition
393  */
394  struct rohc_ts go_back_ir_time;
395 
396  /** The cumulated size of the uncompressed packets */
398  /** The cumulated size of the compressed packets */
400  /** The cumulated size of the uncompressed headers */
402  /** The cumulated size of the compressed headers */
404 
405  /** The total size of the last uncompressed packet */
407  /** The total size of the last compressed packet */
409  /** The header size of the last uncompressed packet */
411  /** The header size of the last compressed packet */
413 
414  /** The number of sent packets */
416 };
417 
418 
419 void rohc_comp_change_mode(struct rohc_comp_ctxt *const context,
420  const rohc_mode_t new_mode)
421  __attribute__((nonnull(1)));
422 
423 void rohc_comp_change_state(struct rohc_comp_ctxt *const context,
424  const rohc_comp_state_t new_state)
425  __attribute__((nonnull(1)));
426 
427 void rohc_comp_periodic_down_transition(struct rohc_comp_ctxt *const context,
428  const struct rohc_ts pkt_time)
429  __attribute__((nonnull(1)));
430 
431 bool rohc_comp_reinit_context(struct rohc_comp_ctxt *const context)
432  __attribute__((warn_unused_result, nonnull(1)));
433 
434 bool rohc_comp_feedback_parse_opts(const struct rohc_comp_ctxt *const context,
435  const uint8_t *const packet,
436  const size_t packet_len,
437  const uint8_t *const feedback_data,
438  const size_t feedback_data_len,
439  size_t opts_present[ROHC_FEEDBACK_OPT_MAX],
440  uint32_t *const sn_bits,
441  size_t *const sn_bits_nr,
442  const rohc_feedback_crc_t crc_type,
443  uint8_t crc_in_packet,
444  size_t crc_pos_from_end)
445  __attribute__((warn_unused_result, nonnull(1, 2, 4, 6, 7, 8)));
446 
447 #endif
448 
int total_last_uncompressed_size
Definition: rohc_comp_internals.h:406
rohc_reordering_offset_t reorder_ratio
Definition: rohc_comp_internals.h:204
int(* rohc_comp_random_cb_t)(const struct rohc_comp *const comp, void *const user_context)
The prototype of the callback for random numbers.
Definition: rohc_comp.h:289
struct rohc_comp_ctxt * contexts
Definition: rohc_comp_internals.h:138
void * random_cb_ctxt
Definition: rohc_comp_internals.h:195
size_t rohc_cid_t
Definition: rohc.h:195
int total_compressed_size
Definition: rohc_comp_internals.h:184
rohc_comp_features_t features
Definition: rohc_comp_internals.h:135
rohc_cid_t cid
Definition: rohc_comp_internals.h:336
rohc_reordering_offset_t
The different values of reordering offset.
Definition: rohc.h:203
rohc_mode_t
ROHC operation modes.
Definition: rohc.h:111
int connection_type
Definition: rohc_comp_internals.h:220
int header_last_uncompressed_size
Definition: rohc_comp_internals.h:410
int header_uncompressed_size
Definition: rohc_comp_internals.h:401
bool rohc_comp_reinit_context(struct rohc_comp_ctxt *const context)
Re-initialize the given context.
Definition: rohc_comp.c:3125
uint8_t crc_table_7[256]
Definition: rohc_comp_internals.h:151
void rohc_comp_change_mode(struct rohc_comp_ctxt *const context, const rohc_mode_t new_mode)
Change the mode of the context.
Definition: rohc_comp.c:2977
ROHC compression routines.
const unsigned short protocol
The IP protocol ID used to find out which profile is able to compress an IP packet.
Definition: rohc_comp_internals.h:246
The ROHC compressor.
Definition: rohc_comp_internals.h:129
int num_sent_packets
Definition: rohc_comp_internals.h:415
size_t num_contexts_used
Definition: rohc_comp_internals.h:140
uint64_t periodic_refreshes_fo_timeout_time
Definition: rohc_comp_internals.h:216
bool rohc_comp_feedback_parse_opts(const struct rohc_comp_ctxt *const context, const uint8_t *const packet, const size_t packet_len, const uint8_t *const feedback_data, const size_t feedback_data_len, size_t opts_present[ROHC_FEEDBACK_OPT_MAX], uint32_t *const sn_bits, size_t *const sn_bits_nr, const rohc_feedback_crc_t crc_type, uint8_t crc_in_packet, size_t crc_pos_from_end)
Parse FEEDBACK-2 options.
Definition: rohc_comp.c:3224
rohc_comp_random_cb_t random_cb
Definition: rohc_comp_internals.h:193
uint64_t latest_used
Definition: rohc_comp_internals.h:331
struct rohc_comp * compressor
Definition: rohc_comp_internals.h:339
Network packet (may contains several IP headers)
struct rohc_comp_ctxt * last_context
Definition: rohc_comp_internals.h:187
void * trace_callback_priv
Definition: rohc_comp_internals.h:227
void(* rohc_trace_callback2_t)(void *const priv_ctxt, const rohc_trace_level_t level, const rohc_trace_entity_t entity, const int profile, const char *const format,...)
The function prototype for the trace callback.
Definition: rohc_traces.h:118
int total_compressed_size
Definition: rohc_comp_internals.h:399
Window-based Least Significant Bits (W-LSB) encoding.
size_t go_back_fo_count
The number of packet sent while in SO state, used for the periodic refreshes of the context...
Definition: rohc_comp_internals.h:376
The ROHC compression profile.
Definition: rohc_comp_internals.h:237
struct rohc_medium medium
Definition: rohc_comp_internals.h:132
rohc_rtp_detection_callback_t rtp_callback
Definition: rohc_comp_internals.h:172
size_t wlsb_window_width
Definition: rohc_comp_internals.h:201
bool(* rohc_rtp_detection_callback_t)(const unsigned char *const ip, const unsigned char *const udp, const unsigned char *const payload, const unsigned int payload_size, void *const rtp_private)
The prototype of the RTP detection callback.
Definition: rohc_comp.h:264
The ROHC compression context.
Definition: rohc_comp_internals.h:326
rohc_mode_t mode
Definition: rohc_comp_internals.h:353
const struct rohc_comp_profile * profile
Definition: rohc_comp_internals.h:342
int used
Definition: rohc_comp_internals.h:329
size_t rru_off
Definition: rohc_comp_internals.h:164
const rohc_profile_t id
Definition: rohc_comp_internals.h:240
void * specific
Definition: rohc_comp_internals.h:344
Definition: net_pkt.h:39
rohc_comp_state_t state
Definition: rohc_comp_internals.h:355
bool do_ctxt_replication
Definition: rohc_comp_internals.h:347
size_t rru_len
Definition: rohc_comp_internals.h:166
int total_uncompressed_size
Definition: rohc_comp_internals.h:182
size_t so_count
Definition: rohc_comp_internals.h:367
int total_last_compressed_size
Definition: rohc_comp_internals.h:408
int total_uncompressed_size
Definition: rohc_comp_internals.h:397
bool enabled_profiles[C_NUM_PROFILES]
Definition: rohc_comp_internals.h:143
#define C_NUM_PROFILES
Definition: rohc_comp_internals.h:46
uint8_t crc_table_3[256]
Definition: rohc_comp_internals.h:149
A timestamp for the ROHC library.
Definition: rohc_time.h:51
rohc_packet_t packet_type
Definition: rohc_comp_internals.h:360
size_t fo_count
Definition: rohc_comp_internals.h:365
uint64_t first_used
Definition: rohc_comp_internals.h:333
rohc_comp_features_t
The different features of the ROHC compressor.
Definition: rohc_comp.h:226
rohc_comp_state_t
The different ROHC compressor states.
Definition: rohc_comp.h:83
size_t ir_count
Definition: rohc_comp_internals.h:363
rohc_trace_callback2_t trace_callback
Definition: rohc_comp_internals.h:225
Definition of ROHC packets and extensions.
int header_compressed_size
Definition: rohc_comp_internals.h:403
void rohc_comp_change_state(struct rohc_comp_ctxt *const context, const rohc_comp_state_t new_state)
Change the state of the context.
Definition: rohc_comp.c:3012
int header_last_compressed_size
Definition: rohc_comp_internals.h:412
size_t list_trans_nr
Definition: rohc_comp_internals.h:222
void * rtp_private
Definition: rohc_comp_internals.h:174
size_t go_back_ir_count
The number of packet sent while in FO or SO state, used for the periodic refreshes of the context...
Definition: rohc_comp_internals.h:388
ROHC medium (CID characteristics)
Definition: rohc_internal.h:146
int num_packets
Definition: rohc_comp_internals.h:180
ROHC private common definitions and routines.
Definition: feedback.h:95
size_t periodic_refreshes_ir_timeout_pkts
Definition: rohc_comp_internals.h:207
size_t cr_count
Definition: rohc_comp_internals.h:369
size_t periodic_refreshes_fo_timeout_pkts
Definition: rohc_comp_internals.h:213
rohc_cid_t cr_base_cid
Definition: rohc_comp_internals.h:349
rohc_feedback_type
Definition: feedback.h:42
rohc_profile_t
The different ROHC compression/decompression profiles.
Definition: rohc_profiles.h:70
uint8_t crc_table_8[256]
Definition: rohc_comp_internals.h:153
uint8_t rru[ROHC_MAX_MRRU]
Definition: rohc_comp_internals.h:162
uint64_t periodic_refreshes_ir_timeout_time
Definition: rohc_comp_internals.h:210
rohc_packet_t
The different types of ROHC packets.
Definition: rohc_packets.h:55
void rohc_comp_periodic_down_transition(struct rohc_comp_ctxt *const context, const struct rohc_ts pkt_time)
Periodically change the context state after a certain number of packets.
Definition: rohc_comp.c:3039
size_t mrru
Definition: rohc_comp_internals.h:218
#define ROHC_MAX_MRRU
Definition: rohc_comp_internals.h:159
ROHC feedback definitions and formats.
rohc_feedback_crc_t
Whether the feedback is protected by a CRC or not.
Definition: feedback.h:62
Internal ROHC macros and functions for traces.