Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011-2012, Pavel Zubarev <pavel.zubarev@gmail.com> |
| 3 | * Copyright 2011-2012, Marco Porsch <marco.porsch@s2005.tu-chemnitz.de> |
| 4 | * Copyright 2011-2012, cozybit Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | #include "ieee80211_i.h" |
| 12 | #include "mesh.h" |
| 13 | #include "driver-ops.h" |
| 14 | |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 15 | /* This is not in the standard. It represents a tolerable tbtt drift below |
| 16 | * which we do no TSF adjustment. |
| 17 | */ |
Javier Cardona | a802a6e | 2012-04-12 14:32:22 -0700 | [diff] [blame] | 18 | #define TOFFSET_MINIMUM_ADJUSTMENT 10 |
| 19 | |
Javier Cardona | ec14bcd | 2012-04-12 14:32:23 -0700 | [diff] [blame] | 20 | /* This is not in the standard. It is a margin added to the |
| 21 | * Toffset setpoint to mitigate TSF overcorrection |
| 22 | * introduced by TSF adjustment latency. |
| 23 | */ |
| 24 | #define TOFFSET_SET_MARGIN 20 |
| 25 | |
Javier Cardona | a802a6e | 2012-04-12 14:32:22 -0700 | [diff] [blame] | 26 | /* This is not in the standard. It represents the maximum Toffset jump above |
| 27 | * which we'll invalidate the Toffset setpoint and choose a new setpoint. This |
| 28 | * could be, for instance, in case a neighbor is restarted and its TSF counter |
| 29 | * reset. |
Javier Cardona | ec14bcd | 2012-04-12 14:32:23 -0700 | [diff] [blame] | 30 | */ |
Pedersen, Thomas | 3a53731 | 2016-09-28 16:56:31 -0700 | [diff] [blame] | 31 | #define TOFFSET_MAXIMUM_ADJUSTMENT 800 /* 0.8 ms */ |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 32 | |
| 33 | struct sync_method { |
| 34 | u8 method; |
| 35 | struct ieee80211_mesh_sync_ops ops; |
| 36 | }; |
| 37 | |
| 38 | /** |
| 39 | * mesh_peer_tbtt_adjusting - check if an mp is currently adjusting its TBTT |
| 40 | * |
| 41 | * @ie: information elements of a management frame from the mesh peer |
| 42 | */ |
| 43 | static bool mesh_peer_tbtt_adjusting(struct ieee802_11_elems *ie) |
| 44 | { |
| 45 | return (ie->mesh_config->meshconf_cap & |
Johannes Berg | bf7cd94 | 2013-02-15 14:40:31 +0100 | [diff] [blame] | 46 | IEEE80211_MESHCONF_CAPAB_TBTT_ADJUSTING) != 0; |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void mesh_sync_adjust_tbtt(struct ieee80211_sub_if_data *sdata) |
| 50 | { |
| 51 | struct ieee80211_local *local = sdata->local; |
| 52 | struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh; |
| 53 | /* sdata->vif.bss_conf.beacon_int in 1024us units, 0.04% */ |
| 54 | u64 beacon_int_fraction = sdata->vif.bss_conf.beacon_int * 1024 / 2500; |
| 55 | u64 tsf; |
| 56 | u64 tsfdelta; |
| 57 | |
| 58 | spin_lock_bh(&ifmsh->sync_offset_lock); |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 59 | if (ifmsh->sync_offset_clockdrift_max < beacon_int_fraction) { |
Johannes Berg | bdcbd8e | 2012-06-22 11:29:50 +0200 | [diff] [blame] | 60 | msync_dbg(sdata, "TBTT : max clockdrift=%lld; adjusting\n", |
| 61 | (long long) ifmsh->sync_offset_clockdrift_max); |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 62 | tsfdelta = -ifmsh->sync_offset_clockdrift_max; |
| 63 | ifmsh->sync_offset_clockdrift_max = 0; |
| 64 | } else { |
Johannes Berg | bdcbd8e | 2012-06-22 11:29:50 +0200 | [diff] [blame] | 65 | msync_dbg(sdata, "TBTT : max clockdrift=%lld; adjusting by %llu\n", |
| 66 | (long long) ifmsh->sync_offset_clockdrift_max, |
| 67 | (unsigned long long) beacon_int_fraction); |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 68 | tsfdelta = -beacon_int_fraction; |
| 69 | ifmsh->sync_offset_clockdrift_max -= beacon_int_fraction; |
| 70 | } |
Thomas Pedersen | 55fabef | 2012-10-05 17:57:39 -0700 | [diff] [blame] | 71 | spin_unlock_bh(&ifmsh->sync_offset_lock); |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 72 | |
Pedersen, Thomas | 354d381 | 2016-09-28 16:56:28 -0700 | [diff] [blame] | 73 | if (local->ops->offset_tsf) { |
| 74 | drv_offset_tsf(local, sdata, tsfdelta); |
| 75 | } else { |
| 76 | tsf = drv_get_tsf(local, sdata); |
| 77 | if (tsf != -1ULL) |
| 78 | drv_set_tsf(local, sdata, tsf + tsfdelta); |
| 79 | } |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static void mesh_sync_offset_rx_bcn_presp(struct ieee80211_sub_if_data *sdata, |
| 83 | u16 stype, |
| 84 | struct ieee80211_mgmt *mgmt, |
| 85 | struct ieee802_11_elems *elems, |
| 86 | struct ieee80211_rx_status *rx_status) |
| 87 | { |
| 88 | struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh; |
| 89 | struct ieee80211_local *local = sdata->local; |
| 90 | struct sta_info *sta; |
| 91 | u64 t_t, t_r; |
| 92 | |
| 93 | WARN_ON(ifmsh->mesh_sp_id != IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET); |
| 94 | |
| 95 | /* standard mentions only beacons */ |
| 96 | if (stype != IEEE80211_STYPE_BEACON) |
| 97 | return; |
| 98 | |
Bob Copeland | ce95320 | 2013-11-18 17:25:28 -0500 | [diff] [blame] | 99 | /* |
| 100 | * Get time when timestamp field was received. If we don't |
| 101 | * have rx timestamps, then use current tsf as an approximation. |
| 102 | * drv_get_tsf() must be called before entering the rcu-read |
| 103 | * section. |
| 104 | */ |
| 105 | if (ieee80211_have_rx_timestamp(rx_status)) |
| 106 | t_r = ieee80211_calculate_rx_timestamp(local, rx_status, |
| 107 | 24 + 12 + |
| 108 | elems->total_len + |
| 109 | FCS_LEN, |
| 110 | 24); |
| 111 | else |
| 112 | t_r = drv_get_tsf(local, sdata); |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 113 | |
| 114 | rcu_read_lock(); |
| 115 | sta = sta_info_get(sdata, mgmt->sa); |
| 116 | if (!sta) |
| 117 | goto no_sync; |
| 118 | |
| 119 | /* check offset sync conditions (13.13.2.2.1) |
| 120 | * |
| 121 | * TODO also sync to |
| 122 | * dot11MeshNbrOffsetMaxNeighbor non-peer non-MBSS neighbors |
| 123 | */ |
| 124 | |
| 125 | if (elems->mesh_config && mesh_peer_tbtt_adjusting(elems)) { |
| 126 | clear_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN); |
Johannes Berg | bf7cd94 | 2013-02-15 14:40:31 +0100 | [diff] [blame] | 127 | msync_dbg(sdata, "STA %pM : is adjusting TBTT\n", |
| 128 | sta->sta.addr); |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 129 | goto no_sync; |
| 130 | } |
| 131 | |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 132 | /* Timing offset calculation (see 13.13.2.2.2) */ |
| 133 | t_t = le64_to_cpu(mgmt->u.beacon.timestamp); |
Johannes Berg | 433f5bc | 2015-06-17 10:31:00 +0200 | [diff] [blame] | 134 | sta->mesh->t_offset = t_t - t_r; |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 135 | |
| 136 | if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) { |
Johannes Berg | 433f5bc | 2015-06-17 10:31:00 +0200 | [diff] [blame] | 137 | s64 t_clockdrift = sta->mesh->t_offset_setpoint - sta->mesh->t_offset; |
Johannes Berg | bdcbd8e | 2012-06-22 11:29:50 +0200 | [diff] [blame] | 138 | msync_dbg(sdata, |
Johannes Berg | 433f5bc | 2015-06-17 10:31:00 +0200 | [diff] [blame] | 139 | "STA %pM : t_offset=%lld, t_offset_setpoint=%lld, t_clockdrift=%lld\n", |
| 140 | sta->sta.addr, (long long) sta->mesh->t_offset, |
| 141 | (long long) sta->mesh->t_offset_setpoint, |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 142 | (long long) t_clockdrift); |
Javier Cardona | a802a6e | 2012-04-12 14:32:22 -0700 | [diff] [blame] | 143 | |
| 144 | if (t_clockdrift > TOFFSET_MAXIMUM_ADJUSTMENT || |
Johannes Berg | bf7cd94 | 2013-02-15 14:40:31 +0100 | [diff] [blame] | 145 | t_clockdrift < -TOFFSET_MAXIMUM_ADJUSTMENT) { |
Johannes Berg | bdcbd8e | 2012-06-22 11:29:50 +0200 | [diff] [blame] | 146 | msync_dbg(sdata, |
| 147 | "STA %pM : t_clockdrift=%lld too large, setpoint reset\n", |
Javier Cardona | a802a6e | 2012-04-12 14:32:22 -0700 | [diff] [blame] | 148 | sta->sta.addr, |
| 149 | (long long) t_clockdrift); |
| 150 | clear_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN); |
| 151 | goto no_sync; |
| 152 | } |
| 153 | |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 154 | spin_lock_bh(&ifmsh->sync_offset_lock); |
Johannes Berg | bf7cd94 | 2013-02-15 14:40:31 +0100 | [diff] [blame] | 155 | if (t_clockdrift > ifmsh->sync_offset_clockdrift_max) |
| 156 | ifmsh->sync_offset_clockdrift_max = t_clockdrift; |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 157 | spin_unlock_bh(&ifmsh->sync_offset_lock); |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 158 | } else { |
Johannes Berg | 433f5bc | 2015-06-17 10:31:00 +0200 | [diff] [blame] | 159 | sta->mesh->t_offset_setpoint = sta->mesh->t_offset - TOFFSET_SET_MARGIN; |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 160 | set_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN); |
Johannes Berg | bdcbd8e | 2012-06-22 11:29:50 +0200 | [diff] [blame] | 161 | msync_dbg(sdata, |
Johannes Berg | 433f5bc | 2015-06-17 10:31:00 +0200 | [diff] [blame] | 162 | "STA %pM : offset was invalid, t_offset=%lld\n", |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 163 | sta->sta.addr, |
Johannes Berg | 433f5bc | 2015-06-17 10:31:00 +0200 | [diff] [blame] | 164 | (long long) sta->mesh->t_offset); |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 165 | } |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 166 | |
| 167 | no_sync: |
| 168 | rcu_read_unlock(); |
| 169 | } |
| 170 | |
Thomas Pedersen | 43552be | 2013-12-15 13:14:16 -0800 | [diff] [blame] | 171 | static void mesh_sync_offset_adjust_tbtt(struct ieee80211_sub_if_data *sdata, |
| 172 | struct beacon_data *beacon) |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 173 | { |
| 174 | struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh; |
Thomas Pedersen | 43552be | 2013-12-15 13:14:16 -0800 | [diff] [blame] | 175 | u8 cap; |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 176 | |
Johannes Berg | bf7cd94 | 2013-02-15 14:40:31 +0100 | [diff] [blame] | 177 | WARN_ON(ifmsh->mesh_sp_id != IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET); |
Johannes Berg | 8c5bb1f | 2014-04-29 17:55:26 +0200 | [diff] [blame] | 178 | WARN_ON(!rcu_read_lock_held()); |
Thomas Pedersen | 43552be | 2013-12-15 13:14:16 -0800 | [diff] [blame] | 179 | cap = beacon->meshconf->meshconf_cap; |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 180 | |
| 181 | spin_lock_bh(&ifmsh->sync_offset_lock); |
| 182 | |
Johannes Berg | bf7cd94 | 2013-02-15 14:40:31 +0100 | [diff] [blame] | 183 | if (ifmsh->sync_offset_clockdrift_max > TOFFSET_MINIMUM_ADJUSTMENT) { |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 184 | /* Since ajusting the tsf here would |
| 185 | * require a possibly blocking call |
| 186 | * to the driver tsf setter, we punt |
| 187 | * the tsf adjustment to the mesh tasklet |
| 188 | */ |
Johannes Berg | bdcbd8e | 2012-06-22 11:29:50 +0200 | [diff] [blame] | 189 | msync_dbg(sdata, |
| 190 | "TBTT : kicking off TBTT adjustment with clockdrift_max=%lld\n", |
| 191 | ifmsh->sync_offset_clockdrift_max); |
Johannes Berg | bf7cd94 | 2013-02-15 14:40:31 +0100 | [diff] [blame] | 192 | set_bit(MESH_WORK_DRIFT_ADJUST, &ifmsh->wrkq_flags); |
Marco Porsch | 3720390 | 2012-11-23 12:23:18 -0800 | [diff] [blame] | 193 | |
| 194 | ifmsh->adjusting_tbtt = true; |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 195 | } else { |
Johannes Berg | bdcbd8e | 2012-06-22 11:29:50 +0200 | [diff] [blame] | 196 | msync_dbg(sdata, |
| 197 | "TBTT : max clockdrift=%lld; too small to adjust\n", |
| 198 | (long long)ifmsh->sync_offset_clockdrift_max); |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 199 | ifmsh->sync_offset_clockdrift_max = 0; |
Marco Porsch | 3720390 | 2012-11-23 12:23:18 -0800 | [diff] [blame] | 200 | |
| 201 | ifmsh->adjusting_tbtt = false; |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 202 | } |
| 203 | spin_unlock_bh(&ifmsh->sync_offset_lock); |
Thomas Pedersen | 43552be | 2013-12-15 13:14:16 -0800 | [diff] [blame] | 204 | |
| 205 | beacon->meshconf->meshconf_cap = ifmsh->adjusting_tbtt ? |
| 206 | IEEE80211_MESHCONF_CAPAB_TBTT_ADJUSTING | cap : |
| 207 | ~IEEE80211_MESHCONF_CAPAB_TBTT_ADJUSTING & cap; |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Johannes Berg | 8ba7acf | 2012-09-30 17:07:19 +0200 | [diff] [blame] | 210 | static const struct sync_method sync_methods[] = { |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 211 | { |
| 212 | .method = IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET, |
| 213 | .ops = { |
| 214 | .rx_bcn_presp = &mesh_sync_offset_rx_bcn_presp, |
| 215 | .adjust_tbtt = &mesh_sync_offset_adjust_tbtt, |
| 216 | } |
| 217 | }, |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 218 | }; |
| 219 | |
Johannes Berg | 8ba7acf | 2012-09-30 17:07:19 +0200 | [diff] [blame] | 220 | const struct ieee80211_mesh_sync_ops *ieee80211_mesh_sync_ops_get(u8 method) |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 221 | { |
Johannes Berg | bf7cd94 | 2013-02-15 14:40:31 +0100 | [diff] [blame] | 222 | int i; |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 223 | |
| 224 | for (i = 0 ; i < ARRAY_SIZE(sync_methods); ++i) { |
Johannes Berg | bf7cd94 | 2013-02-15 14:40:31 +0100 | [diff] [blame] | 225 | if (sync_methods[i].method == method) |
| 226 | return &sync_methods[i].ops; |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 227 | } |
Johannes Berg | bf7cd94 | 2013-02-15 14:40:31 +0100 | [diff] [blame] | 228 | return NULL; |
Javier Cardona | dbf498f | 2012-03-31 11:31:32 -0700 | [diff] [blame] | 229 | } |