xref: /openbmc/qemu/hw/net/rocker/rocker_world.c (revision 502d0f36)
1 /*
2  * QEMU rocker switch emulation - switch worlds
3  *
4  * Copyright (c) 2014 Scott Feldman <sfeldma@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  */
16 
17 #include "qemu/osdep.h"
18 #include "qemu/iov.h"
19 
20 #include "rocker.h"
21 #include "rocker_world.h"
22 
23 struct world {
24     Rocker *r;
25     enum rocker_world_type type;
26     WorldOps *ops;
27 };
28 
29 ssize_t world_ingress(World *world, uint32_t pport,
30                       const struct iovec *iov, int iovcnt)
31 {
32     if (world->ops->ig) {
33         return world->ops->ig(world, pport, iov, iovcnt);
34     }
35 
36     return -1;
37 }
38 
39 int world_do_cmd(World *world, DescInfo *info,
40                  char *buf, uint16_t cmd, RockerTlv *cmd_info_tlv)
41 {
42     if (world->ops->cmd) {
43         return world->ops->cmd(world, info, buf, cmd, cmd_info_tlv);
44     }
45 
46     return -ROCKER_ENOTSUP;
47 }
48 
49 World *world_alloc(Rocker *r, size_t sizeof_private,
50                    enum rocker_world_type type, WorldOps *ops)
51 {
52     World *w = g_malloc0(sizeof(World) + sizeof_private);
53 
54     w->r = r;
55     w->type = type;
56     w->ops = ops;
57     if (w->ops->init) {
58         w->ops->init(w);
59     }
60 
61     return w;
62 }
63 
64 void world_free(World *world)
65 {
66     if (world->ops->uninit) {
67         world->ops->uninit(world);
68     }
69     g_free(world);
70 }
71 
72 void world_reset(World *world)
73 {
74     if (world->ops->uninit) {
75         world->ops->uninit(world);
76     }
77     if (world->ops->init) {
78         world->ops->init(world);
79     }
80 }
81 
82 void *world_private(World *world)
83 {
84     return world + 1;
85 }
86 
87 Rocker *world_rocker(World *world)
88 {
89     return world->r;
90 }
91 
92 enum rocker_world_type world_type(World *world)
93 {
94     return world->type;
95 }
96 
97 const char *world_name(World *world)
98 {
99     return world->ops->name;
100 }
101