<commit_before># Sequreisp - Copyright 2010, 2011 <NAME>
#
# This file is part of Sequreisp.
#
# Sequreisp is free software: you can redistribute it and/or modify
# it under the terms of the GNU Afero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Sequreisp is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Afero General Public License for more details.
#
# You should have received a copy of the GNU Afero General Public License
# along with Sequreisp.  If not, see <http://www.gnu.org/licenses/>.

#PPP_PEER_DIR="/etc/ppp/peer"
BASE=SequreispConfig::CONFIG["base_dir"]
PPP_DIR=SequreispConfig::CONFIG["ppp_dir"]
DHCPD_DIR=SequreispConfig::CONFIG["dhcpd_dir"]
PINGABLE_SERVERS=SequreispConfig::CONFIG["pingable_servers"]
IFB_UP=SequreispConfig::CONFIG["ifb_up"]
IFB_DOWN=SequreispConfig::CONFIG["ifb_down"]
DEPLOY_DIR=SequreispConfig::CONFIG["deploy_dir"]
BASE_SCRIPTS="#{BASE}/scripts"
SEQUREISP_SQUID_CONF=Rails.env.production? ? "/etc/squid/sequreisp.squid.conf" : "/tmp/sequreisp/squid.conf"
TC_FILE_PREFIX="#{BASE_SCRIPTS}/tc_"
IP_FILE_PREFIX="#{BASE_SCRIPTS}/ip_"
PROVIDER_UP_FILE_PREFIX= "#{BASE_SCRIPTS}/provider_up_"
PROVIDER_DOWN_FILE_PREFIX= "#{BASE_SCRIPTS}/provider_down_"
ARP_FILE="#{BASE_SCRIPTS}/arp"
IP_RU_FILE="#{BASE_SCRIPTS}/ip_ru"
IP_RO_FILE="#{BASE_SCRIPTS}/ip_ro"
IPTABLES_FILE="#{BASE_SCRIPTS}/iptables"
IPTABLES_PRE_FILE="#{BASE}/etc/iptables_pre.sh"
IPTABLES_POST_FILE="#{BASE}/etc/iptables_post.sh"
SEQUREISP_PRE_FILE="#{BASE}/etc/sequreisp_pre.sh"
SEQUREISP_POST_FILE="#{BASE}/etc/sequreisp_post.sh"
BOOT_FILE="#{BASE_SCRIPTS}/boot.sh"
QUEUED_COMMANDS_FILE="#{BASE_SCRIPTS}/queued_commands.sh"
BOOT_LOG="#{BOOT_FILE}.log"
CHECK_LINKS_FILE="#{BASE_SCRIPTS}/check_links.sh"
CHECK_LINKS_LOG="#{CHECK_LINKS_FILE}.log"

def create_dirs_if_not_present
  [BASE_SCRIPTS, DHCPD_DIR, PPP_DIR, DEPLOY_DIR, "#{PPP_DIR}/ip-up.d", "#{PPP_DIR}/ip-down.d", "#{DHCPD_DIR}/dhclient-enter-hooks.d",  "#{DHCPD_DIR}/dhclient-exit-hooks.d", "#{PPP_DIR}/peers"].each do |dir|
    dir.split("/").inject do |path, dir|
      new_dir = "#{path}/#{dir}"
      Dir.mkdir(new_dir) if not File.exist? new_dir
      new_dir
    end
  end
end

def gen_tc(f)
  def tc_class_qdisc_filter(o = {})
    classid = "#{o[:parent_mayor]}:#{o[:current_minor]}"
    tc = o[:file]
    tc.puts "class add dev #{o[:iface]} parent #{o[:parent_mayor]}:#{o[:parent_minor]} classid #{classid} " +
            "htb rate #{o[:rate]}kbit ceil #{o[:ceil]}kbit prio #{o[:prio]} quantum #{o[:quantum]}"
    tc.puts "qdisc add dev #{o[:iface]} parent #{classid} sfq perturb 10" #saco el handle
    tc.puts "filter add dev #{o[:iface]} parent #{o[:parent_mayor]}: protocol all prio 200 handle 0x#{o[:mark]}/0x#{o[:mask]} fw classid #{classid}"
  end
  def do_global_prios_tc(file, iface, parent_mayor, parent_minor, rate, quantum)
    mask = "f0000000"
    #TODO tc_global ceil_prio3 quantum mark, etc
    #prio1
    tc_class_qdisc_filter :file => file, :iface => iface, :parent_mayor => parent_mayor, :parent_minor => parent_minor, :current_minor => "a",
                          :rate => rate * 0.2 , :ceil => rate , :prio => 1, :quantum => quantum, :mark => "a0000000", :mask => mask
    #prio2
    tc_class_qdisc_filter :file => file, :iface => iface, :parent_mayor => parent_mayor, :parent_minor => parent_minor, :current_minor => "b",
                          :rate => rate * 0.7 , :ceil => rate , :prio => 2, :quantum => quantum, :mark => "b0000000", :mask => mask
    #prio3
    tc_class_qdisc_filter :file => file, :iface => iface, :parent_mayor => parent_mayor, :parent_minor => parent_minor, :current_minor => "c",
                          :rate => rate * 0.1 , :ceil => rate * 0.3 , :prio => 3, :quantum => quantum / 3, :mark => "c0000000", :mask => mask
  end
  def do_per_contract_prios_tc(tc, plan, c, parent_mayor, parent_minor, iface, direction, prefix=0)
    contract_min_rate = 0.024<commit_msg>More bw to the prio1 class<commit_after># Sequreisp - Copyright 2010, 2011 <NAME>
#
# This file is part of Sequreisp.
#
# Sequreisp is free software: you can redistribute it and/or modify
# it under the terms of the GNU Afero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Sequreisp is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Afero General Public License for more details.
#
# You should have received a copy of the GNU Afero General Public License
# along with Sequreisp.  If not, see <http://www.gnu.org/licenses/>.

#PPP_PEER_DIR="/etc/ppp/peer"
BASE=SequreispConfig::CONFIG["base_dir"]
PPP_DIR=SequreispConfig::CONFIG["ppp_dir"]
DHCPD_DIR=SequreispConfig::CONFIG["dhcpd_dir"]
PINGABLE_SERVERS=SequreispConfig::CONFIG["pingable_servers"]
IFB_UP=SequreispConfig::CONFIG["ifb_up"]
IFB_DOWN=SequreispConfig::CONFIG["ifb_down"]
DEPLOY_DIR=SequreispConfig::CONFIG["deploy_dir"]
BASE_SCRIPTS="#{BASE}/scripts"
SEQUREISP_SQUID_CONF=Rails.env.production? ? "/etc/squid/sequreisp.squid.conf" : "/tmp/sequreisp/squid.conf"
TC_FILE_PREFIX="#{BASE_SCRIPTS}/tc_"
IP_FILE_PREFIX="#{BASE_SCRIPTS}/ip_"
PROVIDER_UP_FILE_PREFIX= "#{BASE_SCRIPTS}/provider_up_"
PROVIDER_DOWN_FILE_PREFIX= "#{BASE_SCRIPTS}/provider_down_"
ARP_FILE="#{BASE_SCRIPTS}/arp"
IP_RU_FILE="#{BASE_SCRIPTS}/ip_ru"
IP_RO_FILE="#{BASE_SCRIPTS}/ip_ro"
IPTABLES_FILE="#{BASE_SCRIPTS}/iptables"
IPTABLES_PRE_FILE="#{BASE}/etc/iptables_pre.sh"
IPTABLES_POST_FILE="#{BASE}/etc/iptables_post.sh"
SEQUREISP_PRE_FILE="#{BASE}/etc/sequreisp_pre.sh"
SEQUREISP_POST_FILE="#{BASE}/etc/sequreisp_post.sh"
BOOT_FILE="#{BASE_SCRIPTS}/boot.sh"
QUEUED_COMMANDS_FILE="#{BASE_SCRIPTS}/queued_commands.sh"
BOOT_LOG="#{BOOT_FILE}.log"
CHECK_LINKS_FILE="#{BASE_SCRIPTS}/check_links.sh"
CHECK_LINKS_LOG="#{CHECK_LINKS_FILE}.log"

def create_dirs_if_not_present
  [BASE_SCRIPTS, DHCPD_DIR, PPP_DIR, DEPLOY_DIR, "#{PPP_DIR}/ip-up.d", "#{PPP_DIR}/ip-down.d", "#{DHCPD_DIR}/dhclient-enter-hooks.d",  "#{DHCPD_DIR}/dhclient-exit-hooks.d", "#{PPP_DIR}/peers"].each do |dir|
    dir.split("/").inject do |path, dir|
      new_dir = "#{path}/#{dir}"
      Dir.mkdir(new_dir) if not File.exist? new_dir
      new_dir
    end
  end
end

def gen_tc(f)
  def tc_class_qdisc_filter(o = {})
    classid = "#{o[:parent_mayor]}:#{o[:current_minor]}"
    tc = o[:file]
    tc.puts "class add dev #{o[:iface]} parent #{o[:parent_mayor]}:#{o[:parent_minor]} classid #{classid} " +
            "htb rate #{o[:rate]}kbit ceil #{o[:ceil]}kbit prio #{o[:prio]} quantum #{o[:quantum]}"
    tc.puts "qdisc add dev #{o[:iface]} parent #{classid} sfq perturb 10" #saco el handle
    tc.puts "filter add dev #{o[:iface]} parent #{o[:parent_mayor]}: protocol all prio 200 handle 0x#{o[:mark]}/0x#{o[:mask]} fw classid #{classid}"
  end
  def do_global_prios_tc(file, iface, parent_mayor, parent_minor, rate, quantum)
    mask = "f0000000"
    #TODO tc_global ceil_prio3 quantum mark, etc
    #prio1
    tc_class_qdisc_filter :file => file, :iface => iface, :parent_mayor => parent_mayor, :parent_minor => parent_minor, :current_minor => "a",
                          :rate => rate * 0.4 , :ceil => rate , :prio => 1, :quantum => quantum, :mark => "a0000000", :mask => mask
    #prio2
    tc_class_qdisc_filter :file => file, :iface => iface, :parent_mayor => parent_mayor, :parent_minor => parent_minor, :current_minor => "b",
                          :rate => rate * 0.5 , :ceil => rate , :prio => 2, :quantum => quantum, :mark => "b0000000", :mask => mask
    #prio3
    tc_class_qdisc_filter :file => file, :iface => iface, :parent_mayor => parent_mayor, :parent_minor => parent_minor, :current_minor => "c",
                          :rate => rate * 0.1 , :ceil => rate * 0.3 , :prio => 3, :quantum => quantum / 3, :mark => "c0000000", :mask => mask
  end
  def do_per_contract_prios_tc(tc, plan, c, parent_mayor, parent_minor, iface, direction, prefix=0)
    contract_min_rate = 0.024
{'fraction_non_alphanumeric': 0.1543486149752026, 'fraction_numerical': 0.0158461352364824, 'mean_word_length': 5.043859649122807, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 8, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4285785', 'n_tokens_mistral': 3229, 'n_tokens_neox': 3094, 'n_words': 664}