/* * Just-In-Time compiler for BPF filters on MIPS * * Copyright (c) 2014 Imagination Technologies Ltd. * Author: Markos Chandras * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; version 2 of the License. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "bpf_jit.h" /* ABI * r_skb_hl SKB header length * r_data SKB data pointer * r_off Offset * r_A BPF register A * r_X BPF register X * r_skb *skb * r_M *scratch memory * r_skb_len SKB length * * On entry (*bpf_func)(*skb, *filter) * a0 = MIPS_R_A0 = skb; * a1 = MIPS_R_A1 = filter; * * Stack * ... * M[15] * M[14] * M[13] * ... * M[0] <-- r_M * saved reg k-1 * saved reg k-2 * ... * saved reg 0 <-- r_sp * * * Packet layout * * <--------------------- len ------------------------> * <--skb-len(r_skb_hl)-->< ----- skb->data_len ------> * ---------------------------------------------------- * | skb->data | * ---------------------------------------------------- */ #define ptr typeof(unsigned long) #define SCRATCH_OFF(k) (4 * (k)) /* JIT flags */ #define SEEN_CALL (1 << BPF_MEMWORDS) #define SEEN_SREG_SFT (BPF_MEMWORDS + 1) #define SEEN_SREG_BASE (1 << SEEN_SREG_SFT) #define SEEN_SREG(x) (SEEN_SREG_BASE << (x)) #define SEEN_OFF SEEN_SREG(2) #define SEEN_A SEEN_SREG(3) #define SEEN_X SEEN_SREG(4) #define SEEN_SKB SEEN_SREG(5) #define SEEN_MEM SEEN_SREG(6) /* SEEN_SK_DATA also implies skb_hl an skb_len */ #define SEEN_SKB_DATA (SEEN_SREG(7) | SEEN_SREG(1) | SEEN_SREG(0)) /* Arguments used by JIT */ #define ARGS_USED_BY_JIT 2 /* only applicable to 64-bit */ #define SBIT(x) (1 << (x)) /* Signed version of BIT() */ /** * struct jit_ctx - JIT context * @skf: The sk_filter * @prologue_bytes: Number of bytes for prologue * @idx: Instruction index * @flags: JIT flags * @offsets: Instruction offsets * @target: Memory location for the compiled filter */ struct jit_ctx { const struct bpf_prog *skf; unsigned int prologue_bytes; u32 idx; u32 flags; u32 *offsets; u32 *target; }; static inline int optimize_div(u32 *k) { /* power of 2 divides can be implemented with right shift */ if (!(*k & (*k-1))) { *k = ilog2(*k); return 1; } return 0; } static inline void emit_jit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx); /* Simply emit the instruction if the JIT memory space has been allocated */ #define emit_instr(ctx, func, ...) \ do { \ if ((ctx)->target != NULL) { \ u32 *p = &(ctx)->target[ctx->idx]; \ uasm_i_##func(&p, ##__VA_ARGS__); \ } \ (ctx)->idx++; \ } while (0) /* * Similar to emit_instr but it must be used when we need to emit * 32-bit or 64-bit instructions */ #define emit_long_instr(ctx, func, ...) \ do { \ if ((ctx)->target != NULL) { \ u32 *p = &(ctx)->target[ctx->idx]; \ UASM_i_##func(&p, ##__VA_ARGS__); \ } \ (ctx)->idx++; \ } while (0) /* Determine if immediate is within the 16-bit signed range */ static inline bool is_range16(s32 imm) { return !(imm >= SBIT(15) || imm < -SBIT(15)); } static inline void emit_addu(unsigned int dst, unsigned int src1, unsigned int src2, struct jit_ctx *ctx) { emit_instr(ctx, addu, dst, src1, src2); } static inline void emit_nop(struct jit_ctx *ctx) { emit_instr(ctx, nop); } /* Load a u32 immediate to a register */ static inline void emit_load_imm(unsigned int dst, u32 imm, struct jit_ctx *ctx) { if (ctx->target != NULL) { /* addiu can only handle s16 */ if (!is_range16(imm)) { u32 *p = &ctx->target[ctx->idx]; uasm_i_lui(&p, r_tmp_imm, (s32)imm >> 16); p = &ctx->target[ctx->idx + 1]; uasm_i_ori(&p, dst, r_tmp_imm, imm & 0xffff); } else { u32 *p = &ctx->target[ctx->idx]; uasm_i_addiu(&p, dst, r_zero, imm); } } ctx->idx++; if (!is_range16(imm)) ctx->idx++; } static inline void emit_or(unsigned int dst, unsigned int src1, unsigned int src2, struct jit_ctx *ctx) { emit_instr(ctx, or, dst, src1, src2); } static inline void emit_ori(unsigned int dst, unsigned src, u32 imm, struct jit_ctx *ctx) { if (imm >= BIT(16)) { emit_load_imm(r_tmp, imm, ctx); emit_or(dst, src, r_tmp, ctx); } else { emit_instr(ctx, ori, dst, src, imm); } } static inline void emit_daddiu(unsigned int dst, unsigned int src, int imm, struct jit_ctx *ctx) { /* * Only used for stack, so the imm is relatively small * and it fits in 15-bits */ emit_instr(ctx, daddiu, dst, src, imm); } static inline void emit_addiu(unsigned int dst, unsigned int src, u32 imm, struct jit_ctx *ctx) { if (!is_range16(imm)) { emit_load_imm(r_tmp, imm, ctx); emit_addu(dst, r_tmp, src, ctx); } else { emit_instr(ctx, addiu, dst, src, imm); } } static inline void emit_and(unsigned int dst, unsigned int src1, unsigned int src2, struct jit_ctx *ctx) { emit_instr(ctx, and, dst, src1, src2); } static inline void emit_andi(unsigned int dst, unsigned int src, u32 imm, struct jit_ctx *ctx) { /* If imm does not fit in u16 then load it to register */ if (imm >= BIT(16)) { emit_load_imm(r_tmp, imm, ctx); emit_and(dst, src, r_tmp, ctx); } else { emit_instr(ctx, andi, dst, src, imm); } } static inline void emit_xor(unsigned int dst, unsigned int src1, unsigned int src2, struct jit_ctx *ctx) { emit_instr(ctx, xor, dst, src1, src2); } static inline void emit_xori(ptr dst, ptr src, u32 imm, struct jit_ctx *ctx) { /* If imm does not fit in u16 then load it to register */ if (imm >= BIT(16)) { emit_load_imm(r_tmp, imm, ctx); emit_xor(dst, src, r_tmp, ctx); } else { emit_instr(ctx, xori, dst, src, imm); } } static inline void emi
#!/bin/bash
# Copyright 2015 Futurewei Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# A copy of the license is included with this distribution. If you did not
# recieve a copy you may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

which dpkg-architecture 2>&1 1>/dev/null
if [ $? -ne 0 ]; then
	echo "Can't locate dpkg-architecture"
	echo "try 'sudo apt-get install dpkg-dev"
	exit 1
fi
KERNEL_VERSION=${KERNEL_VERSION-`uname -r`}
PACKAGE_NAME=l2fwd
PACKAGE_ARCH=`dpkg-architecture -qDEB_BUILD_ARCH`
GIT_VERSION=`git ls-remote 2>/dev/null | awk '{print $2}' | sed 's/\// /g' | sort -n -k4| awk '/[0-9]+/{print $(NF-1)"-"$NF}' | tail -n -1`
PACKAGE_VERSION="1.0.$GIT_VERSION-${KERNEL_VERSION}"

MODULE_NAME=l2fwd
PACKAGE_DEPENDS=linux-image-${KERNEL_VERSION}

rm -rf  ${PACKAGE_NAME}-${PACKAGE_VERSION}
mkdir -p ${PACKAGE_NAME}-${PACKAGE_VERSION}/DEBIAN
# put this in with pktgen
mkdir -p ${PACKAGE_NAME}-${PACKAGE_VERSION}/lib/modules/${KERNEL_VERSION}/kernel/net/core/
cat >>${PACKAGE_NAME}-${PACKAGE_VERSION}/DEBIAN/control <<EOF
Package:  ${PACKAGE_NAME}
Version: ${PACKAGE_VERSION}
Section: kernel
Priority: optional
Architecture: ${PACKAGE_ARCH}
Maintainer: eugene.snider@huawei.com
Depends: ${PACKAGE_DEPENDS}
Description: simple l2 fowarding driver
    This package provides the l2fwd driver
EOF
cat >>${PACKAGE_NAME}-${PACKAGE_VERSION}/DEBIAN/preinst <<EOF
#!/bin/bash
rmmod ${MODULE_NAME}
if [ upgrade != "$1" ] || dpkg --compare-versions "$2" lt ${PACKAGE_VERSION}; then
    dpkg-divert --package ${PACKAGE_NAME} --add --rename \
        --divert  /lib/modules/${KERNEL_VERSION}/kernel/net/core/${MODULE_NAME}.ko.dist  /lib/modules/${KERNEL_VERSION}/kernel/net/core/${MODULE_NAME}.ko
fi
EOF
chmod 555 ${PACKAGE_NAME}-${PACKAGE_VERSION}/DEBIAN/preinst
cat >>${PACKAGE_NAME}-${PACKAGE_VERSION}/DEBIAN/postinst <<EOF
#!/bin/bash
/sbin/depmod -a `uname -r`
EOF
chmod 555 ${PACKAGE_NAME}-${PACKAGE_VERSION}/DEBIAN/postinst
cat >>${PACKAGE_NAME}-${PACKAGE_VERSION}/DEBIAN/postrm <<EOF
#!/bin/bash
rmmod ${MODULE_NAME}
if [ upgrade != "$1" ] || dpkg --compare-versions "$2" lt ${PACKAGE_VERSION}; then
    dpkg-divert --package ${PACKAGE_NAME} --remove --rename \
        --divert  /lib/modules/${KERNEL_VERSION}/kernel/net/core/${MODULE_NAME}.ko.dist  /lib/modules/${KERNEL_VERSION}/kernel/net/core/${MODULE_NAME}.ko
fi
EOF
chmod 555 ${PACKAGE_NAME}-${PACKAGE_VERSION}/DEBIAN/postrm
cp -p ${MODULE_NAME}.ko ${PACKAGE_NAME}-${PACKAGE_VERSION}/lib/modules/${KERNEL_VERSION}/kernel/net/core/${MODULE_NAME}.ko
sudo dpkg-deb --build ${PACKAGE_NAME}-${PACKAGE_VERSION}
sudo rm -rf ${PACKAGE_NAME}-${PACKAGE_VERSION}
tx); } else { /* X */ ctx->flags |= SEEN_A | SEEN_X; emit_sltu(r_s0, r_A, r_X, ctx); } /* A < (K|X) ? r_scrach = 1 */ b_off = b_imm(i + inst->jf + 1, ctx); emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off, ctx); emit_nop(ctx); /* A > (K|X) ? scratch = 0 */ if (condt & MIPS_COND_GT) { /* Checking for equality */ ctx->flags |= SEEN_A | SEEN_X; if (condt & MIPS_COND_K) emit_load_imm(r_s0, k, ctx); else emit_jit_reg_move(r_s0, r_X, ctx); b_off = b_imm(i + inst->jf + 1, ctx); emit_bcond(MIPS_COND_EQ, r_A, r_s0, b_off, ctx); emit_nop(ctx); /* Finally, A > K|X */ b_off = b_imm(i + inst->jt + 1, ctx); emit_b(b_off, ctx); emit_nop(ctx); } else { /* A >= (K|X) so jump */ b_off = b_imm(i + inst->jt + 1, ctx); emit_b(b_off, ctx); emit_nop(ctx); } } else { /* A == K|X */ if (condt & MIPS_COND_K) { /* K */ ctx->flags |= SEEN_A; emit_load_imm(r_s0, k, ctx); /* jump true */ b_off = b_imm(i + inst->jt + 1, ctx); emit_bcond(MIPS_COND_EQ, r_A, r_s0, b_off, ctx); emit_nop(ctx); /* jump false */ b_off = b_imm(i + inst->jf + 1, ctx); emit_bcond(MIPS_COND_NE, r_A, r_s0, b_off, ctx); emit_nop(ctx); } else { /* X */ /* jump true */ ctx->flags |= SEEN_A | SEEN_X; b_off = b_imm(i + inst->jt + 1, ctx); emit_bcond(MIPS_COND_EQ, r_A, r_X, b_off, ctx); emit_nop(ctx); /* jump false */ b_off = b_imm(i + inst->jf + 1, ctx); emit_bcond(MIPS_COND_NE, r_A, r_X, b_off, ctx); emit_nop(ctx); } } break; case BPF_JMP | BPF_JSET | BPF_K: ctx->flags |= SEEN_A; /* pc += (A & K) ? pc -> jt : pc -> jf */ emit_load_imm(r_s1, k, ctx); emit_and(r_s0, r_A, r_s1, ctx); /* jump true */ b_off = b_imm(i + inst->jt + 1, ctx); emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off, ctx); emit_nop(ctx); /* jump false */ b_off = b_imm(i + inst->jf + 1, ctx); emit_b(b_off, ctx); emit_nop(ctx); break; case BPF_JMP | BPF_JSET | BPF_X: ctx->flags |= SEEN_X | SEEN_A; /* pc += (A & X) ? pc -> jt : pc -> jf */ emit_and(r_s0, r_A, r_X, ctx); /* jump true */ b_off = b_imm(i + inst->jt + 1, ctx); emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off, ctx); emit_nop(ctx); /* jump false */ b_off = b_imm(i + inst->jf + 1, ctx); emit_b(b_off, ctx); emit_nop(ctx); break; case BPF_RET | BPF_A: ctx->flags |= SEEN_A; if (i != prog->len - 1) /* * If this is not the last instruction * then jump to the epilogue */ emit_b(b_imm(prog->len, ctx), ctx); emit_reg_move(r_ret, r_A, ctx); /* delay slot */ break; case BPF_RET | BPF_K: /* * It can emit two instructions so it does not fit on * the delay slot. */ emit_load_imm(r_ret, k, ctx); if (i != prog->len - 1) { /* * If this is not the last instruction * then jump to the epilogue */ emit_b(b_imm(prog->len, ctx), ctx); emit_nop(ctx); } break; case BPF_MISC | BPF_TAX: /* X = A */ ctx->flags |= SEEN_X | SEEN_A; emit_jit_reg_move(r_X, r_A, ctx); break; case BPF_MISC | BPF_TXA: /* A = X */ ctx->flags |= SEEN_A | SEEN_X; emit_jit_reg_move(r_A, r_X, ctx); break; /* AUX */ case BPF_ANC | SKF_AD_PROTOCOL: /* A = ntohs(skb->protocol */ ctx->flags |= SEEN_SKB | SEEN_OFF | SEEN_A; BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2); off = offsetof(struct sk_buff, protocol); emit_half_load(r_A, r_skb, off, ctx); #ifdef CONFIG_CPU_LITTLE_ENDIAN /* This needs little endian fixup */ if (cpu_has_wsbh) { /* R2 and later have the wsbh instruction */ emit_wsbh(r_A, r_A, ctx); } else { /* Get first byte */ emit_andi(r_tmp_imm, r_A, 0xff, ctx); /* Shift it */ emit_sll(r_tmp, r_tmp_imm, 8, ctx); /* Get second byte */ emit_srl(r_tmp_imm, r_A, 8, ctx); emit_andi(r_tmp_imm, r_tmp_imm, 0xff, ctx); /* Put everyting together in r_A */ emit_or(r_A, r_tmp, r_tmp_imm, ctx); } #endif break; case BPF_ANC | SKF_AD_CPU: ctx->flags |= SEEN_A | SEEN_OFF; /* A = current_thread_info()->cpu */ BUILD_BUG_ON(FIELD_SIZEOF(struct thread_info, cpu) != 4); off = offsetof(struct thread_info, cpu); /* $28/gp points to the thread_info struct */ emit_load(r_A, 28, off, ctx); break; case BPF_ANC | SKF_AD_IFINDEX: /* A = skb->dev->ifindex */ ctx->flags |= SEEN_SKB | SEEN_A; off = offsetof(struct sk_buff, dev); /* Load *dev pointer */ emit_load_ptr(r_s0, r_skb, off, ctx); /* error (0) in the delay slot */ emit_bcond(MIPS_COND_EQ, r_s0, r_zero, b_imm(prog->len, ctx), ctx); emit_reg_move(r_ret, r_zero, ctx); BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4); off = offsetof(struct net_device, ifindex); emit_load(r_A, r_s0, off, ctx); break; case BPF_ANC | SKF_AD_MARK: ctx->flags |= SEEN_SKB | SEEN_A; BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4); off = offsetof(struct sk_buff, mark); emit_load(r_A, r_skb, off, ctx); break; case BPF_ANC | SKF_AD_RXHASH: ctx->flags |= SEEN_SKB | SEEN_A; BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4); off = offsetof(struct sk_buff, hash); emit_load(r_A, r_skb, off, ctx); break; case BPF_ANC | SKF_AD_VLAN_TAG: case BPF_ANC | SKF_AD_VLAN_TAG_PRESENT: ctx->flags |= SEEN_SKB | SEEN_A; BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2); off = offsetof(struct sk_buff, vlan_tci); emit_half_load(r_s0, r_skb, off, ctx); if (code == (BPF_ANC | SKF_AD_VLAN_TAG)) { emit_andi(r_A, r_s0, (u16)~VLAN_TAG_PRESENT, ctx); } else { emit_andi(r_A, r_s0, VLAN_TAG_PRESENT, ctx); /* return 1 if present */ emit_sltu(r_A, r_zero, r_A, ctx); } break; case BPF_ANC | SKF_AD_PKTTYPE: ctx->flags |= SEEN_SKB; emit_load_byte(r_tmp, r_skb, PKT_TYPE_OFFSET(), ctx); /* Keep only the last 3 bits */ emit_andi(r_A, r_tmp, PKT_TYPE_MAX, ctx); #ifdef __BIG_ENDIAN_BITFIELD /* Get the actual packet type to the lower 3 bits */ emit_srl(r_A, r_A, 5, ctx); #endif break; case BPF_ANC | SKF_AD_QUEUE: ctx->flags |= SEEN_SKB | SEEN_A; BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2); BUILD_BUG_ON(offsetof(struct sk_buff, queue_mapping) > 0xff); off = offsetof(struct sk_buff, queue_mapping); emit_half_load(r_A, r_skb, off, ctx); break; default: pr_debug("%s: Unhandled opcode: 0x%02x\n", __FILE__, inst->code); return -1; } } /* compute offsets only during the first pass */ if (ctx->target == NULL) ctx->offsets[i] = ctx->idx * 4; return 0; } int bpf_jit_enable __read_mostly; void bpf_jit_compile(struct bpf_prog *fp) { struct jit_ctx ctx; unsigned int alloc_size, tmp_idx; if (!bpf_jit_enable) return; memset(&ctx, 0, sizeof(ctx)); ctx.offsets = kcalloc(fp->len, sizeof(*ctx.offsets), GFP_KERNEL); if (ctx.offsets == NULL) return; ctx.skf = fp; if (build_body(&ctx)) goto out; tmp_idx = ctx.idx; build_prologue(&ctx); ctx.prologue_bytes = (ctx.idx - tmp_idx) * 4; /* just to complete the ctx.idx count */ build_epilogue(&ctx); alloc_size = 4 * ctx.idx; ctx.target = module_alloc(alloc_size); if (ctx.target == NULL) goto out; /* Clean it */ memset(ctx.target, 0, alloc_size); ctx.idx = 0; /* Generate the actual JIT code */ build_prologue(&ctx); build_body(&ctx); build_epilogue(&ctx); /* Update the icache */ flush_icache_range((ptr)ctx.target, (ptr)(ctx.target + ctx.idx)); if (bpf_jit_enable > 1) /* Dump JIT code */ bpf_jit_dump(fp->len, alloc_size, 2, ctx.target); fp->bpf_func = (void *)ctx.target; fp->jited = 1; out: kfree(ctx.offsets); } void bpf_jit_free(struct bpf_prog *fp) { if (fp->jited) module_memfree(fp->bpf_func); bpf_prog_unlock_free(fp); }