Holger Hans Peter Freyther | a8ce061 | 2011-01-20 16:30:24 +0100 | [diff] [blame] | 1 | /* MTP level3 link */ |
| 2 | /* |
| 3 | * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 4 | * (C) 2010 by On-Waves |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software: you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU Affero General Public License as published by |
| 9 | * the Free Software Foundation, either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU Affero General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Affero General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <mtp_data.h> |
| 23 | #include <mtp_level3.h> |
| 24 | #include <cellmgr_debug.h> |
Holger Hans Peter Freyther | 4c1eb0e | 2011-01-22 15:52:07 +0100 | [diff] [blame] | 25 | #include <counter.h> |
Holger Hans Peter Freyther | a8ce061 | 2011-01-20 16:30:24 +0100 | [diff] [blame] | 26 | |
| 27 | #include <string.h> |
| 28 | |
| 29 | static struct msgb *mtp_create_sltm(struct mtp_link *link) |
| 30 | { |
| 31 | const uint8_t test_ptrn[14] = { 'G', 'S', 'M', 'M', 'M', 'S', }; |
| 32 | struct mtp_level_3_hdr *hdr; |
| 33 | struct mtp_level_3_mng *mng; |
| 34 | struct msgb *msg = mtp_msg_alloc(link->set); |
| 35 | uint8_t *data; |
| 36 | if (!msg) |
| 37 | return NULL; |
| 38 | |
| 39 | hdr = (struct mtp_level_3_hdr *) msg->l2h; |
| 40 | hdr->ser_ind = MTP_SI_MNT_REG_MSG; |
Holger Hans Peter Freyther | 050577a | 2011-01-20 19:28:15 +0100 | [diff] [blame] | 41 | hdr->addr = MTP_ADDR(link->link_no % 16, link->set->dpc, link->set->opc); |
Holger Hans Peter Freyther | a8ce061 | 2011-01-20 16:30:24 +0100 | [diff] [blame] | 42 | |
| 43 | mng = (struct mtp_level_3_mng *) msgb_put(msg, sizeof(*mng)); |
| 44 | mng->cmn.h0 = MTP_TST_MSG_GRP; |
| 45 | mng->cmn.h1 = MTP_TST_MSG_SLTM; |
| 46 | mng->length = ARRAY_SIZE(test_ptrn); |
| 47 | |
| 48 | data = msgb_put(msg, ARRAY_SIZE(test_ptrn)); |
| 49 | memcpy(data, test_ptrn, ARRAY_SIZE(test_ptrn)); |
| 50 | |
| 51 | /* remember the last tst ptrn... once we have some */ |
| 52 | memcpy(link->test_ptrn, test_ptrn, ARRAY_SIZE(test_ptrn)); |
| 53 | |
| 54 | return msg; |
| 55 | } |
| 56 | |
| 57 | static void mtp_send_sltm(struct mtp_link *link) |
| 58 | { |
| 59 | struct msgb *msg; |
| 60 | |
| 61 | link->sltm_pending = 1; |
| 62 | msg = mtp_create_sltm(link); |
| 63 | if (!msg) { |
| 64 | LOGP(DINP, LOGL_ERROR, "Failed to allocate SLTM.\n"); |
| 65 | return; |
| 66 | } |
| 67 | |
Holger Hans Peter Freyther | 9543f4a | 2011-01-24 20:49:58 +0100 | [diff] [blame] | 68 | mtp_link_submit(link, msg); |
Holger Hans Peter Freyther | a8ce061 | 2011-01-20 16:30:24 +0100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static void mtp_sltm_t1_timeout(void *_link) |
| 72 | { |
| 73 | struct mtp_link *link = (struct mtp_link *) _link; |
| 74 | |
Holger Hans Peter Freyther | 4c1eb0e | 2011-01-22 15:52:07 +0100 | [diff] [blame] | 75 | rate_ctr_inc(&link->ctrg->ctr[MTP_LNK_SLTM_TOUT]); |
| 76 | |
Holger Hans Peter Freyther | a8ce061 | 2011-01-20 16:30:24 +0100 | [diff] [blame] | 77 | if (link->slta_misses == 0) { |
Holger Hans Peter Freyther | 38d936a | 2011-01-26 12:41:42 +0100 | [diff] [blame^] | 78 | LOGP(DINP, LOGL_ERROR, |
| 79 | "No SLTM response. Retrying. Link: %s/%d\n", |
| 80 | link->set->name, link->link_no); |
Holger Hans Peter Freyther | a8ce061 | 2011-01-20 16:30:24 +0100 | [diff] [blame] | 81 | ++link->slta_misses; |
| 82 | mtp_send_sltm(link); |
| 83 | bsc_schedule_timer(&link->t1_timer, MTP_T1); |
| 84 | } else { |
Holger Hans Peter Freyther | 38d936a | 2011-01-26 12:41:42 +0100 | [diff] [blame^] | 85 | LOGP(DINP, LOGL_ERROR, |
| 86 | "Two missing SLTAs. Restart link: %s/%d\n", |
| 87 | link->set->name, link->link_no); |
Holger Hans Peter Freyther | a8ce061 | 2011-01-20 16:30:24 +0100 | [diff] [blame] | 88 | bsc_del_timer(&link->t2_timer); |
Holger Hans Peter Freyther | fa8cf2d | 2011-01-20 16:51:34 +0100 | [diff] [blame] | 89 | mtp_link_failure(link); |
Holger Hans Peter Freyther | a8ce061 | 2011-01-20 16:30:24 +0100 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | static void mtp_sltm_t2_timeout(void *_link) |
| 94 | { |
| 95 | struct mtp_link *link = (struct mtp_link *) _link; |
| 96 | |
| 97 | if (!link->set->running) { |
Holger Hans Peter Freyther | 38d936a | 2011-01-26 12:41:42 +0100 | [diff] [blame^] | 98 | LOGP(DINP, LOGL_INFO, |
| 99 | "The linkset is not active. Stopping SLTM now. %s/%d\n", |
| 100 | link->set->name, link->link_no); |
Holger Hans Peter Freyther | a8ce061 | 2011-01-20 16:30:24 +0100 | [diff] [blame] | 101 | return; |
| 102 | } |
| 103 | |
| 104 | link->slta_misses = 0; |
| 105 | mtp_send_sltm(link); |
| 106 | |
| 107 | bsc_schedule_timer(&link->t1_timer, MTP_T1); |
| 108 | |
| 109 | if (link->set->sltm_once && link->was_up) |
| 110 | LOGP(DINP, LOGL_INFO, "Not sending SLTM again as configured.\n"); |
| 111 | else |
| 112 | bsc_schedule_timer(&link->t2_timer, MTP_T2); |
| 113 | } |
| 114 | |
Holger Hans Peter Freyther | 4c1eb0e | 2011-01-22 15:52:07 +0100 | [diff] [blame] | 115 | int mtp_link_init(struct mtp_link *link) |
Holger Hans Peter Freyther | a8ce061 | 2011-01-20 16:30:24 +0100 | [diff] [blame] | 116 | { |
Holger Hans Peter Freyther | 4c1eb0e | 2011-01-22 15:52:07 +0100 | [diff] [blame] | 117 | link->ctrg = rate_ctr_group_alloc(link, |
| 118 | mtp_link_rate_ctr_desc(), link->link_no); |
| 119 | if (!link->ctrg) { |
| 120 | LOGP(DINP, LOGL_ERROR, "Failed to allocate rate_ctr.\n"); |
| 121 | return -1; |
| 122 | } |
| 123 | |
Holger Hans Peter Freyther | a8ce061 | 2011-01-20 16:30:24 +0100 | [diff] [blame] | 124 | link->t1_timer.data = link; |
| 125 | link->t1_timer.cb = mtp_sltm_t1_timeout; |
| 126 | link->t2_timer.data = link; |
| 127 | link->t2_timer.cb = mtp_sltm_t2_timeout; |
Holger Hans Peter Freyther | 4c1eb0e | 2011-01-22 15:52:07 +0100 | [diff] [blame] | 128 | return 0; |
Holger Hans Peter Freyther | a8ce061 | 2011-01-20 16:30:24 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void mtp_link_stop_link_test(struct mtp_link *link) |
| 132 | { |
| 133 | bsc_del_timer(&link->t1_timer); |
| 134 | bsc_del_timer(&link->t2_timer); |
| 135 | |
| 136 | link->sltm_pending = 0; |
| 137 | } |
| 138 | |
| 139 | void mtp_link_start_link_test(struct mtp_link *link) |
| 140 | { |
| 141 | mtp_sltm_t2_timeout(link); |
| 142 | } |
| 143 | |
| 144 | int mtp_link_slta(struct mtp_link *link, uint16_t l3_len, |
| 145 | struct mtp_level_3_mng *mng) |
| 146 | { |
| 147 | if (mng->length != 14) { |
| 148 | LOGP(DINP, LOGL_ERROR, "Wrongly sized SLTA: %u\n", mng->length); |
| 149 | return -1; |
| 150 | } |
| 151 | |
| 152 | if (l3_len != 16) { |
| 153 | LOGP(DINP, LOGL_ERROR, "Wrongly sized SLTA: %u\n", mng->length); |
| 154 | return -1; |
| 155 | } |
| 156 | |
| 157 | if (memcmp(mng->data, link->test_ptrn, sizeof(link->test_ptrn)) != 0) { |
| 158 | LOGP(DINP, LOGL_ERROR, "Wrong test pattern SLTA\n"); |
| 159 | return -1; |
| 160 | } |
| 161 | |
| 162 | /* we had a matching slta */ |
| 163 | bsc_del_timer(&link->t1_timer); |
| 164 | link->sltm_pending = 0; |
| 165 | link->was_up = 1; |
| 166 | |
| 167 | return 0; |
| 168 | } |
Holger Hans Peter Freyther | fa8cf2d | 2011-01-20 16:51:34 +0100 | [diff] [blame] | 169 | |
| 170 | void mtp_link_failure(struct mtp_link *link) |
| 171 | { |
Holger Hans Peter Freyther | 38d936a | 2011-01-26 12:41:42 +0100 | [diff] [blame^] | 172 | LOGP(DINP, LOGL_ERROR, "Link has failed. Resetting it: %s/%d\n", |
| 173 | link->set->name, link->link_no); |
Holger Hans Peter Freyther | 4c1eb0e | 2011-01-22 15:52:07 +0100 | [diff] [blame] | 174 | rate_ctr_inc(&link->ctrg->ctr[MTP_LNK_ERROR]); |
Holger Hans Peter Freyther | fa8cf2d | 2011-01-20 16:51:34 +0100 | [diff] [blame] | 175 | link->reset(link); |
| 176 | } |
Holger Hans Peter Freyther | ea5ce23 | 2011-01-23 23:31:26 +0100 | [diff] [blame] | 177 | |
| 178 | void mtp_link_block(struct mtp_link *link) |
| 179 | { |
| 180 | link->blocked = 1; |
| 181 | link->shutdown(link); |
| 182 | } |
| 183 | |
| 184 | void mtp_link_unblock(struct mtp_link *link) |
| 185 | { |
| 186 | if (!link->blocked) |
| 187 | return; |
| 188 | link->blocked = 0; |
| 189 | link->reset(link); |
| 190 | } |