blob: 009547c35f557454b1e7c802f609b68f145dc795 [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) {
Holger Hans Peter Freytherd3f412b2011-01-28 18:52:16 +010064 LOGP(DINP, LOGL_ERROR, "Failed to allocate SLTM on %s/%d.\n",
65 link->set->name, link->link_no);
Holger Hans Peter Freythera8ce0612011-01-20 16:30:24 +010066 return;
67 }
68
Holger Hans Peter Freyther9543f4a2011-01-24 20:49:58 +010069 mtp_link_submit(link, msg);
Holger Hans Peter Freythera8ce0612011-01-20 16:30:24 +010070}
71
72static void mtp_sltm_t1_timeout(void *_link)
73{
74 struct mtp_link *link = (struct mtp_link *) _link;
75
Holger Hans Peter Freyther4c1eb0e2011-01-22 15:52:07 +010076 rate_ctr_inc(&link->ctrg->ctr[MTP_LNK_SLTM_TOUT]);
77
Holger Hans Peter Freythera8ce0612011-01-20 16:30:24 +010078 if (link->slta_misses == 0) {
Holger Hans Peter Freyther38d936a2011-01-26 12:41:42 +010079 LOGP(DINP, LOGL_ERROR,
Holger Hans Peter Freytherd3f412b2011-01-28 18:52:16 +010080 "No SLTM response on link %s/%d.\n",
Holger Hans Peter Freyther38d936a2011-01-26 12:41:42 +010081 link->set->name, link->link_no);
Holger Hans Peter Freythera8ce0612011-01-20 16:30:24 +010082 ++link->slta_misses;
83 mtp_send_sltm(link);
84 bsc_schedule_timer(&link->t1_timer, MTP_T1);
85 } else {
Holger Hans Peter Freyther38d936a2011-01-26 12:41:42 +010086 LOGP(DINP, LOGL_ERROR,
Holger Hans Peter Freytherd3f412b2011-01-28 18:52:16 +010087 "Two missing SLTAs on link %s/%d.\n",
Holger Hans Peter Freyther38d936a2011-01-26 12:41:42 +010088 link->set->name, link->link_no);
Holger Hans Peter Freythera8ce0612011-01-20 16:30:24 +010089 bsc_del_timer(&link->t2_timer);
Holger Hans Peter Freytherfa8cf2d2011-01-20 16:51:34 +010090 mtp_link_failure(link);
Holger Hans Peter Freythera8ce0612011-01-20 16:30:24 +010091 }
92}
93
94static void mtp_sltm_t2_timeout(void *_link)
95{
96 struct mtp_link *link = (struct mtp_link *) _link;
97
98 if (!link->set->running) {
Holger Hans Peter Freyther38d936a2011-01-26 12:41:42 +010099 LOGP(DINP, LOGL_INFO,
Holger Hans Peter Freytherd3f412b2011-01-28 18:52:16 +0100100 "The linkset is not active. Stopping the link test on %s/%d.\n",
Holger Hans Peter Freyther38d936a2011-01-26 12:41:42 +0100101 link->set->name, link->link_no);
Holger Hans Peter Freythera8ce0612011-01-20 16:30:24 +0100102 return;
103 }
104
105 link->slta_misses = 0;
106 mtp_send_sltm(link);
107
108 bsc_schedule_timer(&link->t1_timer, MTP_T1);
109
110 if (link->set->sltm_once && link->was_up)
Holger Hans Peter Freytherd3f412b2011-01-28 18:52:16 +0100111 LOGP(DINP, LOGL_INFO, "Not sending SLTM again as configured on %s/%d.\n",
112 link->set->name, link->link_no);
Holger Hans Peter Freythera8ce0612011-01-20 16:30:24 +0100113 else
114 bsc_schedule_timer(&link->t2_timer, MTP_T2);
115}
116
Holger Hans Peter Freyther4c1eb0e2011-01-22 15:52:07 +0100117int mtp_link_init(struct mtp_link *link)
Holger Hans Peter Freythera8ce0612011-01-20 16:30:24 +0100118{
Holger Hans Peter Freyther4c1eb0e2011-01-22 15:52:07 +0100119 link->ctrg = rate_ctr_group_alloc(link,
120 mtp_link_rate_ctr_desc(), link->link_no);
121 if (!link->ctrg) {
122 LOGP(DINP, LOGL_ERROR, "Failed to allocate rate_ctr.\n");
123 return -1;
124 }
125
Holger Hans Peter Freythera8ce0612011-01-20 16:30:24 +0100126 link->t1_timer.data = link;
127 link->t1_timer.cb = mtp_sltm_t1_timeout;
128 link->t2_timer.data = link;
129 link->t2_timer.cb = mtp_sltm_t2_timeout;
Holger Hans Peter Freyther4c1eb0e2011-01-22 15:52:07 +0100130 return 0;
Holger Hans Peter Freythera8ce0612011-01-20 16:30:24 +0100131}
132
133void mtp_link_stop_link_test(struct mtp_link *link)
134{
135 bsc_del_timer(&link->t1_timer);
136 bsc_del_timer(&link->t2_timer);
137
138 link->sltm_pending = 0;
139}
140
141void mtp_link_start_link_test(struct mtp_link *link)
142{
143 mtp_sltm_t2_timeout(link);
144}
145
146int mtp_link_slta(struct mtp_link *link, uint16_t l3_len,
147 struct mtp_level_3_mng *mng)
148{
149 if (mng->length != 14) {
150 LOGP(DINP, LOGL_ERROR, "Wrongly sized SLTA: %u\n", mng->length);
151 return -1;
152 }
153
154 if (l3_len != 16) {
155 LOGP(DINP, LOGL_ERROR, "Wrongly sized SLTA: %u\n", mng->length);
156 return -1;
157 }
158
159 if (memcmp(mng->data, link->test_ptrn, sizeof(link->test_ptrn)) != 0) {
160 LOGP(DINP, LOGL_ERROR, "Wrong test pattern SLTA\n");
161 return -1;
162 }
163
164 /* we had a matching slta */
165 bsc_del_timer(&link->t1_timer);
166 link->sltm_pending = 0;
167 link->was_up = 1;
168
169 return 0;
170}
Holger Hans Peter Freytherfa8cf2d2011-01-20 16:51:34 +0100171
172void mtp_link_failure(struct mtp_link *link)
173{
Holger Hans Peter Freyther309d79f2011-01-28 18:26:20 +0100174 if (link->blocked) {
175 LOGP(DINP, LOGL_ERROR, "Ignoring failure on blocked link %s/%d.\n",
176 link->set->name, link->link_no);
177 return;
178 }
179
Holger Hans Peter Freytherd3f412b2011-01-28 18:52:16 +0100180 LOGP(DINP, LOGL_ERROR, "Link %s/%d has failed, going to reset it.\n",
Holger Hans Peter Freyther38d936a2011-01-26 12:41:42 +0100181 link->set->name, link->link_no);
Holger Hans Peter Freyther4c1eb0e2011-01-22 15:52:07 +0100182 rate_ctr_inc(&link->ctrg->ctr[MTP_LNK_ERROR]);
Holger Hans Peter Freytherfa8cf2d2011-01-20 16:51:34 +0100183 link->reset(link);
184}
Holger Hans Peter Freytherea5ce232011-01-23 23:31:26 +0100185
186void mtp_link_block(struct mtp_link *link)
187{
188 link->blocked = 1;
189 link->shutdown(link);
190}
191
192void mtp_link_unblock(struct mtp_link *link)
193{
194 if (!link->blocked)
195 return;
196 link->blocked = 0;
197 link->reset(link);
198}