• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * ngtcp2
3  *
4  * Copyright (c) 2019 ngtcp2 contributors
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 #include "ngtcp2_pv.h"
26 
27 #include <string.h>
28 #include <assert.h>
29 
30 #include "ngtcp2_mem.h"
31 #include "ngtcp2_log.h"
32 #include "ngtcp2_macro.h"
33 #include "ngtcp2_addr.h"
34 
ngtcp2_pv_entry_init(ngtcp2_pv_entry * pvent,const uint8_t * data,ngtcp2_tstamp expiry,uint8_t flags)35 void ngtcp2_pv_entry_init(ngtcp2_pv_entry *pvent, const uint8_t *data,
36                           ngtcp2_tstamp expiry, uint8_t flags) {
37   memcpy(pvent->data, data, sizeof(pvent->data));
38   pvent->expiry = expiry;
39   pvent->flags = flags;
40 }
41 
ngtcp2_pv_new(ngtcp2_pv ** ppv,const ngtcp2_dcid * dcid,ngtcp2_duration timeout,uint8_t flags,ngtcp2_log * log,const ngtcp2_mem * mem)42 int ngtcp2_pv_new(ngtcp2_pv **ppv, const ngtcp2_dcid *dcid,
43                   ngtcp2_duration timeout, uint8_t flags, ngtcp2_log *log,
44                   const ngtcp2_mem *mem) {
45   (*ppv) = ngtcp2_mem_malloc(mem, sizeof(ngtcp2_pv));
46   if (*ppv == NULL) {
47     return NGTCP2_ERR_NOMEM;
48   }
49 
50   ngtcp2_static_ringbuf_pv_ents_init(&(*ppv)->ents);
51 
52   ngtcp2_dcid_copy(&(*ppv)->dcid, dcid);
53 
54   (*ppv)->mem = mem;
55   (*ppv)->log = log;
56   (*ppv)->timeout = timeout;
57   (*ppv)->fallback_pto = 0;
58   (*ppv)->started_ts = UINT64_MAX;
59   (*ppv)->probe_pkt_left = NGTCP2_PV_NUM_PROBE_PKT;
60   (*ppv)->round = 0;
61   (*ppv)->flags = flags;
62 
63   return 0;
64 }
65 
ngtcp2_pv_del(ngtcp2_pv * pv)66 void ngtcp2_pv_del(ngtcp2_pv *pv) {
67   if (pv == NULL) {
68     return;
69   }
70   ngtcp2_mem_free(pv->mem, pv);
71 }
72 
ngtcp2_pv_add_entry(ngtcp2_pv * pv,const uint8_t * data,ngtcp2_tstamp expiry,uint8_t flags,ngtcp2_tstamp ts)73 void ngtcp2_pv_add_entry(ngtcp2_pv *pv, const uint8_t *data,
74                          ngtcp2_tstamp expiry, uint8_t flags,
75                          ngtcp2_tstamp ts) {
76   ngtcp2_pv_entry *ent;
77 
78   assert(pv->probe_pkt_left);
79 
80   if (ngtcp2_ringbuf_len(&pv->ents.rb) == 0) {
81     pv->started_ts = ts;
82   }
83 
84   ent = ngtcp2_ringbuf_push_back(&pv->ents.rb);
85   ngtcp2_pv_entry_init(ent, data, expiry, flags);
86 
87   pv->flags &= (uint8_t)~NGTCP2_PV_FLAG_CANCEL_TIMER;
88   --pv->probe_pkt_left;
89 }
90 
ngtcp2_pv_validate(ngtcp2_pv * pv,uint8_t * pflags,const uint8_t * data)91 int ngtcp2_pv_validate(ngtcp2_pv *pv, uint8_t *pflags, const uint8_t *data) {
92   size_t len = ngtcp2_ringbuf_len(&pv->ents.rb);
93   size_t i;
94   ngtcp2_pv_entry *ent;
95 
96   if (len == 0) {
97     return NGTCP2_ERR_INVALID_STATE;
98   }
99 
100   for (i = 0; i < len; ++i) {
101     ent = ngtcp2_ringbuf_get(&pv->ents.rb, i);
102     if (memcmp(ent->data, data, sizeof(ent->data)) == 0) {
103       *pflags = ent->flags;
104       ngtcp2_log_info(pv->log, NGTCP2_LOG_EVENT_PTV, "path has been validated");
105       return 0;
106     }
107   }
108 
109   return NGTCP2_ERR_INVALID_ARGUMENT;
110 }
111 
ngtcp2_pv_handle_entry_expiry(ngtcp2_pv * pv,ngtcp2_tstamp ts)112 void ngtcp2_pv_handle_entry_expiry(ngtcp2_pv *pv, ngtcp2_tstamp ts) {
113   ngtcp2_pv_entry *ent;
114 
115   if (ngtcp2_ringbuf_len(&pv->ents.rb) == 0) {
116     return;
117   }
118 
119   ent = ngtcp2_ringbuf_get(&pv->ents.rb, ngtcp2_ringbuf_len(&pv->ents.rb) - 1);
120 
121   if (ent->expiry > ts) {
122     return;
123   }
124 
125   ++pv->round;
126   pv->probe_pkt_left = NGTCP2_PV_NUM_PROBE_PKT;
127 }
128 
ngtcp2_pv_should_send_probe(ngtcp2_pv * pv)129 int ngtcp2_pv_should_send_probe(ngtcp2_pv *pv) {
130   return pv->probe_pkt_left > 0;
131 }
132 
ngtcp2_pv_validation_timed_out(ngtcp2_pv * pv,ngtcp2_tstamp ts)133 int ngtcp2_pv_validation_timed_out(ngtcp2_pv *pv, ngtcp2_tstamp ts) {
134   ngtcp2_tstamp t;
135   ngtcp2_pv_entry *ent;
136 
137   if (pv->started_ts == UINT64_MAX) {
138     return 0;
139   }
140 
141   assert(ngtcp2_ringbuf_len(&pv->ents.rb));
142 
143   ent = ngtcp2_ringbuf_get(&pv->ents.rb, ngtcp2_ringbuf_len(&pv->ents.rb) - 1);
144 
145   t = pv->started_ts + pv->timeout;
146   t = ngtcp2_max(t, ent->expiry);
147 
148   return t <= ts;
149 }
150 
ngtcp2_pv_next_expiry(ngtcp2_pv * pv)151 ngtcp2_tstamp ngtcp2_pv_next_expiry(ngtcp2_pv *pv) {
152   ngtcp2_pv_entry *ent;
153 
154   if ((pv->flags & NGTCP2_PV_FLAG_CANCEL_TIMER) ||
155       ngtcp2_ringbuf_len(&pv->ents.rb) == 0) {
156     return UINT64_MAX;
157   }
158 
159   ent = ngtcp2_ringbuf_get(&pv->ents.rb, ngtcp2_ringbuf_len(&pv->ents.rb) - 1);
160 
161   return ent->expiry;
162 }
163 
ngtcp2_pv_cancel_expired_timer(ngtcp2_pv * pv,ngtcp2_tstamp ts)164 void ngtcp2_pv_cancel_expired_timer(ngtcp2_pv *pv, ngtcp2_tstamp ts) {
165   ngtcp2_tstamp expiry = ngtcp2_pv_next_expiry(pv);
166 
167   if (expiry > ts) {
168     return;
169   }
170 
171   pv->flags |= NGTCP2_PV_FLAG_CANCEL_TIMER;
172 }
173