Beispiel #1
0
    def _set_opts(self):
        """Set scheduler-specific options based on heuristics."""
        # Constants
        MCLSHIFT = 11                 # from /usr/src/sys/sys/param.h
        MCLBYTES = 1 << MCLSHIFT      # from /usr/src/sys/sys/param.h
        RM_NS_PER_SEC  = 1000000000   # from pfctl_altq.c
        RM_FILTER_GAIN = 5            # from pfctl_altq.c

        mtu, ifbw = getifmtu(self.ifname)
        pktsize = ((mtu & ~MCLBYTES) if (mtu > MCLBYTES) else mtu)
        maxpktsize = mtu
        if pktsize > maxpktsize:
            pktsize = maxpktsize

        f = 0.0001
        if self.bandwidth:
            f = float(self.bandwidth) / self.ifbandwidth
        if_ns_per_byte = (1.0 / self.ifbandwidth) * RM_NS_PER_SEC * 8
        ns_per_byte = if_ns_per_byte / f
        ptime = pktsize * if_ns_per_byte
        cptime = ptime * (1.0 - f) / f
        maxburst = (4 if (cptime > 10 * 1000000) else 16)
        minburst = (2 if (maxburst >= 2) else maxburst)

        z = 1 <<  RM_FILTER_GAIN
        g = 1.0 - (1.0 / z)
        gton = g ** maxburst
        gtom = g ** (minburst -1)
        maxidle = ((1.0 / f - 1.0) * ((1.0 - gton) / gton))
        maxidle_s = 1.0 - g
        maxidle = ptime * (maxidle if (maxidle > maxidle_s) else maxidle_s)
        offtime = cptime * (1.0 + 1.0 / (1.0 - g) * (1.0 - gtom) / gtom)
        minidle = -(maxpktsize * ns_per_byte)

        maxidle = ((maxidle*8.0) / ns_per_byte) * (2 ** RM_FILTER_GAIN)
        offtime = ((offtime*8.0) / ns_per_byte) * (2 ** RM_FILTER_GAIN)
        minidle = ((minidle*8.0) / ns_per_byte) * (2 ** RM_FILTER_GAIN)

        self.opts["minburst"]    = minburst
        self.opts["maxburst"]    = maxburst
        self.opts["pktsize"]     = pktsize
        self.opts["maxpktsize"]  = maxpktsize
        self.opts["ns_per_byte"] = int(ns_per_byte)
        self.opts["maxidle"]     = int(abs(maxidle / 1000.0))
        self.opts["minidle"]     = int(minidle / 1000.0)
        self.opts["offtime"]     = int(abs(offtime / 1000.0))
Beispiel #2
0
    def __init__(self, altq=None, **kw):
        """Check argument and initialize class attributes.

        'altq' can be either a pfaltq structure or a string with the name of
        the queue.
        """
        if isinstance(altq, basestring):
            kw["qname"] = altq
        if altq is None or isinstance(altq, basestring):
            altq = pf_altq(qlimit=DEFAULT_QLIMIT, priority=DEFAULT_PRIORITY)

        super(PFAltq, self).__init__(altq, **kw)

        if not self.ifname:
            raise PFError("Interface name not specified")

        mtu, ifbw = getifmtu(self.ifname)
        if not self.ifbandwidth:
            if not ifbw:
                raise PFError("No ifbandwidth for interface " +
                              "'{.ifname}'".format(self))
            self.ifbandwidth = ifbw

        if not self.qname and not self.parent:
            # This is a root ALTQ discipline
            self.bandwidth = 0
            self.priority = 0

            if not self.tbrsize:
                self.tbrsize = mtu * 24
                if self.ifbandwidth <= 1 * 1000 * 1000:
                    self.tbrsize = mtu
                elif self.ifbandwidth <= 4 * 1000 * 1000:
                    self.tbrsize = mtu * 4
                elif self.ifbandwidth <= 8 * 1000 * 1000:
                    self.tbrsize = mtu * 8