socsvn commit: r274309 - soc2014/dpl/netmap-ipfwjit/sys/netpfil/ipfw

dpl at FreeBSD.org dpl at FreeBSD.org
Thu Sep 18 14:30:29 UTC 2014


Author: dpl
Date: Thu Sep 18 14:30:27 2014
New Revision: 274309
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=274309

Log:
  Corrected some wrong rules actions.

Modified:
  soc2014/dpl/netmap-ipfwjit/sys/netpfil/ipfw/ip_fw_rules.c
  soc2014/dpl/netmap-ipfwjit/sys/netpfil/ipfw/jit.cc

Modified: soc2014/dpl/netmap-ipfwjit/sys/netpfil/ipfw/ip_fw_rules.c
==============================================================================
--- soc2014/dpl/netmap-ipfwjit/sys/netpfil/ipfw/ip_fw_rules.c	Thu Sep 18 13:59:36 2014	(r274308)
+++ soc2014/dpl/netmap-ipfwjit/sys/netpfil/ipfw/ip_fw_rules.c	Thu Sep 18 14:30:27 2014	(r274309)
@@ -2,7 +2,8 @@
  #include <sys/ucred.h>
  #include <netinet/in.h>
 
- #define IPFW_RULES_INLINE __unused
+ // #define IPFW_RULES_INLINE __unused
+ #define IPFW_RULES_INLINE __attribute__((used))
  #include "ip_fw_rules.h"
 
 // The real function will be compiled and inserted by the JIT.

Modified: soc2014/dpl/netmap-ipfwjit/sys/netpfil/ipfw/jit.cc
==============================================================================
--- soc2014/dpl/netmap-ipfwjit/sys/netpfil/ipfw/jit.cc	Thu Sep 18 13:59:36 2014	(r274308)
+++ soc2014/dpl/netmap-ipfwjit/sys/netpfil/ipfw/jit.cc	Thu Sep 18 14:30:27 2014	(r274309)
@@ -911,7 +911,6 @@
 	{
 		// Create the module and load the code.
 		mod = loadBitcode("rules.bc");
-		mod->dump();
 
 		Func = mod->getFunction("ipfw_chk_jit");
 		if (Func == NULL)
@@ -1328,22 +1327,30 @@
 	void
 	emit_nop()
 	{
-		Irb.CreateStore(ConstantInt::get(Int32Ty, 1), Match);
+		// rule_nop(&match);
+		Irb.CreateCall(RuleNop, Match);
 	}
 
 	// XXX Exec not tested.
 	void
 	emit_forward_mac()
 	{
-		printf("Compilation error:\n");
-		printf("ipfwjitter: MAC forwarding unimplemented\n");
-		printf("Compilation continues.\n");
+		// rule_forward_mac(cmd->opcode);
+		Value *CmdL = Irb.CreateLoad(Cmd);
+		Value *Opcode = Irb.CreateStructGEP(CmdL, 0);
+		Value *OpcodeL = Irb.CreateLoad(Opcode);
+		// Opcode is u_int8
+		Value *OpcodeL32 = Irb.CreateZExt(OpcodeL, Int32Ty);
+		Irb.CreateCall(RuleForwardMac, {OpcodeL32});
 	}
 
 	// XXX Exec not tested.
 	void
 	emit_jail()
 	{
+		// rule_jail(&match, offset, proto, cmd, args, ucred_lookup, ucred_cache);
+		// We roll our own version because we don't have ucred_lookup.
+
 		BasicBlock *OffsetNZ = BasicBlock::Create(Con, "R_offsetnotzero", Func);
 		BasicBlock *OffsetZE = BasicBlock::Create(Con, "R_offsetiszero", Func);
 		BasicBlock *TCPorUDP = BasicBlock::Create(Con, "R_setmatchzero", Func);
@@ -1388,57 +1395,27 @@
 	void
 	emit_recv()
 	{
-		//*match = iface_match(m->m_pkthdr.rcvif, (ipfw_insn_if *)cmd, chain, tablearg);
-		Value *MPkthdr = Irb.CreateStructGEP(M, 5);
-		Value *Rcvif = Irb.CreateStructGEP(MPkthdr, 0);
-		Value *RcvifL = Irb.CreateLoad(Rcvif);
+		// rule_recv(&match, cmd, m, chain, &tablearg);
 		Value *CmdL = Irb.CreateLoad(Cmd);
-		Value *CmdBitC = Irb.CreateBitCast(CmdL, Ipfw_insn_ifPtrTy);
-		Value *IfaceMatchCall = Irb.CreateCall4(IfaceMatch, RcvifL, CmdBitC, Chain, Tablearg);
-		Irb.CreateStore(IfaceMatchCall, Match);
+		Irb.CreateCall(RuleRecv, {Match, CmdL, M, Chain, Tablearg});
 	}
 
 	// XXX Exec not tested.
 	void
 	emit_xmit()
 	{
-		//*match = iface_match(oif, (ipfw_insn_if *)cmd, chain, tablearg);
+		// rule_xmit(&match, oif, cmd, chain, &tablearg);
 		Value *CmdL = Irb.CreateLoad(Cmd);
-		Value *Cmdc = Irb.CreateBitCast(CmdL, Ipfw_insn_ifPtrTy);
-		Value *IfaceMatchCall = Irb.CreateCall4(IfaceMatch, Oif, Cmdc, Chain, Tablearg);
-		Irb.CreateStore(IfaceMatchCall, Match);
+		Irb.CreateCall(RuleXmit, {Match, Oif, CmdL, Chain, Tablearg});
 	}
 
 	// XXX Exec not tested.
 	void
 	emit_via()
 	{
-		BasicBlock *OifNZ = BasicBlock::Create(Con, "R_OifNotZero", Func);
-		BasicBlock *OifZE = BasicBlock::Create(Con, "R_OifIsZero", Func);
-		Value *IfaceMatchCall;
-
-		// if (oif)
-		// 	match = iface_match(oif, (ipfw_insn_if *)cmd, chain, &tablearg);
-		// else
-		// 	match = iface_match(m->m_pkthdr.rcvif, (ipfw_insn_if *)cmd,
-		// 		chain, &tablearg);
-		Value *Comp = Irb.CreateICmpNE(Oif, ConstantPointerNull::get((PointerType *)Oif->getType()));
-		Value *CmdLBitC = Irb.CreateBitCast(Cmd, Ipfw_insn_ifPtrTy);
-		Irb.CreateCondBr(Comp, OifNZ, OifZE );
-
-		Irb.SetInsertPoint(OifNZ);
-		// 	match = iface_match(oif, (ipfw_insn_if *)cmd, chain, &tablearg);
-		IfaceMatchCall = Irb.CreateCall4(IfaceMatch, Oif, CmdLBitC, Chain, Tablearg);
-		Irb.CreateStore(IfaceMatchCall, Match);
-
-		Irb.SetInsertPoint(OifZE);
-		// 	match = iface_match(m->m_pkthdr.rcvif, (ipfw_insn_if *)cmd,
-		// 		chain, &tablearg);
-		Value *MPkthdr = Irb.CreateStructGEP(M, 5);
-		Value *Rcvif = Irb.CreateStructGEP(MPkthdr, 0);
-		Value *RcvifL = Irb.CreateLoad(Rcvif);
-		IfaceMatchCall = Irb.CreateCall4(IfaceMatch, RcvifL, CmdLBitC, Chain, Tablearg);
-		Irb.CreateStore(IfaceMatchCall, Match);
+		// rule_via(&match, oif, m, cmd, chain, &tablearg);
+		Value *CmdL = Irb.CreateLoad(Cmd);
+		Irb.CreateCall(RuleVia, {Match, Oif, M, CmdL, Chain, Tablearg});
 	}
 
 	void
@@ -1451,40 +1428,29 @@
 	{
 	}
 
-
 	// XXX Exec not tested.
 	void
 	emit_frag()
 	{
-		// match = (offset != 0);
+		// rule_frag(&match, offset);
 		Value *OffsetL = Irb.CreateLoad(Offset);
-		Value *Comp = Irb.CreateICmpNE(OffsetL, ConstantInt::get(OffsetL->getType(), 0));
-		Value *Comp32 = Irb.CreateSExt(Comp, Int32Ty);
-		Irb.CreateStore(Comp32, Match);
+		Irb.CreateCall(RuleFrag, {Match, OffsetL});
 	}
 
 	// XXX Exec not tested.
 	void
 	emit_in()
 	{
-		// "out" is "not in"
-		// match = (oif == NULL);
-		Value *OffsetL = Irb.CreateLoad(Offset);
-		Value *Comp = Irb.CreateICmpEQ(OffsetL, ConstantInt::get(OffsetL->getType(), 0));
-		Value *Comp32 = Irb.CreateSExt(Comp, Int32Ty);
-		Irb.CreateStore(Comp32, Match);
+		// rule_in(&match, oif);
+		Irb.CreateCall(RuleIn, {Match, Oif});
 	}
 
 	// XXX Exec not tested.
 	void
 	emit_layer2()
 	{
-		// match = (args->eh != NULL);
-		Value *EhPtr = Irb.CreateStructGEP(Args, 5);
-		Value *Eh = Irb.CreateLoad(EhPtr);
-		Value *Comp = Irb.CreateICmpNE(Eh, ConstantPointerNull::get((PointerType *)Eh->getType()));
-		Value *Comp32 = Irb.CreateSExt(Comp, Int32Ty);
-		Irb.CreateStore(Comp32, Match);
+		// rule_layer2(&match, args);
+		Irb.CreateCall(RuleLayer2, {Match, Args});
 	}
 
 	void
@@ -1501,27 +1467,10 @@
 	void
 	emit_ip_src()
 	{
-		// match = is_ipv4 &&
-		// 	(((ipfw_insn_ip *)cmd)->addr.s_addr ==
-		// 	src_ip.s_addr);
-
-		// ((ipfw_insn_ip *)cmd)->addr.s_addr
-		Value *CmdBitC = Irb.CreateBitCast(Cmd, IpfwInsnIpPtrTy);
-		Value *CmdAddr = Irb.CreateStructGEP(CmdBitC, 1);
-		Value *CmdSAddr = Irb.CreateStructGEP(CmdAddr, 0);
-		Value *CmdSAddrL = Irb.CreateLoad(CmdSAddr);
-
-		// src_ip.s_addr
-		//Value *SrcIpL = Irb.CreateLoad(SrcIp);
-		Value *SrcAddr = Irb.CreateStructGEP(SrcIp, 0);
-		Value *SrcAddrL = Irb.CreateLoad(SrcAddr);
-		//s_addr == src_ip.s_addr
-		Value *Comp = Irb.CreateICmpEQ(CmdSAddrL, SrcAddrL);
-
+		// rule_ip_src(&match, is_ipv4, cmd, &src_ip);
 		Value *IsIpv4L = Irb.CreateLoad(IsIpv4);
-		Value *Comp32 = Irb.CreateZExt(Comp, IsIpv4L->getType());
-		Value *NewMatch = Irb.CreateAnd(Comp32, IsIpv4L);
-		Irb.CreateStore(NewMatch, Match);
+		Value *CmdL = Irb.CreateLoad(Cmd);
+		Irb.CreateCall(RuleIpSrc, {Match, IsIpv4L, CmdL, SrcIp});
 	}
 
 	void
@@ -1581,117 +1530,6 @@
 		Value *SrcPortL = Irb.CreateLoad(SrcPort);
 
 		Irb.CreateCall(RuleIpDstport, {Match, ProtoL, OffsetL, CmdL, CmdlenL, DstPortL, SrcPortL});
-
-		// // /*
-		// //  * offset == 0 && proto != 0 is enough
-		// //  * to guarantee that we have a
-		// //  * packet with port info.
-		// //  */
-		// // if ((proto==IPPROTO_UDP || proto==IPPROTO_TCP)
-		// // 	&& offset == 0) {
-		// // 	u_int16_t x =
-		// // 		(cmd->opcode == O_IP_SRCPORT) ?
-		// // 		src_port : dst_port ;
-		// // 	u_int16_t *p =
-		// // 		((ipfw_insn_u16 *)cmd)->ports;
-		// // 	int i;
-
-		// // 	for (i = cmdlen - 1; !match && i>0;
-		// // 		i--, p += 2)
-		// // 		match = (x>=p[0] && x<=p[1]);
-		// //  }
-		// BasicBlock *Yes = BasicBlock::Create(Con, "R_Yes", Func);
-		// BasicBlock *Out = BasicBlock::Create(Con, "R_Out", Func);
-		// BasicBlock *Src = BasicBlock::Create(Con, "R_Src", Func);
-		// BasicBlock *Dst = BasicBlock::Create(Con, "R_Dst", Func);
-		// BasicBlock *Loop = BasicBlock::Create(Con, "R_Loop", Func);
-		// BasicBlock *ContLoop = BasicBlock::Create(Con, "R_ContLoop", Func);
-		// Value *Comp;
-
-		// // Perform allocations at the beginning.
-		// Value *X = Irb.CreateAlloca(Int16Ty, nullptr, "x");
-		// Value *P = Irb.CreateAlloca(Int16PtrTy, nullptr, "p");
-		// Value *I = Irb.CreateAlloca(Int32Ty, nullptr, "i");
-		// // p = ((ipfw_insn_u16 *)cmd)->ports;
-		// Value *CmdLBitC = Irb.CreateBitCast(Cmd, IpfwInsnU16PtrTy);
-		// Value *Ports = Irb.CreateStructGEP(CmdLBitC, 1);
-		// Value *PortsGEP = Irb.CreateStructGEP(Ports, 0);
-		// Irb.CreateStore(PortsGEP, P);
-		// // New p
-		// Value *PL = Irb.CreateLoad(P);
-
-		// // (proto == IPPROTO_UDP || proto == IPPROTO_TCP)
-		// Value *ProtoL = Irb.CreateLoad(Proto);
-		// Value *Comp1 = Irb.CreateICmpEQ(ProtoL, ConstantInt::get(ProtoL->getType(), IPPROTO_TCP));
-		// Value *Comp2 = Irb.CreateICmpEQ(ProtoL, ConstantInt::get(ProtoL->getType(), IPPROTO_UDP));
-		// Value *OrComps = Irb.CreateOr(Comp1, Comp2);
-
-		// // (Offset == 0)
-		// Value *OffsetL = Irb.CreateLoad(Offset);
-		// Value *Comp3 = Irb.CreateICmpEQ(OffsetL, ConstantInt::get(OffsetL->getType(), 0));
-		// // (OrComps && Comp3)
-		// Comp = Irb.CreateAnd(OrComps, Comp3);
-		// Irb.CreateCondBr(Comp, Yes, Out);
-
-		// // yes:
-		// Irb.SetInsertPoint(Yes);
-		// // if (cmd->opcode == O_IP_SRCPORT)
-		// Value *CmdL = Irb.CreateLoad(Cmd);
-		// Value *OpcodePtr = Irb.CreateStructGEP(CmdL, 0);
-		// Value *Opcode = Irb.CreateLoad(OpcodePtr);
-		// Comp = Irb.CreateICmpEQ(Opcode, ConstantInt::get(Opcode->getType(), O_IP_SRCPORT));
-		// Irb.CreateCondBr(Comp, Src, Dst);
-	
-		// Irb.SetInsertPoint(Src);
-		// // 	u_int16_t x = src_port;
-		// Value *SrcPortL = Irb.CreateLoad(SrcPort);
-		// Irb.CreateStore(SrcPortL, X);
-		// Irb.CreateBr(Loop);
-
-		// Irb.SetInsertPoint(Dst);
-		// // 	u_int16_t x = dst_port;
-		// Value *DstPortL = Irb.CreateLoad(DstPort);
-		// Irb.CreateStore(DstPortL, X);
-		// Irb.CreateBr(Loop);
-
-		// Irb.SetInsertPoint(Loop);
-		// // Loop initialisation
-		// // i = cmdlen - 1;
-		// // cmdlen: signed
-		// Value *CmdlenL = Irb.CreateLoad(Cmdlen);
-		// Value *Sub = Irb.CreateNSWSub(CmdlenL, ConstantInt::get(CmdlenL->getType(), 1));
-		// Irb.CreateStore(Sub, I);
-		// Irb.CreateBr(ContLoop);
-
-		// // Check condition
-		// Irb.SetInsertPoint(ContLoop);
-		// // while((!match) && (i>0)) {
-		// Value *IL = Irb.CreateLoad(I);
-		// Value *MatchL = Irb.CreateLoad(Match);
-		// Comp1 = Irb.CreateICmpEQ(MatchL, ConstantInt::get(MatchL->getType(), 0));
-		// Comp2 = Irb.CreateICmpSGT(IL, ConstantInt::get(IL->getType(), 0));
-		// Value *BreakCond = Irb.CreateAnd(Comp1, Comp2);
-		// Irb.CreateCondBr(BreakCond, ContLoop, Out);
-
-		// // 	match = ((x >= p[0]) && (x <= p[1]));
-		// Value *PZ = Irb.CreateInBoundsGEP(PL, ConstantInt::get(Int32Ty, 0));
-		// Value *PO = Irb.CreateInBoundsGEP(PL, ConstantInt::get(Int32Ty, 1));
-		// Comp1 = Irb.CreateICmpUGE(X, PZ);
-		// Comp2 = Irb.CreateICmpULE(X, PO);
-		// Comp = Irb.CreateAnd(Comp1, Comp2);
-		// Value *Comp32 = Irb.CreateSExt(Comp, MatchL->getType());
-		// Irb.CreateStore(Comp32, Match);
-
-		// // Increment, decrement.
-		// // 	i--;
-		// Value *ILD = Irb.CreateNSWSub(IL, ConstantInt::get(IL->getType(), 1));
-		// Irb.CreateStore(ILD, I);
-		// // 	p += 2;
-		// Value *PGEP = Irb.CreateInBoundsGEP(PL, ConstantInt::get(Int32Ty, 2));
-		// Irb.CreateStore(PGEP, P);
-		// Irb.CreateBr(ContLoop);
-
-		// Irb.SetInsertPoint(Out);
 	}
 
 	void
@@ -1916,13 +1754,8 @@
 	void
 	emit_accept()
 	{
-
-		// retval = 0;	/* accept */
-		Irb.CreateStore(ConstantInt::get(Int32Ty, IP_FW_PASS), Retval);
-		// l = 0;		/* exit inner loop */
-		Irb.CreateStore(ConstantInt::get(Int32Ty, 0), L);
-		// done = 1;	/* exit outer loop */
-		Irb.CreateStore(ConstantInt::get(Int32Ty, 1), Done);
+		// rule_deny(&l, &done, &retval);
+		Irb.CreateCall(RuleAccept, {L, Done, Retval});
 	}
 
 	void
@@ -1986,12 +1819,8 @@
 	void
 	emit_deny()
 	{
-		// retval = IP_FW_DENY;
-		Irb.CreateStore(ConstantInt::get(Int32Ty, IP_FW_DENY), Retval);
-		// l = 0;		/* exit inner loop */
-		Irb.CreateStore(ConstantInt::get(Int32Ty, 0), L);
-		// done = 1;	/* exit outer loop */
-		Irb.CreateStore(ConstantInt::get(Int32Ty, 1), Done);
+		// rule_deny(&l, &done, &retval);
+		Irb.CreateCall(RuleDeny, {L, Done, Retval});
 	}
 
 	void
@@ -2043,7 +1872,7 @@
 	compiler.emit_inner_for_prologue();
 	// Rule to test
 	printf("Testing rule compilation\n");
-	compiler.emit_ip_dst();
+	compiler.emit_forward_mac();
 	printf("emit_inner_for_epilogue()\n");
 	compiler.emit_inner_for_epilogue();
 	printf("emit_outer_for_epilogue()\n");


More information about the svn-soc-all mailing list