blob: 6c7b3e1b84d51e3e3171b64ccc1f7cff93c8561c [file] [log] [blame]
Holger Hans Peter Freythera8ce0612011-01-20 16:30:24 +01001/* 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 Freyther4c1eb0e2011-01-22 15:52:07 +010025#include <counter.h>
Holger Hans Peter Freythera8ce0612011-01-20 16:30:24 +010026
27#include <string.h>
28
29static 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 Freyther050577a2011-01-20 19:28:15 +010041 hdr->addr = MTP_ADDR(link->link_no % 16, link->set->dpc, link->set->opc);
Holger Hans Peter Freythera8ce0612011-01-20 16:30:24 +010042
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
57static 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 Freyther9543f4a2011-01-24 20:49:58 +010068 mtp_link_submit(link, msg);
Holger Hans Peter Freythera8ce0612011-01-20 16:30:24 +010069}
70
71static void mtp_sltm_t1_timeout(void *_link)
72{
73 struct mtp_link *link = (struct mtp_link *) _link;
74
Holger Hans Peter Freyther4c1eb0e2011-01-22 15:52:07 +010075 rate_ctr_inc(&link->ctrg->ctr[MTP_LNK_SLTM_TOUT]);
76
Holger Hans Peter Freythera8ce0612011-01-20 16:30:24 +010077 if (link->slta_misses == 0) {
Holger Hans Peter Freyther38d936a2011-01-26 12:41:42 +010078 LOGP(DINP, LOGL_ERROR,
79 "No SLTM response. Retrying. Link: %s/%d\n",
80 link->set->name, link->link_no);
Holger Hans Peter Freythera8ce0612011-01-20 16:30:24 +010081 ++link->slta_misses;
82 mtp_send_sltm(link);
83 bsc_schedule_timer(&link->t1_timer, MTP_T1);
84 } else {
Holger Hans Peter Freyther38d936a2011-01-26 12:41:42 +010085 LOGP(DINP, LOGL_ERROR,
86 "Two missing SLTAs. Restart link: %s/%d\n",
87 link->set->name, link->link_no);
Holger Hans Peter Freythera8ce0612011-01-20 16:30:24 +010088 bsc_del_timer(&link->t2_timer);
Holger Hans Peter Freytherfa8cf2d2011-01-20 16:51:34 +010089 mtp_link_failure(link);
Holger Hans Peter Freythera8ce0612011-01-20 16:30:24 +010090 }
91}
92
93static 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 Freyther38d936a2011-01-26 12:41:42 +010098 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 Freythera8ce0612011-01-20 16:30:24 +0100101 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 Freyther4c1eb0e2011-01-22 15:52:07 +0100115int mtp_link_init(struct mtp_link *link)
Holger Hans Peter Freythera8ce0612011-01-20 16:30:24 +0100116{
Holger Hans Peter Freyther4c1eb0e2011-01-22 15:52:07 +0100117 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 Freythera8ce0612011-01-20 16:30:24 +0100124 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 Freyther4c1eb0e2011-01-22 15:52:07 +0100128 return 0;
Holger Hans Peter Freythera8ce0612011-01-20 16:30:24 +0100129}
130
131void 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
139void mtp_link_start_link_test(struct mtp_link *link)
140{
141 mtp_sltm_t2_timeout(link);
142}
143
144int 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 Freytherfa8cf2d2011-01-20 16:51:34 +0100169
170void mtp_link_failure(struct mtp_link *link)
171{
Holger Hans Peter Freyther38d936a2011-01-26 12:41:42 +0100172 LOGP(DINP, LOGL_ERROR, "Link has failed. Resetting it: %s/%d\n",
173 link->set->name, link->link_no);
Holger Hans Peter Freyther4c1eb0e2011-01-22 15:52:07 +0100174 rate_ctr_inc(&link->ctrg->ctr[MTP_LNK_ERROR]);
Holger Hans Peter Freytherfa8cf2d2011-01-20 16:51:34 +0100175 link->reset(link);
176}
Holger Hans Peter Freytherea5ce232011-01-23 23:31:26 +0100177
178void mtp_link_block(struct mtp_link *link)
179{
180 link->blocked = 1;
181 link->shutdown(link);
182}
183
184void mtp_link_unblock(struct mtp_link *link)
185{
186 if (!link->blocked)
187 return;
188 link->blocked = 0;
189 link->reset(link);
190}