## 4 Design of RBMM
### Buffer Replacement Strategy Based on Flow Priority Scoring
```
1:Input:\(pkt\), \(RBTree_{1,...,N}\), \(ExpSeqNum_{1,...,N}\), Flag
2:Output:\(Score_{k}\)
3:Initialization:
4:\(a=0.1,b=1\)
5:\(k=GetEid(pkt),SeqNum=GetSeq(pkt),L=GetLength(pkt)\)
6:\(N_{1}=RBTree_{k}\)-size()
7:\(MinimumSeq=RBTree_{k}.begin()\)
8:\(MaximumSeq=RBTree_{k}.end()\)
9:\(D_{1}=MinimumSeq-ExpSeqNum_{k}-1\)
10:\(D_{\text{inside}}=MaximumSeq-MinimumSeq-N_{1}+1\)
11:\(L_{\text{norm}}=a+\left(\frac{L-L_{\text{min}}}{L_{\text{max}}-L_{\text{ min}}}\right)\cdot(b-a)\)
12:if\(Flag==0\)then\(\triangleright\) Packet insertion
13:if\(SeqNum\geq MaximumSeq+1\)then
14:\(Score_{k}=Score_{k}+(D_{1}+D_{\text{inside}})\cdot L_{\text{norm}}\)
15:else
16:\(Score_{k}=Score_{k}+(D_{1}+D_{\text{inside}}-MaximumSeq+SeqNum-1)\cdot L_{ \text{norm}}\)
17:endif
18:elseif\(Flag==1\)then\(\triangleright\) Packet removal
19:\(Score_{k}=Score_{k}-D_{1}\cdot L_{\text{norm}}\)
20:endif
```  
**Algorithm 2** Differential Update Flow Scoring Algorithm  
In the initialization phase, the algorithm defines the normalized range as \([0.1,1]\), avoiding the occurrence of zero scores. By using the flow EID, we can determine the size of its corresponding Red-Black Tree, as well as the minimum and maximum sequence numbers, allowing us to calculate \(D_{1}\) and the total number of gaps \(D_{\text{inside}}\) within the Red-Black Tree. Based on these conditions, we can compute the corresponding score changes when packets are inserted into or removed from the Red-Black Tree under different scenarios. When the sequence number of the inserted packet exceeds all existing sequence numbers in the Red-Black Tree, the increase in score is represented by Line 14 of the algorithm. When the packet with the minimum sequence number in the Red-Black Tree is forwarded to the terminal, the decrease in score is represented by Line 19 of the algorithm. For packets inserted in the middle positions of the Red-Black Tree, their impact on the score can also be expressed through simple derivation based on the given conditions. As shown in Figure 4, we assume a packet (with sequence number \(SeqNum\)) is inserted into the position of the Red-Black Tree at the \(m-1\)th gap. The score at this moment is  
\[ \begin{split}\text{Score}^{\prime}&=[D_{1}\cdot(N_ {1}+1)+D_{2}\cdot(N_{2}+1)+\cdots+(SeqNum-a-1)\cdot(N_{m}+1)\\ &\quad+(b-SeqNum-1)\cdot N_{m}+\cdots+D_{n}\cdot N_{n}]\cdot L_{ \text{norm}}\\ &=[D_{1}\cdot(N_{1}+1)+D_{2}\cdot(N_{2}+1)+(b-a-2)\cdot N_{m}+SeqNum -a-1\\ &\quad+\cdots+D_{n}\cdot N_{n}]\cdot L_{\text{norm}}\end{split}  \tag{8}\]  
Since \(D_{m}=b-a-1\), the score difference before and after the packet insertion can be calculated by subtracting Equation (7) from Equation (8):  
\[ \begin{split}\delta_{\text{score}}&=\text{Score}^{ \prime}-\text{Score}\\ &=[D_{1}+D_{2}+\cdots+D_{m-1}-N_{m}+SeqNum-a-1]\cdot L_{\text{norm }}\\ &=[D_{1}+D_{\text{inside}}-(D_{m}+D_{m+1}+\cdots+D_{n})-N_{m}+SeqNum -a-1]\cdot L_{\text{norm}}\\ &=[D_{1}+D_{\text{inside}}-[(D_{m}+D_{m+1}+\cdots+D_{n})+N_{m}- SeqNum+a+1]]\cdot L_{\text{norm}}\\ &=[D_{1}+D_{\text{inside}}-(MaximumSeq-a-SeqNum+a+1)]\cdot L_{ \text{norm}}\\ &=[D_{1}+D_{\text{inside}}-(MaximumSeq-SeqNum+1)]\cdot L_{ \text{norm}}\\ \end{split}  \tag{9}\](Conclusion at Line 16 of Algorithm 2)  
After calculating the scores, different replacement strategies are adopted for the buffer under various conditions. If there are PHRD flows present in the buffer, newly arrived packets inevitably replace existing packets in the buffer, with the element having the minimum sequence number from the PHRD flow with the highest score being evicted. This packet is then directly forwarded to the terminal. If no PHRD flows are present in the buffer, the scores are then normalized to the range [0.1, 1] using the Min-Max method.