|
| 1 | +#!/usr/bin/lua |
| 2 | + |
| 3 | +-- Run this after 500-mesh-vpn! |
| 4 | + |
| 5 | +local uci = require('simple-uci').cursor() |
| 6 | + |
| 7 | +local site = require 'gluon.site' |
| 8 | +local vpn_core = require 'gluon.mesh-vpn' |
| 9 | + |
| 10 | +local function get_mem_total() |
| 11 | + for line in io.lines('/proc/meminfo') do |
| 12 | + local match = line:match('^MemTotal:%s+(%d+)') |
| 13 | + if match then |
| 14 | + return tonumber(match) |
| 15 | + end |
| 16 | + end |
| 17 | +end |
| 18 | + |
| 19 | +local function configure_sqm() |
| 20 | + local limit_enabled = uci:get_bool('gluon', 'mesh_vpn', 'limit_enabled') |
| 21 | + local limit_downstream = uci:get('gluon', 'mesh_vpn', 'limit_ingress') |
| 22 | + local limit_upstream = uci:get('gluon', 'mesh_vpn', 'limit_egress') |
| 23 | + |
| 24 | + -- Only enable if we have sufficient RAM (256MB or more) |
| 25 | + if get_mem_total() < 240*1024 then |
| 26 | + return |
| 27 | + end |
| 28 | + |
| 29 | + -- Only enable if we actually have a limit configured |
| 30 | + if not limit_enabled then |
| 31 | + return |
| 32 | + end |
| 33 | + |
| 34 | + -- Disable simple-tc |
| 35 | + uci:set('simple-tc', 'mesh_vpn', 'enabled', '0') |
| 36 | + |
| 37 | + -- Create SQM configuration |
| 38 | + uci:section('sqm', 'queue', 'mesh_vpn', { |
| 39 | + interface = vpn_core.get_interface(), |
| 40 | + enabled = true, |
| 41 | + upload = limit_upstream, |
| 42 | + download = limit_downstream, |
| 43 | + qdisc = 'cake', |
| 44 | + script = 'piece_of_cake.qos', |
| 45 | + debug_logging = '0', |
| 46 | + verbosity = '5', |
| 47 | + }) |
| 48 | +end |
| 49 | + |
| 50 | +-- Ensure existing SQM settings are cleared |
| 51 | +uci:delete('sqm', 'mesh_vpn') |
| 52 | + |
| 53 | +-- Enable SQM if configured in site.conf or user-enabled in uci |
| 54 | +local site_state = site.mesh_vpn.fastd.sqm() |
| 55 | +if uci:get_bool('ffda-mesh-vpn-sqm', 'settings', 'enabled') or site_state == true then |
| 56 | + configure_sqm() |
| 57 | +end |
| 58 | + |
| 59 | +uci:save('simple-tc') |
| 60 | +uci:save('sqm') |
0 commit comments