summaryrefslogtreecommitdiff
path: root/luci2/htdocs/luci2/view/network.switch.js
blob: 39a5b0bfe02f091e323c1c6f1e7af81a55311caa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
L.ui.view.extend({
	title: L.tr('Switch'),
	description: L.tr('The network ports on this device can be combined to several VLANs in which computers can communicate directly with each other. VLANs are often used to separate different network segments. Often there is by default one Uplink port for a connection to the next greater network like the internet and other ports for a local network.'),

	listSwitchNames: L.rpc.declare({
		object: 'luci2.network',
		method: 'switch_list',
		expect: { switches: [ ] }
	}),

	getSwitchInfo: L.rpc.declare({
		object: 'luci2.network',
		method: 'switch_info',
		params: [ 'switch' ],
		expect: { info: { } },
		filter: function(data, params) {
			data['attrs']      = data['switch'];
			data['vlan_attrs'] = data['vlan'];
			data['port_attrs'] = data['port'];
			data['switch']     = params['switch'];

			delete data.vlan;
			delete data.port;

			return data;
		}
	}),

	getSwitchStatus: L.rpc.declare({
		object: 'luci2.network',
		method: 'switch_status',
		params: [ 'switch' ],
		expect: { ports: [ ] }
	}),

	switchPortState: L.cbi.ListValue.extend({
		choices: [
			[ 'n', L.trc('Switch port state', 'off')      ],
			[ 'u', L.trc('Switch port state', 'untagged') ],
			[ 't', L.trc('Switch port state', 'tagged')   ]
		],

		init: function(name, options)
		{
			var self = this;

			options.datatype = function(val, elem)
			{
				if (val == 'u')
				{
					var u = false;
					var sections = self.section.sections();

					for (var i = 0; i < sections.length; i++)
					{
						var v = self.formvalue(sections[i]['.name']);
						if (v == 'u')
						{
							if (u)
								return L.tr('Port must not be untagged in multiple VLANs');

							u = true;
						}
					}
				}

				return true;
			};

			this.callSuper('init', name, options);
		},

		ucivalue: function(sid)
		{
			var ports = (this.map.get('network', sid, 'ports') || '').match(/[0-9]+[tu]?/g);

			if (ports)
				for (var i = 0; i < ports.length; i++)
					if (ports[i].match(/^([0-9]+)([tu]?)$/))
						if (RegExp.$1 == this.name)
							return RegExp.$2 || 'u';

			return 'n';
		},

		save: function(sid)
		{
			return;
		}
	}),

	execute: function() {
		var self = this;
		return self.listSwitchNames().then(function(switches) {
			L.rpc.batch();

			for (var i = 0; i < switches.length; i++)
				self.getSwitchInfo(switches[i]);

			return L.rpc.flush();
		}).then(function(switches) {
			var m = new L.cbi.Map('network', {
				readonly:    !self.options.acls.network
			});

			for (var i = 0; i < switches.length; i++)
			{
				var swname    = switches[i]['switch'];

				var vid_opt   = 'vlan';
				var v4k_opt   = undefined;
				var pvid_opt  = undefined;
				var max_vid   = switches[i].num_vlans - 1;
				var num_vlans = switches[i].num_vlans;

				for (var j = 0; j < switches[i].vlan_attrs.length; j++)
				{
					switch (switches[i].vlan_attrs[j].name)
					{
					case 'tag':
					case 'vid':
					case 'pvid':
						vid_opt = switches[i].vlan_attrs[j].name;
						max_vid = 4095;
						break;
					}
				}

				for (var j = 0; j < switches[i].port_attrs.length; j++)
				{
					switch (switches[i].port_attrs[j].name)
					{
					case 'pvid':
						pvid_opt = switches[i].port_attrs[j].name;
						break;
					}
				}


				var sw = m.section(L.cbi.TypedSection, 'switch', {
					caption:  L.tr('Switch "%s"').format(switches[i].model),
					swname:   swname
				});

				sw.filter = function(section) {
					return (section['.name'] == this.options.swname ||
							section.name     == this.options.swname);
				};

				for (var j = 0; j < switches[i].attrs.length; j++)
				{
					switch (switches[i].attrs[j].name)
					{
					case 'enable_vlan':
						sw.option(L.cbi.CheckboxValue, 'enable_vlan', {
							caption:     L.tr('Enable VLAN functionality')
						});
						break;

					case 'enable_learning':
						sw.option(L.cbi.CheckboxValue, 'enable_learning', {
							caption:     L.tr('Enable learning and aging'),
							initial:     true,
							optional:    true
						});
						break;

					case 'max_length':
						sw.option(L.cbi.CheckboxValue, 'max_length', {
							caption:     L.tr('Enable Jumbo Frame passthrough'),
							enabled:     '3',
							optional:    true
						});
						break;

					case 'enable_vlan4k':
						v4k_opt = switches[i].attrs[j].name;
						break;
					}
				}

				var vlans = m.section(L.cbi.TableSection, 'switch_vlan', {
					caption:     L.tr('VLANs on "%s"').format(switches[i].model),
					swname:      swname,
					addremove:   true,
					add_caption: L.tr('Add VLAN entry …')
				});

				vlans.add = function() {
					var sections = this.sections();
					var used_vids = { };

					for (var j = 0; j < sections.length; j++)
					{
						var v = this.map.get('network', sections[j]['.name'], 'vlan');
						if (v)
							used_vids[v] = true;
					}

					for (var j = 1; j < num_vlans; j++)
					{
						if (used_vids[j.toString()])
							continue;

						var sid = this.map.add('network', 'switch_vlan');
						this.map.set('network', sid, 'device', this.options.swname);
						this.map.set('network', sid, 'vlan', j);
						break;
					}
				};

				vlans.filter = function(section) {
					return (section.device == this.options.swname);
				};

				vlans.sections = function() {
					var s = this.callSuper('sections');

					s.sort(function(a, b) {
						var x = parseInt(a[vid_opt] || a.vlan);
						if (isNaN(x))
							x = 9999;

						var y = parseInt(b[vid_opt] || b.vlan);
						if (isNaN(y))
							y = 9999;

						return (x - y);
					});

					return s;
				};

				var port_opts = [ ];

				var vo = vlans.option(L.cbi.InputValue, vid_opt, {
					caption:     L.tr('VLAN ID'),
					datatype:    function(val) {
						var sections = vlans.sections();
						var used_vids = { };

						for (var j = 0; j < sections.length; j++)
						{
							var v = vlans.fields[vid_opt].formvalue(sections[j]['.name']);
							if (!v)
								continue;

							if (used_vids[v])
								return L.tr('VLAN ID must be unique');

							used_vids[v] = true;
						}

						if (val.match(/[^0-9]/))
							return L.tr('Invalid VLAN ID');

						val = parseInt(val, 10);

						if (val < 1 || val > max_vid)
							return L.tr('VLAN ID must be a value between %u and %u').format(1, max_vid);

						return true;
					}
				});

				vo.ucivalue = function(sid) {
					var id = this.map.get('network', sid, vid_opt);

					if (isNaN(parseInt(id)))
						id = this.map.get('network', sid, 'vlan');

					return id;
				};

				vo.save = function(sid) {
					var old_ports = this.map.get('network', sid, 'ports');
					var new_ports = '';

					for (var j = 0; j < port_opts.length; j++)
					{
						var v = port_opts[j].formvalue(sid);
						if (v != 'n')
							new_ports += '%s%d%s'.format(
								new_ports ? ' ' : '', j,
								(v == 'u') ? '' : 't');
					}

					if (new_ports != old_ports)
						this.map.set('network', sid, 'ports', new_ports);

					if (v4k_opt)
					{
						var s = sw.sections();
						for (var j = 0; j < s.length; j++)
							this.map.set('network', s[j]['.name'], v4k_opt, '1');
					}

					this.callSuper('save', sid);
				};

				for (var j = 0; j < switches[i].num_ports; j++)
				{
					var label = L.trc('Switch port label', 'Port %d').format(j);

					if (j == switches[i].cpu_port)
						label = L.trc('Switch port label', 'CPU');

					var po = vlans.option(self.switchPortState, j.toString(), {
						caption: label + '<br /><small id="portstatus-%s-%d"></small>'.format(swname, j)
					});

					port_opts.push(po);
				}
			}

			return m.insertInto('#map').then(function() {
				self.repeat(function() {
					return self.getSwitchStatus(swname).then(function(ports) {
						for (var j = 0; j < ports.length; j++)
						{
							var s = L.tr('No link');
							var d = '&#160;';

							if (ports[j].link)
							{
								s = '%dbaseT'.format(ports[j].speed);
								d = ports[j].full_duplex ? L.tr('Full-duplex') : L.tr('Half-duplex');
							}

							$('#portstatus-%s-%d'.format(swname, j))
								.empty().append(s + '<br />' + d);
						}
					});
				}, 5000);
			});
		});
	}
});