target.py 5.68 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
# Copyright (c) Barefoot Networks, Inc.
# Licensed under the Apache License, Version 2.0 (the "License")

from programSerializer import ProgramSerializer

# abstraction for isolating target-specific features

# Base class for representing target-specific configuration
class TargetConfig(object):
    def __init__(self, target):
        self.targetName = target

    def getIncludes(self):
        return ""

    def serializeLookup(self, serializer, tableName, key, value):
        serializer.appendFormat("{0} = bpf_map_lookup_elem(&{1}, &{2});",
                                value, tableName, key)

    def serializeUpdate(self, serializer, tableName, key, value):
        serializer.appendFormat(
            "bpf_map_update_elem(&{0}, &{1}, &{2}, BPF_ANY);",
            tableName, key, value)

    def serializeLicense(self, serializer, licenseString):
        assert isinstance(serializer, ProgramSerializer)
        serializer.emitIndent()
        serializer.appendFormat(
            "char _license[] {0}(\"license\") = \"{1}\";",
            self.config.section, licenseString)
        serializer.newline()

    def serializeCodeSection(self, serializer):
        assert isinstance(serializer, ProgramSerializer)
        serializer.appendFormat("{0}(\"{1}\")", self.section, self.entrySection)

    def serializeTableDeclaration(self, serializer, tableName,
                                  isHash, keyType, valueType, size):
        assert isinstance(serializer, ProgramSerializer)
        assert isinstance(tableName, str)
        assert isinstance(isHash, bool)
        assert isinstance(keyType, str)
        assert isinstance(valueType, str)
        assert isinstance(size, int)

        serializer.emitIndent()
        serializer.appendFormat("struct {0} {1}(\"maps\") {2} = ",
                                self.tableName, self.section, tableName)
        serializer.blockStart()

        serializer.emitIndent()
        serializer.append(".type = ")
        if isHash:
            serializer.appendLine("BPF_MAP_TYPE_HASH,")
        else:
            serializer.appendLine("BPF_MAP_TYPE_ARRAY,")

        serializer.emitIndent()
        serializer.appendFormat(".{0} = sizeof(struct {1}), ",
                                self.tableKeyAttribute, keyType)
        serializer.newline()

        serializer.emitIndent()
        serializer.appendFormat(".{0} = sizeof(struct {1}), ",
                                self.tableValueAttribute, valueType)
        serializer.newline()

        serializer.emitIndent()
        serializer.appendFormat(".{0} = {1}, ", self.tableSizeAttribute, size)
        serializer.newline()

        serializer.blockEnd(False)
        serializer.endOfStatement(True)

    def generateDword(self, serializer):
        serializer.appendFormat(
            "static inline {0}64 load_dword(void *skb, {0}64 off)",
            self.uprefix)
        serializer.newline()
        serializer.blockStart()
        serializer.emitIndent()
        serializer.appendFormat(
            ("return (({0}64)load_word(skb, off) << 32) | " +
             "load_word(skb, off + 4);"),
            self.uprefix)
        serializer.newline()
        serializer.blockEnd(True)


# Represents a target that is compiled within the kernel
# source tree samples folder and which attaches to a socket
class KernelSamplesConfig(TargetConfig):
    def __init__(self):
        super(SocketConfig, self).__init__("Socket")
        self.entrySection = "socket1"
        self.section = "SEC"
        self.uprefix = "u"
        self.iprefix = "i"
        self.tableKeyAttribute = "key_size"
        self.tableValueAttribute = "value_size"
        self.tableSizeAttribute = "max_entries"
        self.tableName = "bpf_map_def"
        self.postamble = ""

    def getIncludes(self):
        return """
#include <uapi/linux/bpf.h>
#include <uapi/linux/if_ether.h>
#include <uapi/linux/if_packet.h>
#include <uapi/linux/ip.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#include "bpf_helpers.h"
"""

    
# Represents a target compiled by bcc that uses the TC
class BccConfig(TargetConfig):
    def __init__(self):
        super(BccConfig, self).__init__("BCC")
        self.uprefix = "u"
        self.iprefix = "i"
        self.postamble = ""

    def serializeTableDeclaration(self, serializer, tableName,
                                  isHash, keyType, valueType, size):
        assert isinstance(serializer, ProgramSerializer)
        assert isinstance(tableName, str)
        assert isinstance(isHash, bool)
        assert isinstance(keyType, str)
        assert isinstance(valueType, str)
        assert isinstance(size, int)

        serializer.emitIndent()
        if isHash:
            kind = "hash"
        else:
            kind = "array"
        serializer.appendFormat(
            "BPF_TABLE(\"{0}\", {1}, {2}, {3}, {4});",
            kind, keyType, valueType, tableName, size)
        serializer.newline()

    def serializeLookup(self, serializer, tableName, key, value):
        serializer.appendFormat("{0} = {1}.lookup(&{2});",
                                value, tableName, key)

    def serializeUpdate(self, serializer, tableName, key, value):
        serializer.appendFormat("{0}.update(&{1}, &{2});",
                                tableName, key, value)

    def generateDword(self, serializer):
        pass

    def serializeCodeSection(self, serializer):
        pass

    def getIncludes(self):
        return """
#include <uapi/linux/bpf.h>
#include <uapi/linux/if_ether.h>
#include <uapi/linux/if_packet.h>
#include <uapi/linux/ip.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#include <linux/pkt_cls.h>
"""

    def serializeLicense(self, serializer, licenseString):
        assert isinstance(serializer, ProgramSerializer)
        pass