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

dpl at FreeBSD.org dpl at FreeBSD.org
Thu Sep 11 10:15:16 UTC 2014


Author: dpl
Date: Thu Sep 11 10:15:15 2014
New Revision: 273962
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=273962

Log:
  Added a test function to test the compilation of rules, corrected errors, and added the _frag, _in, and _layer2 functions, whose compilation is already tested.

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

Modified: soc2014/dpl/netmap-ipfwjit/sys/netpfil/ipfw/jit.cc
==============================================================================
--- soc2014/dpl/netmap-ipfwjit/sys/netpfil/ipfw/jit.cc	Thu Sep 11 10:13:18 2014	(r273961)
+++ soc2014/dpl/netmap-ipfwjit/sys/netpfil/ipfw/jit.cc	Thu Sep 11 10:15:15 2014	(r273962)
@@ -81,6 +81,7 @@
 	// Loop control.
 	Value *Match;
 	Value *L;
+	Value *M;
 	Value *Done;
 	Value *FPos;
 	Value *Retval;
@@ -228,22 +229,41 @@
 
 		// Get StrucType from bitcode.
 		MbufTy = mod->getTypeByName("struct.mbuf");
+		if (MbufTy == NULL)
+			err(1, "bitcode fault: struct.mbuf");
 		IfnetTy = mod->getTypeByName("struct.ifnet");
+		if (IfnetTy == NULL)
+			err(1, "bitcode fault: struct.ifnet");
 		In_addrTy = mod->getTypeByName("struct.in_addr");
+		if (In_addrTy == NULL)
+			err(1, "bitcode fault: struct.in_addr");
 		IpTy = mod->getTypeByName("struct.ip");
+		if (IpTy == NULL)
+			err(1, "bitcode fault: struct.ip");
 		Ip_fw_argsTy = mod->getTypeByName("struct.ip_fw_args");
+		if (Ip_fw_argsTy == NULL)
+			err(1, "bitcode fault: struct.ip_fw_args");
 		Ip_fw_chainTy = mod->getTypeByName("struct.ip_fw_chain");
+		if (Ip_fw_chainTy == NULL)
+			err(1, "bitcode fault: struct.ip_fw_chain");
 		Ip_fwTy = mod->getTypeByName("struct.ip_fw");
+		if (Ip_fwTy == NULL)
+			err(1, "bitcode fault: struct.ip_fw");
 		Ipfw_insnTy = mod->getTypeByName("struct._ipfw_insn");
+		if (Ipfw_insnTy == NULL)
+			err(1, "bitcode fault: struct._ipfw_insn");
 		IpfwInsnU16Ty = mod->getTypeByName("struct._ipfw_insn_u16");
+		if (IpfwInsnU16Ty == NULL)
+			err(1, "bitcode fault: struct._ipfw_insn_u16");
 		IpfwInsnIpTy = mod->getTypeByName("struct._ipfw_insn_ip");
+		if (IpfwInsnIpTy == NULL)
+			err(1, "bitcode fault: struct._ipfw_insn_ip");
 		Ipfw_insn_ifTy = mod->getTypeByName("struct._ipfw_insn_if");
+		if (Ipfw_insn_ifTy == NULL)
+			err(1, "bitcode fault: struct._ipfw_insn_if");
 		Ipfw_dyn_ruleTy = mod->getTypeByName("struct._ipfw_dyn_rule");
-#ifdef __FreeBSD__
-		UcredTy = mod->getTypeByName("struct.ucred");
-#else
-		UcredTy = mod->getTypeByName("struct.bsd_ucred");
-#endif /* __FreeBSD__ */
+		if (Ipfw_dyn_ruleTy == NULL)
+			err(1, "bitcode fault: struct._ipfw_dyn_rule");
 
 		// Create Pointer to StructType types.
 		MbufPtrTy = PointerType::getUnqual(MbufTy);
@@ -258,35 +278,69 @@
 		IpfwInsnIpPtrTy = PointerType::getUnqual(IpfwInsnIpTy);
 		Ipfw_insn_ifPtrTy = PointerType::getUnqual(Ipfw_insn_ifTy);
 		Ipfw_dyn_rulePtrTy = PointerType::getUnqual(Ipfw_dyn_ruleTy);
-#ifdef __FreeBSD__
-		UcredPtrTy = PointerType::getUnqual(UcredTy);
-#endif
+
 		// Get Function defs from bitcode.
 		// All of them are auxiliary functions.
 		InspectPkt = mod->getFunction("inspect_pkt");
+		if (InspectPkt == NULL)
+			err(1, "bitcode fault: inspect_pkt");
 		IsIcmpQuery = mod->getFunction("is_icmp_query");
+		if (IsIcmpQuery == NULL)
+			err(1, "bitcode fault: is_icmp_query");
 		FlagsMatch = mod->getFunction("flags_match");
+		if (FlagsMatch == NULL)
+			err(1, "bitcode fault: flags_match");
 		IpoptsMatch = mod->getFunction("ipopts_match");
+		if (IpoptsMatch == NULL)
+			err(1, "bitcode fault: ipopts_match");
 		TcpoptsMatch = mod->getFunction("tcpopts_match");
-		IfaceMatch = mod->getFunction("ifaceMatch");
+		if (TcpoptsMatch == NULL)
+			err(1, "bitcode fault: tcpopts_match");
+		IfaceMatch = mod->getFunction("iface_match");
+		if (IfaceMatch == NULL)
+			err(1, "bitcode fault: iface_match");
 		VerifyPath = mod->getFunction("verify_path");
+		if (VerifyPath == NULL)
+			err(1, "bitcode fault: verify_path");
 
 #ifdef INET6
 		Icmp6typeMatch = mod->getFunction("icmp6type_match");
+		if (Icmp6typeMatch == NULL)
+			err(1, "bitcode fault: icmp6type_match");
 		SearchIp6AddrNet = mod->getFunction("search_ip6_addr_net");
+		if (SearchIp6AddrNet == NULL)
+			err(1, "bitcode fault: search_ip6_addr_net");
 		Flow6idMatch = mod->getFunction("flow6id_match");
+		if (Flow6idMatch == NULL)
+			err(1, "bitcode fault: flow6id_match");
 		VerifyPath6 = mod->getFunction("verify_path6");
+		if (VerifyPath6 == NULL)
+			err(1, "bitcode fault: verify_path6");
 		IsIcmp6Query = mod->getFunction("is_icmp6_query");
+		if (IsIcmp6Query == NULL)
+			err(1, "bitcode fault: is_icmp6_query");
 		SendReject6 = mod->getFunction("send_reject6");
+		if (SendReject6 == NULL)
+			err(1, "bitcode fault: send_reject6");
 #endif /* INET6 */
 
 		SendReject = mod->getFunction("send_reject");
+		if (SendReject == NULL)
+			err(1, "bitcode fault: send_reject");
 		SetMatch = mod->getFunction("set_match");
+		if (SetMatch == NULL)
+			err(1, "bitcode fault: set_match");
 		JumpFast = mod->getFunction("jump_fast");
+		if (JumpFast == NULL)
+			err(1, "bitcode fault: jump_fast");
 
 		// Functions declared at bitcode.
 		PrintfFunc = mod->getFunction("printf");
+		if (PrintfFunc == NULL)
+			err(1, "bitcode fault: printf");
 		IpfwFindRule = mod->getFunction("ipfw_find_rule");
+		if (IpfwFindRule == NULL)
+			err(1, "bitcode fault: ipfw_find_rule");
 	}
 
 	// Allocate and initialize LLVM vars.
@@ -310,7 +364,7 @@
 		// m = args->m (idx: 0)
 		MPtr = Irb.CreateAlloca(MbufPtrTy, nullptr, "m");
 		Irb.CreateStore(Irb.CreateLoad(Irb.CreateStructGEP(Args, 0)), MPtr);
-		Value *M = Irb.CreateLoad(MPtr);
+		M = Irb.CreateLoad(MPtr);
 
 		// ip = (struct ip *)((m)->m_data) (idx: 2)
 		IpPtr = Irb.CreateAlloca(IpPtrTy, nullptr, "ip");
@@ -475,11 +529,6 @@
 		Value *Slot = Irb.CreateStructGEP(Rule, 0);
 		Value *SlotValue = Irb.CreateLoad(Slot);
 		Comp = Irb.CreateICmpEQ(SlotValue, ConstantInt::get(Int32Ty, 0));
-		// if (1) {
-		// 	Value *Str = Irb.CreateGlobalString("args->rule.slot: %d\n");
-		// 	Value *StrFirstElement = Irb.CreateStructGEP(Str, 0);
-		// 	Irb.CreateCall(PrintfFunc, {StrFirstElement});
-		// }
 		Irb.CreateCondBr(Comp, Nottagged, Tagged);
 
 		Irb.SetInsertPoint(Tagged);
@@ -512,13 +561,6 @@
 		// else f_pos = 0;
 		// Since f_pos is initialized by default as 0, we only br.
 		Irb.SetInsertPoint(Nottagged);
-		// XXX Fpos
-		// if (1) {
-		// 	Value *Str = Irb.CreateGlobalString("f_pos: %d\n&fpos: %p\n");
-		// 	Value *StrFirstElement = Irb.CreateStructGEP(Str, 0);
-		// 	Irb.CreateCall(PrintfFunc, {StrFirstElement, FPos, FPos});
-		// }
-		// Jump to first rule.
 		Irb.CreateBr(rules.front());
 	}
 
@@ -529,7 +571,11 @@
 		mod = loadBitcode("rules.bc");
 
 		Func = mod->getFunction("ipfw_chk_jit");
+		if (Func == NULL)
+			err(1, "bitcode fault: ipfw_chk_jit");
+
 		Func->setLinkage(GlobalValue::ExternalLinkage);
+
 		// Create static BasicBlocks.
 		// The entry basic block contains all the initialization 
 		// and allocation of resources, and a basic check done 
@@ -593,8 +639,6 @@
 			exit(1);
 		}
 
-		// mod->dump();
-		
 		// XXX We should use a NON deperecated function.
 		return (funcptr)EE->getPointerToFunction(mod->getFunction("ipfw_chk_jit"));
 	}
@@ -937,30 +981,30 @@
 
 
 	// Rules
-	// XXX Not tested.
+	// XXX Exec not tested.
 	void
 	emit_nop()
 	{
 		Irb.CreateStore(ConstantInt::get(Int32Ty, 1), Match);
 	}
 
-	// XXX Not tested.
+	// XXX Exec not tested.
 	void
-	emit_forward_mac(u_int8_t opcode)
+	emit_forward_mac()
 	{
 		printf("Compilation error:\n");
-		printf("ipfwjitter: opcode %d unimplemented\n", opcode);
+		printf("ipfwjitter: MAC forwarding unimplemented\n");
 		printf("Compilation continues.\n");
 	}
 
-	// XXX Not tested.
+	// XXX Exec not tested.
 	void
 	emit_jail()
 	{
-		BasicBlock *OffsetNZ = BasicBlock::Create(Con, "offsetnotzero", Func);
-		BasicBlock *OffsetZE = BasicBlock::Create(Con, "offsetiszero", Func);
-		BasicBlock *TCPorUDP = BasicBlock::Create(Con, "setmatchzero", Func);
-		BasicBlock *Continue = BasicBlock::Create(Con, "Continue", Func);
+		BasicBlock *OffsetNZ = BasicBlock::Create(Con, "R_offsetnotzero", Func);
+		BasicBlock *OffsetZE = BasicBlock::Create(Con, "R_offsetiszero", Func);
+		BasicBlock *TCPorUDP = BasicBlock::Create(Con, "R_setmatchzero", Func);
+		BasicBlock *Continue = BasicBlock::Create(Con, "R_Continue", Func);
 		Value *Comp;
 
 		// if (offset != 0)
@@ -984,8 +1028,8 @@
 		//		*match = 0;
 		Irb.SetInsertPoint(OffsetZE);
 		Value *ProtoL = Irb.CreateLoad(Proto);
-		Comp = Irb.CreateICmpEQ(OffsetL, ConstantInt::get(Int8Ty, IPPROTO_TCP));
-		Value *Comp2 = Irb.CreateICmpEQ(OffsetL, ConstantInt::get(Int8Ty, IPPROTO_UDP));
+		Comp = Irb.CreateICmpEQ(OffsetL, ConstantInt::get(OffsetL->getType(), IPPROTO_TCP));
+		Value *Comp2 = Irb.CreateICmpEQ(OffsetL, ConstantInt::get(OffsetL->getType(), IPPROTO_UDP));
 		Irb.CreateCondBr(Comp, TCPorUDP, Continue);
 		Irb.CreateCondBr(Comp2, TCPorUDP, Continue);
 
@@ -997,87 +1041,120 @@
 		Irb.SetInsertPoint(Continue);
 	}
 
-	// XXX Not tested.
+	// XXX Exec not tested.
 	void
 	emit_recv()
 	{
 		//*match = iface_match(m->m_pkthdr.rcvif, (ipfw_insn_if *)cmd, chain, tablearg);
-		Value *rcvif = Irb.CreateStructGEP(Irb.CreateStructGEP(Irb.CreateLoad(MPtr), 5), 0);
-		Value *cmdc = Irb.CreateBitCast(Cmd, Ipfw_insn_ifPtrTy);
-		Value *IfaceMatchCall = Irb.CreateCall4(IfaceMatch, rcvif, cmdc, Chain, Tablearg);
+		Value *MPkthdr = Irb.CreateStructGEP(M, 5);
+		Value *Rcvif = Irb.CreateStructGEP(MPkthdr, 0);
+		Value *RcvifL = Irb.CreateLoad(Rcvif);
+		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);
 	}
 
-	// XXX Not tested.
+	// XXX Exec not tested.
 	void
 	emit_xmit()
 	{
 		//*match = iface_match(oif, (ipfw_insn_if *)cmd, chain, tablearg);
-		Value *Cmdc = Irb.CreateBitCast(Cmd, Ipfw_insn_ifPtrTy);
+		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);
 	}
 
-	// XXX Not tested.
+	// XXX Exec not tested.
 	void
 	emit_via()
 	{
-		BasicBlock *OifNZ = BasicBlock::Create(Con, "OifNotZero", Func);
-		BasicBlock *OifZE = BasicBlock::Create(Con, "OifIsZero", Func);
+		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);
-		// break;
-		//Value *Comp = Irb.CreateICmp(Oif, ConstantPointerNull::get(IfnetPtrTy));
+		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);
 	}
 
-	// XXX Not tested.
 	void
 	emit_macaddr2()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_mac_type()
 	{
 	}
 
-	// XXX Not tested.
+
+	// XXX Exec not tested.
 	void
 	emit_frag()
 	{
+		// match = (offset != 0);
+		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);
 	}
 
-	// XXX Not tested.
+	// 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);
 	}
 
-	// XXX Not tested.
+	// 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);
 	}
 
-	// XXX Not tested.
 	void
 	emit_diverted()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_proto()
 	{
 	}
 
-	// XXX Not tested.
+	// XXX Exec not tested.
 	void
 	emit_ip_src()
 	{
@@ -1086,69 +1163,65 @@
 		// 	src_ip.s_addr);
 
 		// ((ipfw_insn_ip *)cmd)->addr.s_addr
-		// addr only has one element (s_adddr) we only need one GEP.
-		Value *CmdL = Irb.CreateLoad(Cmd);
-		Value *CmdLBitC = Irb.CreateBitCast(CmdL, IpfwInsnIpPtrTy);
-		Value *CmdSAddr = Irb.CreateStructGEP(CmdLBitC, 1);
+		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(SrcIpL, 0);
-		Value *Comp = Irb.CreateICmpEQ(CmdSAddrL, SrcAddr);
-
-		Value *NewMatch = Irb.CreateAnd(Comp, IsIpv4);
+		//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);
+
+		Value *IsIpv4L = Irb.CreateLoad(IsIpv4);
+		Value *Comp32 = Irb.CreateZExt(Comp, IsIpv4L->getType());
+		Value *NewMatch = Irb.CreateAnd(Comp32, IsIpv4L);
+		Irb.CreateStore(NewMatch, Match);
 	}
 
-	// XXX Not tested.
 	void
 	emit_ip_dst_lookup()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_ip_dst_mask()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_ip_src_me()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_ip6_src_me()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_ip_src_set()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_ip_dst()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_ip_dst_me()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_ip6_dst_me()
 	{
 	}
 
-	// XXX Not tested.
+	// XXX Exec not tested.
 	void
 	emit_ip_dstport()
 	{
@@ -1183,19 +1256,22 @@
 		Value *P = Irb.CreateAlloca(Int16PtrTy, nullptr, "p");
 		Value *I = Irb.CreateAlloca(Int32Ty, nullptr, "i");
 		// p = ((ipfw_insn_u16 *)cmd)->ports;
-		// XXX TODO Ensure correctness.
 		Value *CmdLBitC = Irb.CreateBitCast(Cmd, IpfwInsnU16PtrTy);
 		Value *Ports = Irb.CreateStructGEP(CmdLBitC, 1);
-		Irb.CreateStore(Ports, P);
+		Value *PortsGEP = Irb.CreateStructGEP(Ports, 0);
+		Irb.CreateStore(PortsGEP, P);
+		// New p
 		Value *PL = Irb.CreateLoad(P);
 
 		// (proto == IPPROTO_UDP || proto == IPPROTO_TCP)
-		Value *Comp1 = Irb.CreateICmpEQ(Proto, ConstantInt::get(Proto->getType(), IPPROTO_TCP));
-		Value *Comp2 = Irb.CreateICmpEQ(Proto, ConstantInt::get(Proto->getType(), IPPROTO_UDP));
+		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 *Comp3 = Irb.CreateICmpEQ(Offset, ConstantInt::get(Offset->getType(), 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);
@@ -1208,22 +1284,25 @@
 		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;
-		Irb.CreateStore(SrcPort, X);
+		Value *SrcPortL = Irb.CreateLoad(SrcPort);
+		Irb.CreateStore(SrcPortL, X);
 		Irb.CreateBr(Loop);
 
 		Irb.SetInsertPoint(Dst);
 		// 	u_int16_t x = dst_port;
-		Irb.CreateStore(DstPort, X);
+		Value *DstPortL = Irb.CreateLoad(DstPort);
+		Irb.CreateStore(DstPortL, X);
 		Irb.CreateBr(Loop);
 
 		Irb.SetInsertPoint(Loop);
 		// Loop initialisation
 		// i = cmdlen - 1;
 		// cmdlen: signed
-		Value *Sub = Irb.CreateNSWSub(CmdLen, ConstantInt::get(CmdLen->getType(), 1));
+		Value *CmdLenL = Irb.CreateLoad(CmdLen);
+		Value *Sub = Irb.CreateNSWSub(CmdLenL, ConstantInt::get(CmdLenL->getType(), 1));
 		Irb.CreateStore(Sub, I);
 		Irb.CreateBr(ContLoop);
 
@@ -1232,23 +1311,24 @@
 		// while((!match) && (i>0)) {
 		Value *IL = Irb.CreateLoad(I);
 		Value *MatchL = Irb.CreateLoad(Match);
-		Comp1 = Irb.CreateICmpEQ(MatchL, ConstantInt::get(Match->getType(), 0));
-		Comp2 = Irb.CreateICmpSGT(IL, ConstantInt::get(I->getType(), 0));
+		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.CreateStructGEP(PL, 0);
-		Value *PO = Irb.CreateStructGEP(PL, 1);
+		// FIXME
+		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, Match->getType());
+		Value *Comp32 = Irb.CreateSExt(Comp, MatchL->getType());
 		Irb.CreateStore(Comp32, Match);
 
 		// Increment, decrement.
 		// 	i--;
-		Value *ILD = Irb.CreateNSWSub(IL, ConstantInt::get(I->getType(), 1));
+		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));
@@ -1258,199 +1338,166 @@
 		Irb.SetInsertPoint(Out);
 	}
 
-	// XXX Not tested.
 	void
 	emit_icmptype()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_icmp6type()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_ipopt()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_ipver()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_ipttl()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_ipprecedence()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_iptos()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_dscp()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_tcpdatalen()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_tcpflags()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_tcpopts()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_tcpseq()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_tcpack()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_tcpwin()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_estab()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_altq()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_log()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_prob()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_verrevpath()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_versrcreach()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_antispoof()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_ipsec()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_ip6_src()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_ip6_dst()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_ip6_dst_mask()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_flow6id()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_ext_hdr()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_ip6()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_ip4()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_tag()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_fib()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_sockarg()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_tagged()
 	{
@@ -1498,13 +1545,11 @@
 	 *   l=0, cmdlen=0.
 	 */
 
-	// XXX Not tested.
 	void
 	emit_keep_state()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_check_state()
 	{
@@ -1522,49 +1567,64 @@
 		Irb.CreateStore(ConstantInt::get(Int32Ty, 1), Done);
 	}
 
-	// XXX Not tested.
 	void
 	emit_queue()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_tee()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_count()
 	{
 	}
 
-	// XXX Not tested.
+	// TODO
 	void
 	emit_skipto()
 	{
+		// IPFW_INC_RULE_COUNTER(f, pktlen);
+		// f_pos = jump_fast(chain, f, cmd->arg1, tablearg, 0);
+		// /*
+		//  * Skip disabled rules, and re-enter
+		//  * the inner loop with the correct
+		//  * f_pos, f, l and cmd.
+		//  * Also clear cmdlen and skip_or
+		//  */
+		// for (; f_pos < chain->n_rules - 1 &&
+		//	   (V_set_disable &
+		//	    (1 << chain->map[f_pos]->set));
+		//	   f_pos++)
+		// ;
+		// /* Re-enter the inner loop at the skipto rule. */
+		// f = chain->map[f_pos];
+		// l = f->cmd_len;
+		// cmd = f->cmd;
+		// match = 1;
+		// cmdlen = 0;
+		// skip_or = 0;
+		// continue;
 	}
 
-	// XXX Not tested.
 	void
 	emit_callreturn()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_reject()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_unreach6()
 	{
 	}
 
-	// XXX Not tested.
+	// XXX Exec not tested.
 	void
 	emit_deny()
 	{
@@ -1576,49 +1636,66 @@
 		Irb.CreateStore(ConstantInt::get(Int32Ty, 1), Done);
 	}
 
-	// XXX Not tested.
 	void
 	emit_forward_ip()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_forward_ip6()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_ngtee()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_setfib()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_setdscp()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_nat()
 	{
 	}
 
-	// XXX Not tested.
 	void
 	emit_reass()
 	{
 	}
 };
 
+// Function to test compilation code.
+// Filtering code has to be tested by real usage.
+void
+test_compilation()
+{
+	printf("Creating object\n");
+	ipfwJIT compiler(1);
+	printf("emit_outer_for_prologue()\n");
+	compiler.emit_outer_for_prologue();
+	printf("emit_inner_for_prologue()\n");
+	compiler.emit_inner_for_prologue();
+	// Rule to test
+	printf("testing rule\n");
+	compiler.emit_layer2();
+	printf("emit_inner_for_epilogue()\n");
+	compiler.emit_inner_for_epilogue();
+	printf("emit_outer_for_epilogue()\n");
+	compiler.emit_outer_for_epilogue();
+	compiler.end_rule();
+	printf("emit_end()\n");
+	compiler.emit_end();
+	err(1, "Compilation");
+}
+
 extern "C" funcptr
 compile_code(struct ip_fw_args *args, struct ip_fw_chain *chain)
 {
@@ -1628,6 +1705,8 @@
 	if (chain->n_rules == 0)
 		return (NULL);
 
+	// test_compilation();
+
 	ipfwJIT compiler(chain->n_rules);
 
 	// Iterate through the rules.
@@ -1656,8 +1735,9 @@
 				compiler.emit_nop();
 				break;
 
+			// XXX Not implemented in netmap-ipfw
 			case O_FORWARD_MAC:
-				compiler.emit_forward_mac(cmd->opcode);
+				compiler.emit_forward_mac();
 				break;
 
 			case O_GID:


More information about the svn-soc-all mailing list