Re: Problem with setting up namespaces

Björn Döbel <[email protected]>
Newsgroups gmane.comp.micro-kernel.l4.devel
Message-ID <[email protected]>
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Johannes,

>> Well, I want to provide capabilities for an application by
>> putting them into a local namespace. Since our system changes
>> dynamically I want to add capabilities at runtime by registering
>> them to the corresponding namespace.

please find attached the clntsrv example adapted to use namespaces.
The general approach is:

1) In the Lua config create an empty namespace and make it accessible
   to your application(s). Note that the example also uses different
   access rights for client and server.

2) On the server side I use the object registry's register_object()
   function, which internally creates an IPC gate and binds the calling
   thread to it. Then I use the namespace's register_object() call to
   make the new channel externally visible.

3) In the client I query for the object name. This call blocks until
   the server has registered the new channel.

Apart from these steps, client and server implementation are identical
to the original example.

Regarding your approach of putting a channel into the namespace within
the Lua config file: This does not work for me. We are still trying to
figure out, why. It's probably indeed be related to the downgrading as
suggested by Christian.

Bjoern
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAlHoHZAACgkQP5ijxgQLUNlt0wCfX3qShBjEXdXvaXGmVtbk5IgG
Ys8An2vS/Uqj5qrFyKVxFAvGBB0BgtE0
=0Qez
-----END PGP SIGNATURE-----

_______________________________________________
l4-hackers mailing list
[email protected]
http://os.inf.tu-dresden.de/mailman/listinfo/l4-hackers
client.cc (text/x-c++src, 2.6 KB)
/*
 * Client-server example using namespaces. Client component.
 * 
 * (c) 2008-2013 Adam Lackorzynski <[email protected]>,
 *               Alexander Warg <[email protected]>,
 *               Bjoern Doebel <[email protected]>
 *     economic rights: Technische Universität Dresden (Germany)
 *
 * This file is part of TUD:OS and distributed under the terms of the
 * GNU General Public License 2.
 * Please see the COPYING-GPL-2 file for details.
 */
#include <l4/sys/err.h>
#include <l4/sys/types.h>
#include <l4/re/env>
#include <l4/re/namespace>
#include <l4/re/util/cap_alloc>
#include <l4/cxx/ipc_stream>

#include <stdio.h>

#include "shared.h"

static int
func_neg_call(L4::Cap<void> const &server, l4_uint32_t *result,
              l4_uint32_t val)
{
  L4::Ipc::Iostream s(l4_utcb());
  s << l4_umword_t(Opcode::func_neg) << val;
  int r = l4_error(s.call(server.cap(), Protocol::Calc));
  if (r)
    return r; // failure
  s >> *result;
  return 0; // ok
}

static int
func_sub_call(L4::Cap<void> const &server, l4_uint32_t *result,
              l4_uint32_t val1, l4_uint32_t val2)
{
  L4::Ipc::Iostream s(l4_utcb());
  s << l4_umword_t(Opcode::func_sub) << val1 << val2;
  int r = l4_error(s.call(server.cap(), Protocol::Calc));
  if (r)
    return r; // failure
  s >> *result;
  return 0; // ok
}

int
main()
{
  /*
   * Get the namespace cap which comes from
   * our initial set of capabilities. (See ns.cfg)
   */
  L4::Cap<L4Re::Namespace> ns = L4Re::Env::env()->get_cap<L4Re::Namespace>("namespace");
  if (!ns.is_valid())
    {
      printf("Could not find namespace\n");
      return 1;
    }
  
  /*
   * We will query the (shared) namespace for an object. This object needs
   * to go into a local capability slot. Hence, we need to reserve one here.
   */
  L4::Cap<void> server = L4Re::Util::cap_alloc.alloc<void>();
  
  /*
   * Now query the object name. This blocks until the object appears.
   */
  long r = ns->query("the_object", server, L4Re::Namespace::To_forever);
  if (r < 0)
    {
      printf("Error querying object: %ld\n", r);
      return 1;
    }

  if (!server.is_valid())
    {
      printf("Could not get server capability!\n");
      return 1;
    }

  l4_uint32_t val1 = 8;
  l4_uint32_t val2 = 5;

  printf("Asking for %d - %d\n", val1, val2);

  if (func_sub_call(server, &val1, val1, val2))
    {
      printf("Error talking to server\n");
      return 1;
    }
  printf("Result of substract call: %d\n", val1);
  printf("Asking for -%d\n", val1);
  if (func_neg_call(server, &val1, val1))
    {
      printf("Error talking to server\n");
      return 1;
    }
  printf("Result of negate call: %d\n", val1);

  return 0;
}
Makefile (text/plain, 250 B)
PKGDIR          ?= ..
L4DIR           ?= $(PKGDIR)/../../../../..

TARGET              = ex_ns-server ex_ns-client
SRC_CC_ex_ns-server = server.cc
SRC_CC_ex_ns-client = client.cc
REQUIRES_LIBS       = cxx_libc_io cxx_io

include $(L4DIR)/mk/prog.mk
ns.cfg (text/plain, 787 B)
-- vim:set ft=lua:

-- Include L4 functionality
require("L4");

-- Some shortcut for less typing
local ld = L4.default_loader;

-- Namespace to exchange objects
local ns = ld:create_namespace({});

-- The server program, getting the namespace with extended
-- access rights (read + write)
ld:start({ caps = { namespace = ns:m("rw") },
           log = { "server", "cyan" } },
           "rom/ex_ns-server");

-- The client program, getting the 'calc_server' channel to be able to talk
-- to the server. The client will be started with a green log output.
-- Note, the client only gets read rights for the namespace, so it may
-- query, but cannot register own objects.
ld:start({ caps = { namespace = ns:m("r") },
           log = { "client", "green" } },
          "rom/ex_ns-client");
server.cc (text/x-c++src, 2.6 KB)
/*
 * Client-server example using namespaces. Server component.
 * 
 * (c) 2008-2013 Adam Lackorzynski <[email protected]>,
 *               Alexander Warg <[email protected]>
 *               Bjoern Doebel  <[email protected]>
 *     economic rights: Technische Universität Dresden (Germany)
 *
 * This file is part of TUD:OS and distributed under the terms of the
 * GNU General Public License 2.
 * Please see the COPYING-GPL-2 file for details.
 */
#include <stdio.h>
#include <l4/re/env>
#include <l4/re/namespace>
#include <l4/re/util/cap_alloc>
#include <l4/re/util/object_registry>
#include <l4/cxx/ipc_server>

#include "shared.h"

static L4Re::Util::Registry_server<> server;

class Calculation_server : public L4::Server_object
{
public:
  int dispatch(l4_umword_t obj, L4::Ipc::Iostream &ios);
};

int
Calculation_server::dispatch(l4_umword_t, L4::Ipc::Iostream &ios)
{
  l4_msgtag_t t;
  ios >> t;

  // We're only talking the calculation protocol
  if (t.label() != Protocol::Calc)
    return -L4_EBADPROTO;

  L4::Opcode opcode;
  ios >> opcode;

  switch (opcode)
    {
    case Opcode::func_neg:
      l4_uint32_t val;
      ios >> val;
      val = -val;
      ios << val;
      return L4_EOK;
    case Opcode::func_sub:
      l4_uint32_t val1, val2;
      ios >> val1 >> val2;
      val1 -= val2;
      ios << val1;
      return L4_EOK;
    default:
      return -L4_ENOSYS;
    }
}

int
main()
{
  static Calculation_server calc;
  L4::Cap<L4Re::Namespace> ns;

  /*
   * In this example we register the object _without_ a name
   * at the registry server. This will create a new IPC gate,
   * attach our server object, and bind the current thread to it.
   */
  if (!server.registry()->register_obj(&calc).is_valid())
    {
      printf("Could not register server object\n");
      return 1;
    }

  /*
   * We get an empty namespace through our initial
   * set of capabilities. (See ns.cfg)
   */
  ns = L4Re::Env::env()->get_cap<L4Re::Namespace>("namespace");
  if (!ns.is_valid())
    {
      printf("Could not find namespace\n");
      return 1;
    }

  /*
   * Register object in the namespace.
   *
   * NOTE: This is *not* the same as registering the object
   *       in our local registry. The registry tracks local
   *       server objects. The namespace tracks capabilities
   *       across tasks.
   */
  long r = ns->register_obj("the_object", calc.obj_cap());
  if (r < 0)
    {
      printf("Error registering object in namespace: %ld\n", r);
      return 1;
    }

  printf("Welcome to the calculation server!\n"
         "I can do substractions and negations.\n");

  // Wait for client requests
  server.loop();

  return 0;
}
shared.h (text/x-chdr, 421 B)
/*
 * (c) 2008-2009 Adam Lackorzynski <[email protected]>
 *     economic rights: Technische Universität Dresden (Germany)
 *
 * This file is part of TUD:OS and distributed under the terms of the
 * GNU General Public License 2.
 * Please see the COPYING-GPL-2 file for details.
 */

#pragma once


namespace Opcode {
enum Opcodes {
  func_sub, func_neg
};
};

namespace Protocol {
enum Protocols {
  Calc
};
};
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.