blob: e04b7229436f3272bb0047f2a8b5aa9e3e45ea8f [file] [log] [blame]
Rich Lane9cfa1652013-10-01 21:24:04 -07001:: # Copyright 2013, Big Switch Networks, Inc.
2:: #
3:: # LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with
4:: # the following special exception:
5:: #
6:: # LOXI Exception
7:: #
8:: # As a special exception to the terms of the EPL, you may distribute libraries
9:: # generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided
10:: # that copyright and licensing notices generated by LoxiGen are not altered or removed
11:: # from the LoxiGen Libraries and the notice provided below is (i) included in
12:: # the LoxiGen Libraries, if distributed in source code form and (ii) included in any
13:: # documentation for the LoxiGen Libraries, if distributed in binary form.
14:: #
15:: # Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler."
16:: #
17:: # You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain
18:: # a copy of the EPL at:
19:: #
20:: # http://www.eclipse.org/legal/epl-v10.html
21:: #
22:: # Unless required by applicable law or agreed to in writing, software
23:: # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24:: # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
25:: # EPL for the specific language governing permissions and limitations
26:: # under the EPL.
27::
Rich Lane972be332013-06-04 13:36:48 -070028:: import of_g
29:: ir = of_g.ir
Rich Laneeb10d162013-10-01 21:15:35 -070030:: include('_copyright.lua')
Rich Lane422d1b12013-06-04 13:09:17 -070031
Rich Lane872b95a2013-06-17 18:39:14 -070032:: include('_ofreader.lua')
33
Rich Lane7708c182013-10-01 23:27:27 -070034:: include('_oftype_readers.lua')
35
Rich Lane422d1b12013-06-04 13:09:17 -070036p_of = Proto ("of", "OpenFlow")
37
Rich Lane972be332013-06-04 13:36:48 -070038local openflow_versions = {
39:: for (version, name) in of_g.param_version_names.items():
40 [${version}] = "${name}",
41:: #endfor
Rich Lane422d1b12013-06-04 13:09:17 -070042}
43
Rich Lane972be332013-06-04 13:36:48 -070044:: for version, ofproto in ir.items():
45:: for enum in ofproto.enums:
46local enum_v${version}_${enum.name} = {
47:: for (name, value) in enum.values:
48 [${value}] = "${name}",
49:: #endfor
50}
51
52:: #endfor
53
54:: #endfor
55
Rich Lane364e0292013-10-01 21:05:57 -070056fields = {}
Rich Laneb014bcf2013-06-19 11:14:11 -070057:: for field in fields:
Rich Lane364e0292013-10-01 21:05:57 -070058fields[${repr(field.fullname)}] = ProtoField.new("${field.name}", "${field.fullname}", "FT_${field.type}", nil, "BASE_${field.base}")
Rich Laneb014bcf2013-06-19 11:14:11 -070059:: #endfor
Rich Lane422d1b12013-06-04 13:09:17 -070060
61p_of.fields = {
Rich Laneb014bcf2013-06-19 11:14:11 -070062:: for field in fields:
Rich Lane364e0292013-10-01 21:05:57 -070063 fields[${repr(field.fullname)}],
Rich Laneb014bcf2013-06-19 11:14:11 -070064:: #endfor
Rich Lane422d1b12013-06-04 13:09:17 -070065}
66
Rich Lane4bbdcf52013-10-01 22:07:35 -070067-- Subclass maps for virtual classes
Rich Lane96641df2013-06-10 13:36:35 -070068:: for version, ofproto in ir.items():
Rich Lane4bbdcf52013-10-01 22:07:35 -070069:: for ofclass in ofproto.classes:
70:: if ofclass.virtual:
71${ofclass.name}_v${version}_dissectors = {}
72:: #endif
Rich Lane96641df2013-06-10 13:36:35 -070073:: #endfor
Rich Lane96641df2013-06-10 13:36:35 -070074:: #endfor
75
Rich Lane4bbdcf52013-10-01 22:07:35 -070076--- Dissectors for each class
77
Rich Lane96641df2013-06-10 13:36:35 -070078:: for version, ofproto in ir.items():
79:: for ofclass in ofproto.classes:
80:: name = 'dissect_%s_v%d' % (ofclass.name, version)
Rich Lane7708c182013-10-01 23:27:27 -070081:: include('_ofclass_dissector.lua', name=name, ofclass=ofclass, version=version)
Rich Lane4bbdcf52013-10-01 22:07:35 -070082:: if ofclass.superclass:
Rich Lane586864e2013-10-01 23:26:35 -070083:: discriminator = ofproto.class_by_name(ofclass.superclass).discriminator
84:: discriminator_value = ofclass.member_by_name(discriminator.name).value
Rich Lane4bbdcf52013-10-01 22:07:35 -070085${ofclass.superclass}_v${version}_dissectors[${discriminator_value}] = ${name}
Rich Lane96641df2013-06-10 13:36:35 -070086
87:: #endif
88:: #endfor
89:: #endfor
90
Rich Lane4bbdcf52013-10-01 22:07:35 -070091local of_message_dissectors = {
92:: for version in ir:
93 [${version}] = of_header_v${version}_dissectors,
94:: #endfor
95}
96
Rich Lane96641df2013-06-10 13:36:35 -070097function dissect_of_message(buf, root)
Rich Lane93991e42013-10-01 22:24:51 -070098 local reader = OFReader.new(buf)
Rich Lane422d1b12013-06-04 13:09:17 -070099 local subtree = root:add(p_of, buf(0))
Rich Lane96641df2013-06-10 13:36:35 -0700100 local version_val = buf(0,1):uint()
Rich Lane422d1b12013-06-04 13:09:17 -0700101 local type_val = buf(1,1):uint()
Rich Laneb017c732013-10-02 14:05:01 -0700102
103 local protocol = "OF ?"
104 if openflow_versions[version_val] then
105 protocol = "OF " .. openflow_versions[version_val]
Rich Lane96641df2013-06-10 13:36:35 -0700106 end
Rich Laneb017c732013-10-02 14:05:01 -0700107
108 local info = "unknown"
109 if of_message_dissectors[version_val] and of_message_dissectors[version_val][type_val] then
110 info = of_message_dissectors[version_val][type_val](reader, subtree)
111 end
112
113 return protocol, info
Rich Lane422d1b12013-06-04 13:09:17 -0700114end
115
116-- of dissector function
117function p_of.dissector (buf, pkt, root)
Rich Lane422d1b12013-06-04 13:09:17 -0700118 local offset = 0
119 repeat
120 if buf:len() - offset >= 4 then
121 msg_len = buf(offset+2,2):uint()
122 if offset + msg_len > buf:len() then
123 -- we don't have all the data we need yet
124 pkt.desegment_len = offset + msg_len - buf:len()
125 return
126 end
127
Rich Laneb017c732013-10-02 14:05:01 -0700128 protocol, info = dissect_of_message(buf(offset, msg_len), root)
129
130 if offset == 0 then
131 pkt.cols.protocol:clear()
132 pkt.cols.info:clear()
133 else
134 pkt.cols.protocol:prepend(" + ")
135 pkt.cols.info:prepend(" + ")
136 end
137 pkt.cols.protocol:append(protocol)
138 pkt.cols.info:append(info)
Rich Lane422d1b12013-06-04 13:09:17 -0700139 offset = offset + msg_len
140 else
141 -- we don't have all of length field yet
142 pkt.desegment_len = DESEGMENT_ONE_MORE_SEGMENT
143 return
144 end
145 until offset >= buf:len()
146end
147
148-- Initialization routine
149function p_of.init()
150end
151
Rich Lane88d3afc2013-10-01 21:07:22 -0700152-- register a chained dissector for OpenFlow port numbers
Rich Lane422d1b12013-06-04 13:09:17 -0700153local tcp_dissector_table = DissectorTable.get("tcp.port")
154tcp_dissector_table:add(6633, p_of)
Rich Lane88d3afc2013-10-01 21:07:22 -0700155tcp_dissector_table:add(6653, p_of)