50 | | TfrcSndr::TfrcSndr() { |
51 | | } |
| 51 | TfrcSndr::TfrcSndr() : |
| 52 | seqno_(0), |
| 53 | x_rate_(0), |
| 54 | aoa_(0), |
| 55 | now_(0), |
| 56 | so_recv_(0), |
| 57 | ndtp_(0), |
| 58 | nakp_(0), |
| 59 | ntep_(0), |
| 60 | nsve_(0), |
| 61 | jacked_(0), |
| 62 | begins_(0), |
| 63 | ends_(0), |
| 64 | num_elm_(1), |
| 65 | num_vec_(1) |
| 66 | { |
| 67 | // allocate tsvec_ in memory |
| 68 | tsvec_ = (double *) malloc(sizeof(double) * TSZ); |
| 69 | clear_tsv(TSZ); |
| 70 | // allocate seqvec in memory |
| 71 | seqvec_ = (u_int32_t *) malloc(sizeof(u_int32_t) * SSZ); |
| 72 | clear_sqv(SSZ); |
| 73 | num_seqvec_ = 0; |
| 74 | // allocate refvec in memory |
| 75 | refvec_ = (u_int32_t *) malloc(sizeof(u_int32_t) + RSZ); |
| 76 | clear_refv(RSZ); |
| 77 | num_refvec_ = 0; |
| 78 | |
| 79 | // initialize variables |
| 80 | ts_ = 0.0; |
| 81 | num_missing_ = 0; |
| 82 | |
| 83 | // EWMA packet size |
| 84 | asize_ = 0; |
| 85 | pcnt_ = 0; |
| 86 | psize_ = 1000; |
| 87 | lambda1_ = .75; |
| 88 | lambda2_ = .15; |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * TFRC send |
| 93 | */ |
| 94 | void TfrcSndr::send(pktbuf* pb, double now) { |
| 95 | // the very first data packet |
| 96 | if(seqno_ == 0) |
| 97 | ts_off_ = tx_ts_offset(); |
| 98 | |
| 99 | // parse seqno and mark timestamp for this data packet |
| 100 | rtphdr* rh = (rtphdr *) pb->data; |
| 101 | seqno_ = ntohs(rh->rh_seqno); |
| 102 | now_ = now; |
| 103 | |
| 104 | // alrithmetic average packet size (per frame) |
| 105 | asize_ += pb->len; |
| 106 | pcnt_++; |
| 107 | |
| 108 | // tag finished (end of frame) |
| 109 | if (!(pb->tag)) { |
| 110 | asize_ /= pcnt_; |
| 111 | // EWMA'd packet size |
| 112 | if (pcnt_ != 1) |
| 113 | psize_ = lambda1_ * asize_ + (1 - lambda1_) * psize_; |
| 114 | else |
| 115 | psize_ = lambda2_ * asize_ + (1 - lambda2_) * psize_; |
| 116 | |
| 117 | asize_ = 0; pcnt_ = 0; |
| 118 | } |
| 119 | |
| 120 | // timestamp vector for loss history update |
| 121 | tsvec_[seqno_%TSZ] = now_-SKEW; |
| 122 | |
| 123 | // sequence number must be greater than zero |
| 124 | assert (seqno_ > 0); |
| 125 | // number of total data packet sent |
| 126 | ndtp_++; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * main TFRC reception path |
| 131 | */ |
| 132 | void TfrcSndr::recv(u_int16_t type, u_int16_t begin, u_int16_t end, |
| 133 | u_int16_t *chunk, double so_rtime, bool recv_by_ch, pktbuf* pb) |
| 134 | { |
| 135 | UNUSED(recv_by_ch); |
| 136 | UNUSED(pb); |
| 137 | |
| 138 | switch(type) { |
| 139 | // XR block type 1 |
| 140 | case XR_BT_1: |
| 141 | { |
| 142 | // number of ack received |
| 143 | nakp_++; |
| 144 | // so_timestamp |
| 145 | so_recv_ = so_rtime; |
| 146 | |
| 147 | // get start/end seqno that this XR chunk reports |
| 148 | begins_ = begin; // lowest packet seqno |
| 149 | ends_ = end; // highest packet seqno plus one |
| 150 | |
| 151 | // just acked seqno |
| 152 | // i.e.,) head seqno(= highest seqno) of this ackvec |
| 153 | jacked_ = ends_ - 1; |
| 154 | |
| 155 | // get the number of AckVec chunks |
| 156 | // use seqno space to work out the num chunks |
| 157 | // (add one to num unless exactly divisible by BITLEN |
| 158 | // - so it is large enough to accomodate all the bits) |
| 159 | num_elm_ = get_numelm(begins_, jacked_); |
| 160 | num_vec_ = get_numvec(num_elm_); |
| 161 | |
| 162 | // declared AckVec |
| 163 | ackv_ = (u_int16_t *) malloc (sizeof(u_int16_t) * num_vec_); |
| 164 | // clear the existing AckVec |
| 165 | clear_ackv(num_vec_); |
| 166 | // clone AckVec from Vic |
| 167 | clone_ackv(chunk, num_vec_); |
| 168 | |
| 169 | // generate seqno vector |
| 170 | gen_seqvec(ackv_, num_vec_); |
| 171 | // generate reference vector |
| 172 | // (it represents seqvec when no losses) |
| 173 | // @begin: aoa_+1 (lowest seqno) |
| 174 | // @end: jacked_ |
| 175 | gen_refvec(jacked_, aoa_+1); |
| 176 | |
| 177 | // TFRC congestioin control |
| 178 | calc_rate(); |
| 179 | |
| 180 | // set ackofack (real number) |
| 181 | aoa_ = jacked_; |
| 182 | |
| 183 | // sampled RTT |
| 184 | tao_ = so_recv_ - tsvec_[jacked_%TSZ]; |
| 185 | // update RTT with the sampled RTT |
| 186 | update_rtt(tao_); |
| 187 | } |
| 188 | break; |
| 189 | |
| 190 | // XR block type 3 |
| 191 | case XR_BT_3: |
| 192 | {} |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * generate seqno vector |
| 199 | * (interpret the received AckVec to real sequence numbers) |
| 200 | * @ackvec: receivec AckVec |
| 201 | */ |
| 202 | void TfrcSndr::gen_seqvec(u_int16_t *v, int n) { |
| 203 | // clear seqvec before starts |
| 204 | clear_sqv(num_seqvec_); |
| 205 | |
| 206 | int i, j, k = 0; |
| 207 | int x = num_elm_%BITLEN; |
| 208 | |
| 209 | // start of seqvec (lowest seqno) |
| 210 | int start = begins_; |
| 211 | |
| 212 | for (i = 0; i < n-1; i++) { |
| 213 | for (j = BITLEN; j > 0; j--) { |
| 214 | if( CHECK_BIT_AT(v[i], j) ) |
| 215 | seqvec_[k++%SSZ] = start; |
| 216 | else num_missing_++; |
| 217 | start++; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | int a = (x == 0) ? BITLEN : x; |
| 222 | for (i = a; i > 0; i--) { |
| 223 | if( CHECK_BIT_AT(v[n-1], i) ) |
| 224 | seqvec_[k++%SSZ] = start; |
| 225 | else num_missing_++; |
| 226 | start++; |
| 227 | } |
| 228 | |
| 229 | // therefore, the number of seqvec elements is: |
| 230 | num_seqvec_ = num_elm_ - num_missing_; |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * generate reference vector |
| 235 | * (it represents seqno vector when no losses) |
| 236 | * @end: end seqno (highest) |
| 237 | * @begin: begin seqno (lowest) |
| 238 | */ |
| 239 | void TfrcSndr::gen_refvec(int end, int begin) { |
| 240 | // clear previous reference vector |
| 241 | clear_refv(num_refvec_); |
| 242 | // number of reference element - when no loss |
| 243 | num_refvec_ = end - begin + 1; |
| 244 | |
| 245 | // generate refvec elements |
| 246 | fprintf(stderr, "\tcomparing numbers: ("); |
| 247 | for (int i = 0; i < num_refvec_; i++) { |
| 248 | refvec_[i] = begin + i; |
| 249 | fprintf(stderr, " %d", refvec_[i]); |
| 250 | } fprintf(stderr, " )\n"); |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * calculate sending rate |
| 255 | */ |
| 256 | void TfrcSndr::calc_rate() { |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * update RTT using sampled RTT value |
| 261 | */ |
| 262 | void TfrcSndr::update_rtt(double tao) { |
| 263 | } |