vtep: Limit the split elements to 2 (maxsplit + 1)
[cascardo/ovs.git] / lib / packet-dpif.c
1 /*
2  * Copyright (c) 2014 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "packet-dpif.h"
19
20 #include "ofpbuf.h"
21
22 struct dpif_packet *
23 dpif_packet_new_with_headroom(size_t size, size_t headroom)
24 {
25     struct dpif_packet *p = xmalloc(sizeof *p);
26     struct ofpbuf *b = &p->ofpbuf;
27
28     ofpbuf_init(b, size + headroom);
29     ofpbuf_reserve(b, headroom);
30     p->md = PKT_METADATA_INITIALIZER(0);
31
32     return p;
33 }
34
35 struct dpif_packet *
36 dpif_packet_clone_from_ofpbuf(const struct ofpbuf *b)
37 {
38     struct dpif_packet *p = xmalloc(sizeof *p);
39     size_t headroom = ofpbuf_headroom(b);
40
41     ofpbuf_init(&p->ofpbuf, ofpbuf_size(b) + headroom);
42     p->md = PKT_METADATA_INITIALIZER(0);
43     ofpbuf_reserve(&p->ofpbuf, headroom);
44
45     ofpbuf_put(&p->ofpbuf, ofpbuf_data(b), ofpbuf_size(b));
46
47     if (b->frame) {
48         uintptr_t data_delta
49             = (char *)ofpbuf_data(&p->ofpbuf) - (char *)ofpbuf_data(b);
50
51         p->ofpbuf.frame = (char *) b->frame + data_delta;
52     }
53     p->ofpbuf.l2_5_ofs = b->l2_5_ofs;
54     p->ofpbuf.l3_ofs = b->l3_ofs;
55     p->ofpbuf.l4_ofs = b->l4_ofs;
56
57     return p;
58 }
59
60 struct dpif_packet *
61 dpif_packet_clone(struct dpif_packet *p)
62 {
63     struct dpif_packet *newp;
64
65     newp = dpif_packet_clone_from_ofpbuf(&p->ofpbuf);
66     memcpy(&newp->md, &p->md, sizeof p->md);
67
68     dpif_packet_set_dp_hash(newp, dpif_packet_get_dp_hash(p));
69
70     return newp;
71 }