diff --git a/src/LibreQoS.py b/src/LibreQoS.py index 0937271c..bbf6185e 100755 --- a/src/LibreQoS.py +++ b/src/LibreQoS.py @@ -28,7 +28,6 @@ from liblqos_python import is_lqosd_alive, clear_ip_mappings, delete_ip_mapping, run_shell_commands_as_sudo, generated_pn_download_mbps, generated_pn_upload_mbps, queues_available_override, \ on_a_stick, get_tree_weights, get_weights, is_network_flat, get_libreqos_directory, enable_insight_topology, \ fast_queues_fq_codel, hash_to_i64, fetch_planner_remote, store_planner_remote, \ - read_planner_cbor, write_planner_cbor, \ Bakery # Optional: urgent issue submission (available in newer liblqos_python) @@ -51,14 +50,13 @@ class Planner: PUBLIC methods: - getState(): dict — current planner state (default or loaded) - - persist(): None — save the planner state to planner.cbor + - persist(): None — persist planner state via Insight when enabled """ # Print the Insight planning banner only once per process _insight_banner_printed = False def __init__(self, queuesAvailable: int, onAStick: bool, siteNamesSet, siteCount: int): - self.path = os.path.join(get_libreqos_directory(), 'planner.cbor') now_ts = time.time() # Informational banner when Insight-enhanced planning is in use @@ -82,7 +80,7 @@ class Planner: # Missing/invalid planner state: build a fresh default and record why. try: if is_insight_enabled(): - logging.warning("Planner: no existing plan available from Insight/local store; using default plan.") + logging.warning("Planner: no existing plan available from Insight; using default plan.") else: logging.warning("Planner: no existing plan available (Insight disabled); using default plan.") except Exception: @@ -167,7 +165,7 @@ class Planner: Insight-only feature: when Insight is enabled, planner state is persisted remotely via `store_planner_remote`. Ephemeral fields such - as `free_minors` are never persisted. + as `free_minors` are never persisted, and no local CBOR files are used. """ try: to_save = dict(self.state) @@ -190,23 +188,23 @@ class Planner: # not enabled so that planner reuse remains an Insight feature. pass except Exception as e: - warnings.warn(f"Failed to write planner.cbor: {e}", stacklevel=2) + warnings.warn(f"Failed to persist planner state to Insight: {e}", stacklevel=2) def _default_state(self, site_names_sorted, site_count, queues_available, on_a_stick_flag, now_ts): """Build a default planner state. Notes - Minor IDs 1..3 are globally reserved: - 1 = root qdisc, 2 = default class, 3 = site classes - - Circuits and any ephemeral classes use 4+. + 1 = root qdisc, 2 = default class, 3 = per-CPU Generated_PN_* site classes + - All other sites, circuits, and any ephemeral classes use 4+. - `free_minors` is per-CPU and rebuilt on load; it is not persisted. """ try: # Reserve minors 1..3 globally: # - 1 is used by root qdisc # - 2 is used by the default class under root - # - 3 is used for site classes - # Circuits and any ephemeral classes must use 4+ + # - 3 is used for per-CPU Generated_PN_* site classes + # All other site and circuit classes must use 4+ free_minors = [list(range(4, 0x10000)) for _ in range(int(queues_available))] except Exception: free_minors = [] @@ -280,7 +278,7 @@ class Planner: try: cpu = int(entry.get('cpu', -1)) minor = int(entry.get('minor', -1)) - if 0 <= cpu < q and 3 <= minor <= 0xFFFF: + if 0 <= cpu < q and 4 <= minor <= 0xFFFF: if minor in free[cpu]: free[cpu].discard(minor) except Exception: @@ -293,7 +291,7 @@ class Planner: try: cpu = int(entry.get('cpu', -1)) minor = int(entry.get('minor', -1)) - if 0 <= cpu < q and 3 <= minor <= 0xFFFF: + if 0 <= cpu < q and 4 <= minor <= 0xFFFF: if minor in free[cpu]: free[cpu].discard(minor) except Exception: @@ -1348,11 +1346,12 @@ def refreshShapers(): knownClassIDs = [] nodes_requiring_min_squashing = {} # Track minor counter by CPU. This way we can have > 32000 hosts (htb has u16 limit to minor handle) - # Minor numbers start at 3 to reserve 1 for root qdisc and 2 for default class + # Minor numbers start at 4 to reserve: + # 1 = root qdisc, 2 = default class, 3 = per-CPU Generated_PN_* sites # With CIRCUIT_PADDING, we leave gaps between nodes to allow future circuit additions # without disrupting existing ClassID assignments. This maintains stability across reloads. for x in range(queuesAvailable): - minorByCPUpreloaded[x+1] = 3 + minorByCPUpreloaded[x+1] = 4 def traverseNetwork(data, depth, major, minorByCPU, queue, parentClassID, upParentClassID, parentMaxDL, parentMaxUL, parentMinDL, parentMinUL): nonlocal site_insert_counter # ClassID Assignment Strategy: @@ -1385,49 +1384,64 @@ def refreshShapers(): # data[node]['up_cpuNum'] = hex(queue-1+stickOffset) # traverseNetwork(data[node]['children'], depth, major, minorByCPU, queue, parentClassID, upParentClassID, parentMaxDL, parentMaxUL) # continue - # Prefer reusing previously-assigned site minor from planner.site_map (keyed by hashed site name); - # else try planner free_minors for this CPU; else fallback to sequential counter - chosen_minor = None + # Prefer fixed, low minors for Generated_PN_* per CPU so they are + # visually obvious and stable. For all other nodes, prefer reusing + # previously-assigned site minors from planner.site_map; else try + # planner free_minors for this CPU; else fallback to sequential + # counter starting at 4. + name_s = str(node) + is_generated_pn = False try: - if ('planner' in locals() or 'planner' in globals()) and isinstance(planner, Planner) and (not getattr(planner, '_is_default', False)): - st = planner.getState() - cpu_index = int(queue - 1) - # 1) Attempt reuse from site_map when CPU/major match - try: - site_map = st.get('site_map') if isinstance(st, dict) else None - if isinstance(site_map, dict): - key_hash = int(hash_to_i64(str(node))) - entry = site_map.get(key_hash) - if isinstance(entry, dict): - saved_cpu = int(entry.get('cpu', -1)) - saved_major = int(entry.get('major', -1)) - saved_minor = int(entry.get('minor', -1)) - if saved_cpu == cpu_index and saved_major == int(major) and saved_minor >= 3: - chosen_minor = saved_minor - # Remove from free list if present - try: - fm = st.get('free_minors') - if isinstance(fm, list) and 0 <= cpu_index < len(fm) and isinstance(fm[cpu_index], list) and saved_minor in fm[cpu_index]: - fm[cpu_index].remove(saved_minor) - except Exception: - pass - except Exception: - pass - # 2) If no reuse, allocate from free list for this CPU (depth >= 1 only) - if chosen_minor is None and depth >= 1: - try: - fm = st.get('free_minors') if isinstance(st, dict) else None - if isinstance(fm, list) and 0 <= cpu_index < len(fm) and isinstance(fm[cpu_index], list) and len(fm[cpu_index]) > 0: - chosen_minor = int(fm[cpu_index].pop(0)) - except Exception: - chosen_minor = None + is_generated_pn = name_s.startswith('Generated_PN_') except Exception: - chosen_minor = None - if chosen_minor is None: + is_generated_pn = False + + chosen_minor = None + # Fixed minor for Generated_PN_*: always 3 per CPU, independent of + # planner state and traversal order. + if is_generated_pn: + chosen_minor = 3 + else: try: - chosen_minor = int(minorByCPU[queue]) + if ('planner' in locals() or 'planner' in globals()) and isinstance(planner, Planner) and (not getattr(planner, '_is_default', False)): + st = planner.getState() + cpu_index = int(queue - 1) + # 1) Attempt reuse from site_map when CPU/major match + try: + site_map = st.get('site_map') if isinstance(st, dict) else None + if isinstance(site_map, dict): + key_hash = int(hash_to_i64(str(node))) + entry = site_map.get(key_hash) + if isinstance(entry, dict): + saved_cpu = int(entry.get('cpu', -1)) + saved_major = int(entry.get('major', -1)) + saved_minor = int(entry.get('minor', -1)) + if saved_cpu == cpu_index and saved_major == int(major) and saved_minor >= 4: + chosen_minor = saved_minor + # Remove from free list if present + try: + fm = st.get('free_minors') + if isinstance(fm, list) and 0 <= cpu_index < len(fm) and isinstance(fm[cpu_index], list) and saved_minor in fm[cpu_index]: + fm[cpu_index].remove(saved_minor) + except Exception: + pass + except Exception: + pass + # 2) If no reuse, allocate from free list for this CPU (depth >= 1 only) + if chosen_minor is None and depth >= 1: + try: + fm = st.get('free_minors') if isinstance(st, dict) else None + if isinstance(fm, list) and 0 <= cpu_index < len(fm) and isinstance(fm[cpu_index], list) and len(fm[cpu_index]) > 0: + chosen_minor = int(fm[cpu_index].pop(0)) + except Exception: + chosen_minor = None except Exception: - chosen_minor = 3 + chosen_minor = None + if chosen_minor is None: + try: + chosen_minor = int(minorByCPU[queue]) + except Exception: + chosen_minor = 4 nodeClassID = hex(major) + ':' + hex(chosen_minor) upNodeClassID = hex(major+stickOffset) + ':' + hex(chosen_minor) @@ -2019,7 +2033,8 @@ def refreshShapers(): cpu = int(entry.get('cpu', 0)) minor = int(entry.get('minor', -1)) if isinstance(fm, list) and 0 <= cpu < len(fm) and isinstance(fm[cpu], list): - if minor >= 3 and minor <= 0xFFFF and (minor not in fm[cpu]): + # Reserve 1..3 globally; only return minors >=4 to the free list + if minor >= 4 and minor <= 0xFFFF and (minor not in fm[cpu]): fm[cpu].append(minor) # Remove from circuit_map try: diff --git a/src/bakery_integration_test.py b/src/bakery_integration_test.py index 05f0c60b..7cc81463 100644 --- a/src/bakery_integration_test.py +++ b/src/bakery_integration_test.py @@ -488,12 +488,28 @@ def _read_htb_rate_ceil_mbps(iface: str, major: int, minor: int) -> Optional[Tup rc, out, err = _tc(["class", "show", "dev", iface]) if rc != 0: return None - # Match the exact class line for major:minor in decimal - cls_pat = re.compile(rf"^class\s+htb\s+{major}:{minor}\b.*$", re.MULTILINE) - m = cls_pat.search(out) - if not m: + # Match the class line for this major/minor, accepting decimal or hex forms. + cls_pat = re.compile(r"^class\s+htb\s+([0-9A-Fa-fx]+):([0-9A-Fa-fx]+)\b.*$", re.MULTILINE) + + def _to_int(tok: str) -> Optional[int]: + try: + s = tok.strip() + if s.lower().startswith("0x"): + return int(s, 16) + return int(s) + except Exception: + return None + + line = None + for m in cls_pat.finditer(out): + mj_tok, mn_tok = m.group(1), m.group(2) + mj = _to_int(mj_tok) + mn = _to_int(mn_tok) + if mj == major and mn == minor: + line = m.group(0) + break + if line is None: return None - line = m.group(0) # Extract rate and ceil tokens rpat = re.compile(r"rate\s+([0-9]+(?:\.[0-9]+)?)([GMK])?bit.*?ceil\s+([0-9]+(?:\.[0-9]+)?)([GMK])?bit") mm = rpat.search(line) @@ -787,16 +803,9 @@ def _class_has_parent(iface: str, maj: int, mnr: int, pmaj: int, pmnr: int) -> T rc, out, err = _tc(["class", "show", "dev", iface]) if rc != 0: return False, f"tc error: {err.strip()}" - # Find the specific class line first - cls_pat = re.compile(rf"^class\s+htb\s+{maj}:{mnr}\b.*$", re.MULTILINE) - m = cls_pat.search(out) - if not m: - return False, None - line = m.group(0) - # Extract parent handle (supports hex or decimal) - p = re.search(r"parent\s+([0-9A-Fa-fx]+):([0-9A-Fa-fx]+)\b", line) - if not p: - return False, line + # Find the specific class line first (accept decimal or hex handles) + cls_pat = re.compile(r"^class\s+htb\s+([0-9A-Fa-fx]+):([0-9A-Fa-fx]+)\b.*$", re.MULTILINE) + def _to_int(tok: str) -> Optional[int]: try: s = tok.strip() @@ -805,6 +814,21 @@ def _class_has_parent(iface: str, maj: int, mnr: int, pmaj: int, pmnr: int) -> T return int(s) except Exception: return None + + line = None + for m in cls_pat.finditer(out): + mj_tok, mn_tok = m.group(1), m.group(2) + mj = _to_int(mj_tok) + mn = _to_int(mn_tok) + if mj == maj and mn == mnr: + line = m.group(0) + break + if line is None: + return False, None + # Extract parent handle (supports hex or decimal) + p = re.search(r"parent\s+([0-9A-Fa-fx]+):([0-9A-Fa-fx]+)\b", line) + if not p: + return False, line pmj = _to_int(p.group(1)) pmn = _to_int(p.group(2)) if pmj is None or pmn is None: @@ -1281,10 +1305,26 @@ def run_tiered_suite(log: LogReader, timeout_s: float, results: List[str]) -> bo msgs.append(f"low-rate rtt: 'tc qdisc show dev {ifa}' failed: {err.strip()}") ok_local = False else: - pat = re.compile(rf"^qdisc\s+\S+\s+\S+:\s+parent\s+{maj}:{mnr}\b.*$", re.MULTILINE) - m = pat.search(out) - if m: - line = m.group(0) + pat = re.compile(r"^qdisc\s+\S+\s+\S+:\s+parent\s+([0-9A-Fa-fx]+):([0-9A-Fa-fx]+)\b.*$", re.MULTILINE) + + def _to_int(tok: str) -> Optional[int]: + try: + s = tok.strip() + if s.lower().startswith("0x"): + return int(s, 16) + return int(s) + except Exception: + return None + + line = None + for m in pat.finditer(out): + mj_tok, mn_tok = m.group(1), m.group(2) + mj = _to_int(mj_tok) + mn = _to_int(mn_tok) + if mj == maj and mn == mnr: + line = m.group(0) + break + if line is not None: if "cake" in line.lower(): if re.search(r"\brtt\s+\d+\s*ms\b", line, re.IGNORECASE): msgs.append(f"low-rate rtt: down {ifa} OK | {line}") @@ -1303,10 +1343,26 @@ def run_tiered_suite(log: LogReader, timeout_s: float, results: List[str]) -> bo msgs.append(f"low-rate rtt: 'tc qdisc show dev {ifb}' failed: {err2.strip()}") ok_local = False else: - pat2 = re.compile(rf"^qdisc\s+\S+\s+\S+:\s+parent\s+{upm}:{mnr}\b.*$", re.MULTILINE) - m2 = pat2.search(out2) - if m2: - line2 = m2.group(0) + pat2 = re.compile(r"^qdisc\s+\S+\s+\S+:\s+parent\s+([0-9A-Fa-fx]+):([0-9A-Fa-fx]+)\b.*$", re.MULTILINE) + + def _to_int2(tok: str) -> Optional[int]: + try: + s = tok.strip() + if s.lower().startswith("0x"): + return int(s, 16) + return int(s) + except Exception: + return None + + line2 = None + for m2 in pat2.finditer(out2): + mj_tok, mn_tok = m2.group(1), m2.group(2) + mj = _to_int2(mj_tok) + mn = _to_int2(mn_tok) + if mj == upm and mn == mnr: + line2 = m2.group(0) + break + if line2 is not None: if "cake" in line2.lower(): if re.search(r"\brtt\s+\d+\s*ms\b", line2, re.IGNORECASE): msgs.append(f"low-rate rtt: up {ifb} OK | {line2}") diff --git a/src/rust/lqos_bakery/src/commands.rs b/src/rust/lqos_bakery/src/commands.rs index 0ad4198c..24f40692 100644 --- a/src/rust/lqos_bakery/src/commands.rs +++ b/src/rust/lqos_bakery/src/commands.rs @@ -529,6 +529,10 @@ impl BakeryCommands { params: AddSiteParams, ) -> Option>> { let mut result = Vec::new(); + // Derive major IDs from parent handles so classids are fully qualified + // and consistent with queuingStructure.json (classMajor/classMinor). + let (down_major, _) = params.parent_class_id.get_major_minor(); + let (up_major, _) = params.up_parent_class_id.get_major_minor(); /* bakery.add_site(data[node]['parentClassID'], data[node]['up_parentClassID'], data[node]['classMinor'], format_rate_for_tc(data[node]['downloadBandwidthMbpsMin']), format_rate_for_tc(data[node]['uploadBandwidthMbpsMin']), format_rate_for_tc(data[node]['downloadBandwidthMbps']), format_rate_for_tc(data[node]['uploadBandwidthMbps']), quantum(data[node]['downloadBandwidthMbps']), quantum(data[node]['uploadBandwidthMbps'])) @@ -549,7 +553,7 @@ impl BakeryCommands { "parent".to_string(), params.parent_class_id.as_tc_string(), "classid".to_string(), - format!("0x{:x}", params.class_minor), + format!("0x{:x}:0x{:x}", down_major, params.class_minor), "htb".to_string(), "rate".to_string(), format_rate_for_tc_f32(params.download_bandwidth_min), @@ -571,7 +575,7 @@ impl BakeryCommands { "parent".to_string(), params.up_parent_class_id.as_tc_string(), "classid".to_string(), - format!("0x{:x}", params.class_minor), + format!("0x{:x}:0x{:x}", up_major, params.class_minor), "htb".to_string(), "rate".to_string(), format_rate_for_tc_f32(params.upload_bandwidth_min), @@ -696,7 +700,7 @@ impl BakeryCommands { "parent".to_string(), params.parent_class_id.as_tc_string(), "classid".to_string(), - format!("0x{:x}", params.class_minor), + format!("0x{:x}:0x{:x}", params.class_major, params.class_minor), "htb".to_string(), "rate".to_string(), format_rate_for_tc_f32(params.download_bandwidth_min), @@ -741,7 +745,7 @@ impl BakeryCommands { "parent".to_string(), params.up_parent_class_id.as_tc_string(), "classid".to_string(), - format!("0x{:x}", params.class_minor), + format!("0x{:x}:0x{:x}", params.up_class_major, params.class_minor), "htb".to_string(), "rate".to_string(), format_rate_for_tc_f32(params.upload_bandwidth_min), @@ -896,7 +900,11 @@ impl BakeryCommands { "parent".to_string(), parent_class_id.as_tc_string(), "classid".to_string(), - format!("0x{class_minor:x}"), + format!( + "0x{:x}:0x{:x}", + parent_class_id.get_major_minor().0, + class_minor + ), ]); result.push(vec![ "class".to_string(), @@ -906,7 +914,11 @@ impl BakeryCommands { "parent".to_string(), up_parent_class_id.as_tc_string(), "classid".to_string(), - format!("0x{class_minor:x}"), + format!( + "0x{:x}:0x{:x}", + up_parent_class_id.get_major_minor().0, + class_minor + ), ]); }